|
1 | 1 | --- |
2 | 2 | - name: Set OS dependent variables |
3 | | - include_vars: "{{ item }}" |
| 3 | + include_vars: '{{ item }}' |
4 | 4 | with_first_found: |
5 | | - - "{{ ansible_distribution }}_{{ ansible_distribution_major_version }}.yml" |
6 | | - - "{{ ansible_distribution }}.yml" |
7 | | - - "{{ ansible_os_family }}_{{ ansible_distribution_major_version }}.yml" |
8 | | - - "{{ ansible_os_family }}.yml" |
| 5 | + - '{{ ansible_distribution }}_{{ ansible_distribution_major_version }}.yml' |
| 6 | + - '{{ ansible_distribution }}.yml' |
| 7 | + - '{{ ansible_os_family }}_{{ ansible_distribution_major_version }}.yml' |
| 8 | + - '{{ ansible_os_family }}.yml' |
9 | 9 |
|
10 | 10 | - name: get openssh-version |
11 | 11 | shell: ssh -V 2>&1 | sed -r 's/.*_([0-9]*\.[0-9]).*/\1/g' |
|
18 | 18 | - include_tasks: crypto.yml |
19 | 19 |
|
20 | 20 | - name: create revoked_keys and set permissions to root/600 |
21 | | - template: src='revoked_keys.j2' dest='/etc/ssh/revoked_keys' mode=0600 owner="{{ ssh_owner }}" group="{{ ssh_group }}" |
| 21 | + template: |
| 22 | + src: 'revoked_keys.j2' |
| 23 | + dest: '/etc/ssh/revoked_keys' |
| 24 | + mode: '0600' |
| 25 | + owner: '{{ ssh_owner }}' |
| 26 | + group: '{{ ssh_group }}' |
22 | 27 | notify: restart sshd |
23 | 28 | when: ssh_server_hardening |
24 | 29 |
|
25 | 30 | - name: create sshd_config and set permissions to root/600 |
26 | | - template: src='opensshd.conf.j2' dest='/etc/ssh/sshd_config' mode=0600 owner="{{ ssh_owner }}" group="{{ ssh_group }}" validate="/usr/sbin/sshd -T -f %s" |
| 31 | + template: |
| 32 | + src: 'opensshd.conf.j2' |
| 33 | + dest: '/etc/ssh/sshd_config' |
| 34 | + mode: '0600' |
| 35 | + owner: '{{ ssh_owner }}' |
| 36 | + group: '{{ ssh_group }}' |
| 37 | + validate: '/usr/sbin/sshd -T -f %s' |
27 | 38 | notify: restart sshd |
28 | 39 | when: ssh_server_hardening |
29 | 40 |
|
30 | 41 | - name: create ssh_config and set permissions to root/644 |
31 | | - template: src='openssh.conf.j2' dest='/etc/ssh/ssh_config' mode=0644 owner="{{ ssh_owner }}" group="{{ ssh_group }}" |
| 42 | + template: |
| 43 | + src: 'openssh.conf.j2' |
| 44 | + dest: '/etc/ssh/ssh_config' |
| 45 | + mode: '0644' |
| 46 | + owner: '{{ ssh_owner }}' |
| 47 | + group: '{{ ssh_group }}' |
32 | 48 | when: ssh_client_hardening |
33 | 49 |
|
34 | 50 | - name: Check if /etc/ssh/moduli contains weak DH parameters |
|
46 | 62 | - include_tasks: ca_keys_and_principals.yml |
47 | 63 | when: ssh_trusted_user_ca_keys_file != '' |
48 | 64 |
|
49 | | -- name: test to see if selinux is installed and running |
50 | | - command: getenforce |
51 | | - register: sestatus |
52 | | - failed_when: false |
53 | | - changed_when: false |
54 | | - check_mode: no |
55 | | - |
56 | 65 | # Install the 2FA packages and setup the config in PAM and SSH |
57 | | - |
58 | | -- block: |
59 | | - - name: Install google authenticator PAM module |
60 | | - apt: name=libpam-google-authenticator state=present |
61 | | - when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' |
62 | | - |
63 | | - - name: Install google authenticator PAM module |
64 | | - yum: name=google-authenticator state=present |
65 | | - when: ansible_os_family == 'RedHat' or ansible_os_family == 'Oracle Linux' |
66 | | - |
67 | | - - name: Add google auth module to PAM |
68 | | - pamd: |
69 | | - name: sshd |
70 | | - type: auth |
71 | | - control: required |
72 | | - module_path: pam_google_authenticator.so |
73 | | - |
74 | | - - name: Remove password auth from PAM |
75 | | - pamd: |
76 | | - name: sshd |
77 | | - type: auth |
78 | | - control: substack |
79 | | - module_path: password-auth |
80 | | - state: absent |
81 | | - when: ansible_distribution == 'RedHat' or ansible_distribution == 'Oracle Linux' or ansible_distribution == 'Amazon' |
82 | | - |
83 | | - - name: Remove password auth from PAM |
84 | | - replace: |
85 | | - dest: /etc/pam.d/sshd |
86 | | - regexp: '^@include common-auth' |
87 | | - replace: '#@include common-auth' |
88 | | - when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' |
89 | | - |
| 66 | +- include_tasks: 2fa.yml |
90 | 67 | when: |
91 | 68 | - ssh_use_pam |
92 | 69 | - ssh_challengeresponseauthentication |
93 | 70 | - ssh_google_auth |
94 | 71 |
|
| 72 | +- name: test to see if selinux is installed and running |
| 73 | + command: getenforce |
| 74 | + register: sestatus |
| 75 | + failed_when: false |
| 76 | + changed_when: false |
| 77 | + check_mode: no |
95 | 78 |
|
96 | | -- block: # only runs when selinux is installed |
97 | | - - name: install selinux dependencies when selinux is installed on RHEL or Oracle Linux |
98 | | - package: name="{{item}}" state=present |
99 | | - with_items: |
100 | | - - policycoreutils-python |
101 | | - - checkpolicy |
102 | | - when: ansible_os_family == 'RedHat' or ansible_os_family == 'Oracle Linux' |
103 | | - |
104 | | - - name: install selinux dependencies when selinux is installed on Debian or Ubuntu |
105 | | - apt: name="{{item}}" state=present |
106 | | - with_items: |
107 | | - - policycoreutils |
108 | | - - checkpolicy |
109 | | - when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' |
110 | | - |
111 | | - - name: check if ssh_password module is already installed |
112 | | - shell: "semodule -l | grep ssh_password" |
113 | | - register: ssh_password_module |
114 | | - failed_when: false |
115 | | - changed_when: false |
116 | | - check_mode: no |
117 | | - |
118 | | - # The following tasks only get executed when selinux is in state enforcing, UsePam is "no" and the ssh_password module is installed. |
119 | | - # See this issue for more info: https://github.com/hardening-io/ansible-ssh-hardening/issues/23 |
120 | | - - block: |
121 | | - - name: Create selinux custom policy drop folder |
122 | | - file: path='{{ ssh_custom_selinux_dir }}' state=directory owner=root group=root mode=0750 |
123 | | - |
124 | | - - name: Distributing custom selinux policies |
125 | | - copy: src='ssh_password' dest='{{ ssh_custom_selinux_dir }}' |
126 | | - |
127 | | - - name: check and compile policy |
128 | | - shell: checkmodule -M -m -o {{ ssh_custom_selinux_dir }}/ssh_password.mod {{ ssh_custom_selinux_dir }}/ssh_password |
129 | | - |
130 | | - - name: create selinux policy module package |
131 | | - shell: semodule_package -o {{ ssh_custom_selinux_dir }}/ssh_password.pp -m {{ ssh_custom_selinux_dir }}/ssh_password.mod |
132 | | - |
133 | | - - name: install selinux policy |
134 | | - shell: semodule -i {{ ssh_custom_selinux_dir }}/ssh_password.pp |
135 | | - |
136 | | - when: not ssh_use_pam and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0 |
137 | | - |
138 | | - # The following tasks only get executed when selinux is installed, UsePam is "yes" and the ssh_password module is installed. |
139 | | - - name: remove selinux-policy when Pam is used, because Allowing sshd to read the shadow file directly is considered a potential security risk (http://danwalsh.livejournal.com/12333.html) |
140 | | - command: semodule -r ssh_password |
141 | | - when: ssh_use_pam and ssh_password_module.stdout.find('ssh_password') == 0 |
142 | | - |
| 79 | +- include_tasks: selinux.yml |
143 | 80 | when: sestatus.rc == 0 |
0 commit comments