Skip to content
This repository was archived by the owner on Dec 26, 2020. It is now read-only.

Commit 8420dac

Browse files
committed
Fix ssh config to handle custom options per Host
Formerly printed Hosts on successive lines, applying all options to the final Host only.
1 parent 86b7301 commit 8420dac

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Warning: This role disables root-login on the target server! Please make sure yo
3030
|`ssh_host_key_files` | ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_dsa_key', '/etc/ssh/ssh_host_ecdsa_key'] |Host keys to look for when starting sshd.|
3131
|`ssh_client_alive_interval` | 600 | specifies an interval for sending keepalive messages |
3232
|`ssh_client_alive_count` | 3 | defines how often keep-alive messages are sent |
33-
|`ssh_remote_hosts` | [] | one or more hosts, to which ssh-client can connect to. Default is empty, but should be configured for security reasons!|
33+
|`ssh_remote_hosts` | [] | one or more hosts and their custom options for the ssh-client. Default is empty. See examples in `defaults/main.yml`.|
3434
|`ssh_allow_root_with_key` | false | false to disable root login altogether. Set to true to allow root to login via key-based mechanism.|
3535
|`ssh_allow_tcp_forwarding` | false | false to disable TCP Forwarding. Set to true to allow TCP Forwarding.|
3636
|`ssh_allow_agent_forwarding` | false | false to disable Agent Forwarding. Set to true to allow Agent Forwarding.|

default.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: wrapper playbook for kitchen testing "ansible-ssh-hardening" with default settings
2+
- name: wrapper playbook for kitchen testing "ansible-ssh-hardening" with custom settings
33
hosts: localhost
44
pre_tasks:
55
- package: name="{{item}}" state=installed
@@ -15,3 +15,20 @@
1515
- file: path="/var/run/sshd" state=directory
1616
roles:
1717
- ansible-ssh-hardening
18+
vars:
19+
network_ipv6_enable: true
20+
ssh_allow_root_with_key: true
21+
ssh_client_password_login: true
22+
ssh_client_cbc_required: true
23+
ssh_server_weak_hmac: true
24+
ssh_client_weak_kex: true
25+
ssh_remote_hosts:
26+
- names: ['example.com', 'example2.com']
27+
options: ['Port 2222', 'ForwardAgent yes']
28+
- names: ['example3.com']
29+
options: ['StrictHostKeyChecking no']
30+
31+
- name: wrapper playbook for kitchen testing "ansible-ssh-hardening" with default settings
32+
hosts: localhost
33+
roles:
34+
- ansible-ssh-hardening

defaults/main.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@ ssh_max_auth_retries: 2
3737

3838
ssh_client_alive_interval: 600 # sshd
3939
ssh_client_alive_count: 3 # sshd
40-
# one or more hosts, to which ssh-client can connect to. Default is empty, but should be configured for security reasons!
41-
ssh_remote_hosts: [] # ssh
40+
41+
# Hosts with custom options. # ssh
42+
# Example:
43+
# ssh_remote_hosts:
44+
# - names: ['example.com', 'example2.com']
45+
# options: ['Port 2222', 'ForwardAgent yes']
46+
# - names: ['example3.com']
47+
# options: ['StrictHostKeyChecking no']
48+
ssh_remote_hosts: []
49+
4250
# false to disable root login altogether. Set to true to allow root to login via key-based mechanism.
4351
ssh_allow_root_with_key: false # sshd
4452

templates/openssh.conf.j2

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88

99
# Address family should always be limited to the active network configuration.
1010
AddressFamily {{ 'any' if network_ipv6_enable else 'inet' }}
11-
# Restrict the following configuration to be limited to this Host.
11+
1212
{% for host in ssh_remote_hosts -%}
13-
Host {{host}}
14-
{% endfor %}
13+
{% if loop.first %}
14+
# Host-specific configuration
15+
{% endif %}
16+
Host {{ host.names | join(' ') }}
17+
{{ host.options | join("\n") | indent(2) }}
18+
19+
{% endfor -%}
20+
21+
# Global defaults for all Hosts
22+
Host *
1523

1624
# The port at the destination should be defined
1725
{% for port in ssh_client_ports -%}

0 commit comments

Comments
 (0)