|
2 | 2 | - name: add the OS specific variables |
3 | 3 | include_vars: "{{ ansible_os_family }}.yml" |
4 | 4 |
|
5 | | -- name: test to see if selinux is running |
6 | | - command: getenforce |
7 | | - register: sestatus |
8 | | - failed_when: false |
9 | | - changed_when: false |
10 | | - always_run: true |
11 | | - |
12 | | -- name: install selinux dependencies when selinux is installed on RHEL or Oracle Linux |
13 | | - yum: name="{{item}}" state=installed |
14 | | - with_items: |
15 | | - - policycoreutils-python |
16 | | - - checkpolicy |
17 | | - when: sestatus.rc == 0 and (ansible_os_family == 'RedHat' or ansible_os_family == 'Oracle Linux') |
18 | | - |
19 | | -- name: install selinux dependencies when selinux is installed on Debian or Ubuntu |
20 | | - apt: name="{{item}}" state=installed |
21 | | - with_items: |
22 | | - - policycoreutils |
23 | | - - checkpolicy |
24 | | - when: sestatus.rc == 0 and (ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu') |
25 | | - |
26 | | -- name: check the ssh_password policy state |
27 | | - shell: semodule -l | grep "ssh_password" | awk '{print $3}' |
28 | | - register: selinux_policy_state |
29 | | - when: sestatus.rc == 0 and sestatus.stdout != 'Disabled' |
30 | | - failed_when: false |
31 | | - changed_when: false |
32 | | - |
33 | 5 | - name: create sshd_config and set permissions to root/600 |
34 | 6 | template: src='opensshd.conf.j2' dest='/etc/ssh/sshd_config' mode=0600 owner=root group=root validate="/usr/sbin/sshd -T -f %s" |
35 | 7 | notify: restart sshd |
|
39 | 11 | template: src='openssh.conf.j2' dest='/etc/ssh/ssh_config' mode=0644 owner=root group=root |
40 | 12 | when: ssh_client_hardening |
41 | 13 |
|
42 | | -- name: check if ssh_password module is already installed |
43 | | - shell: "semodule -l| grep ssh_password" |
44 | | - register: ssh_password_module |
| 14 | +- name: create ssh_config and set permissions to root/644 |
| 15 | + template: src='openssh.conf.j2' dest='/etc/ssh/ssh_config' mode=0644 owner=root group=root |
| 16 | + when: ssh_client_hardening |
| 17 | + |
| 18 | +- name: test to see if selinux is running |
| 19 | + command: getenforce |
| 20 | + register: sestatus |
45 | 21 | failed_when: false |
46 | 22 | changed_when: false |
47 | | - always_run: true |
| 23 | + check_mode: no |
| 24 | + |
| 25 | +- block: # only runs when selinux is running |
| 26 | + - name: install selinux dependencies when selinux is installed on RHEL or Oracle Linux |
| 27 | + yum: name="{{item}}" state=installed |
| 28 | + with_items: |
| 29 | + - policycoreutils-python |
| 30 | + - checkpolicy |
| 31 | + when: ansible_os_family == 'RedHat' or ansible_os_family == 'Oracle Linux' |
| 32 | + |
| 33 | + - name: install selinux dependencies when selinux is installed on Debian or Ubuntu |
| 34 | + apt: name="{{item}}" state=installed |
| 35 | + with_items: |
| 36 | + - policycoreutils |
| 37 | + - checkpolicy |
| 38 | + when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' |
| 39 | + |
| 40 | + - name: check if ssh_password module is already installed |
| 41 | + shell: "semodule -l| grep ssh_password" |
| 42 | + register: ssh_password_module |
| 43 | + failed_when: false |
| 44 | + changed_when: false |
| 45 | + check_mode: no |
| 46 | + |
| 47 | + # The following tasks only get executed when selinux is in state enforcing, UsePam is "no" and the ssh_password module is installed. |
| 48 | + # See this issue for more info: https://github.com/hardening-io/ansible-ssh-hardening/issues/23 |
| 49 | + - block: |
| 50 | + - name: Create selinux custom policy drop folder |
| 51 | + file: path='{{ ssh_custom_selinux_dir }}' state=directory owner=root group=root mode=0750 |
48 | 52 |
|
49 | | -# The following tasks only get executed when selinux is in state enforcing, UsePam is "no" and the ssh_password module is installed. |
50 | | -# See this issue for more info: https://github.com/hardening-io/ansible-ssh-hardening/issues/23 |
| 53 | + - name: Distributing custom selinux policies |
| 54 | + copy: src='ssh_password' dest='{{ ssh_custom_selinux_dir }}' |
51 | 55 |
|
52 | | -- name: Create selinux custom policy drop folder |
53 | | - file: path={{ ssh_custom_selinux_dir }} state=directory owner=root group=root mode=0750 |
54 | | - when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0 |
| 56 | + - name: check and compile policy |
| 57 | + shell: checkmodule -M -m -o {{ ssh_custom_selinux_dir }}/ssh_password.mod {{ ssh_custom_selinux_dir }}/ssh_password |
55 | 58 |
|
56 | | -- name: Distributing custom selinux policies |
57 | | - copy: src='ssh_password' dest='{{ ssh_custom_selinux_dir }}' |
58 | | - when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0 |
| 59 | + - name: create selinux policy module package |
| 60 | + shell: semodule_package -o {{ ssh_custom_selinux_dir }}/ssh_password.pp -m {{ ssh_custom_selinux_dir }}/ssh_password.mod |
59 | 61 |
|
60 | | -- name: check and compile policy |
61 | | - shell: checkmodule -M -m -o {{ ssh_custom_selinux_dir }}/ssh_password.mod {{ ssh_custom_selinux_dir }}/ssh_password |
62 | | - when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0 |
| 62 | + - name: install selinux policy |
| 63 | + shell: semodule -i {{ ssh_custom_selinux_dir }}/ssh_password.pp |
63 | 64 |
|
64 | | -- name: create selinux policy module package |
65 | | - shell: semodule_package -o {{ ssh_custom_selinux_dir }}/ssh_password.pp -m {{ ssh_custom_selinux_dir }}/ssh_password.mod |
66 | | - when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0 |
| 65 | + when: not ssh_use_pam and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0 |
67 | 66 |
|
68 | | -- name: install selinux policy |
69 | | - shell: semodule -i {{ ssh_custom_selinux_dir }}/ssh_password.pp |
70 | | - when: not ssh_use_pam and sestatus.rc == 0 and sestatus.stdout != 'Disabled' and ssh_password_module.stdout.find('ssh_password') != 0 |
| 67 | + # The following tasks only get executed when selinux is in state enforcing, UsePam is "yes" and the ssh_password module is installed. |
| 68 | + - 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) |
| 69 | + shell: semodule -r ssh_password |
| 70 | + when: ssh_use_pam and ssh_password_module.stdout.find('ssh_password') == 0 |
71 | 71 |
|
72 | | -# The following tasks only get executed when selinux is in state enforcing, UsePam is "yes" and the ssh_password module is installed. |
73 | | -- 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) |
74 | | - shell: semodule -r ssh_password |
75 | | - when: sestatus.rc == 0 and ssh_use_pam and ssh_password_module.stdout.find('ssh_password') == 0 |
| 72 | + when: sestatus.rc == 0 |
0 commit comments