2021-06-02 12:05:51 +02:00
|
|
|
---
|
2023-04-10 22:19:29 +02:00
|
|
|
- name: Test alternative role file - setup tasks
|
|
|
|
hosts: all
|
2021-06-02 12:05:51 +02:00
|
|
|
vars:
|
|
|
|
__sshd_test_backup_files:
|
|
|
|
- /etc/ssh/sshd_config
|
|
|
|
- /etc/ssh/sshd_config.d/00-ansible_system_role.conf
|
|
|
|
- /etc/ssh/sshd_config_custom
|
|
|
|
- /etc/ssh/sshd_config_custom_second
|
2022-04-06 11:10:27 +02:00
|
|
|
- /tmp/ssh_host_ecdsa_key
|
2021-06-02 12:05:51 +02:00
|
|
|
tasks:
|
|
|
|
- name: "Backup configuration files"
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.include_tasks: tasks/backup.yml
|
2021-06-02 12:05:51 +02:00
|
|
|
|
|
|
|
- name: Ensure group 'nobody' exists
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.group:
|
2021-06-02 12:05:51 +02:00
|
|
|
name: nobody
|
|
|
|
|
|
|
|
- name: Ensure the user 'nobody' exists
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.user:
|
2021-06-02 12:05:51 +02:00
|
|
|
name: nobody
|
|
|
|
group: nobody
|
|
|
|
comment: nobody
|
2023-04-10 22:19:29 +02:00
|
|
|
create_home: false
|
2021-06-02 12:05:51 +02:00
|
|
|
shell: /sbin/nologin
|
|
|
|
|
|
|
|
# Configure alternative sshd_config file
|
2023-04-10 22:19:29 +02:00
|
|
|
- name: Test first alternative role file
|
|
|
|
hosts: all
|
2021-06-02 12:05:51 +02:00
|
|
|
roles:
|
|
|
|
- ansible-sshd
|
|
|
|
vars:
|
|
|
|
# just anything -- will not get processed by sshd
|
|
|
|
sshd_config_file: /etc/ssh/sshd_config_custom
|
|
|
|
sshd_config_owner: "nobody"
|
|
|
|
sshd_config_group: "nobody"
|
|
|
|
sshd_config_mode: "660"
|
2024-10-24 18:59:04 +02:00
|
|
|
sshd_config:
|
2021-06-02 12:05:51 +02:00
|
|
|
AcceptEnv: LANG
|
|
|
|
Banner: /etc/issue
|
|
|
|
Ciphers: aes256-ctr
|
2023-04-10 22:19:29 +02:00
|
|
|
sshd_Compression: false # noqa var-naming
|
2021-06-02 12:05:51 +02:00
|
|
|
|
|
|
|
# Configure second alternative sshd_config file
|
2023-04-10 22:19:29 +02:00
|
|
|
- name: Test first alternative role file
|
|
|
|
hosts: all
|
2021-06-02 12:05:51 +02:00
|
|
|
roles:
|
|
|
|
- ansible-sshd
|
|
|
|
vars:
|
|
|
|
# just anything -- will not get processed by sshd
|
|
|
|
sshd_config_file: /etc/ssh/sshd_config_custom_second
|
|
|
|
sshd_skip_defaults: true
|
2024-10-24 18:59:04 +02:00
|
|
|
sshd_config:
|
2021-06-02 12:05:51 +02:00
|
|
|
Banner: /etc/issue2
|
|
|
|
Ciphers: aes128-ctr
|
2022-06-03 12:22:17 +02:00
|
|
|
sshd_MaxStartups: 100 # noqa var-naming
|
2021-06-02 12:05:51 +02:00
|
|
|
|
|
|
|
# Now configure the main sshd_config file
|
2023-04-10 22:19:29 +02:00
|
|
|
- name: Test main config file
|
|
|
|
hosts: all
|
2021-06-02 12:05:51 +02:00
|
|
|
roles:
|
|
|
|
- ansible-sshd
|
|
|
|
vars:
|
2022-04-06 11:10:27 +02:00
|
|
|
sshd_config_file: /etc/ssh/sshd_config
|
2024-10-24 18:59:04 +02:00
|
|
|
sshd_config:
|
2021-06-02 12:05:51 +02:00
|
|
|
Banner: /etc/issue
|
|
|
|
Ciphers: aes192-ctr
|
|
|
|
HostKey:
|
|
|
|
- /tmp/ssh_host_ecdsa_key
|
2023-04-10 22:19:29 +02:00
|
|
|
sshd_PasswordAuthentication: false # noqa var-naming
|
2021-06-02 12:05:51 +02:00
|
|
|
|
2023-04-10 22:19:29 +02:00
|
|
|
- name: Verify config files are correct
|
|
|
|
hosts: all
|
2021-06-02 12:05:51 +02:00
|
|
|
vars:
|
|
|
|
__sshd_test_backup_files:
|
|
|
|
- /etc/ssh/sshd_config
|
|
|
|
- /etc/ssh/sshd_config.d/00-ansible_system_role.conf
|
|
|
|
- /etc/ssh/sshd_config_custom
|
|
|
|
- /etc/ssh/sshd_config_custom_second
|
|
|
|
tasks:
|
2021-08-03 10:09:13 +02:00
|
|
|
- name: Setup variables
|
2023-04-10 22:19:29 +02:00
|
|
|
ansible.builtin.include_tasks: tasks/setup.yml
|
2021-08-03 10:09:13 +02:00
|
|
|
|
2021-06-02 12:05:51 +02:00
|
|
|
- name: Verify the options are correctly set
|
2023-04-10 22:19:29 +02:00
|
|
|
tags: tests::verify
|
2021-06-02 12:05:51 +02:00
|
|
|
block:
|
2022-06-03 12:22:17 +02:00
|
|
|
- name: Flush handlers
|
|
|
|
ansible.builtin.meta: flush_handlers
|
2021-06-02 12:05:51 +02:00
|
|
|
|
|
|
|
- name: Print current configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.slurp:
|
2021-06-02 12:05:51 +02:00
|
|
|
src: /etc/ssh/sshd_config_custom
|
|
|
|
register: config
|
|
|
|
|
|
|
|
- name: Get stat of the configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.stat:
|
2021-06-02 12:05:51 +02:00
|
|
|
path: /etc/ssh/sshd_config_custom
|
|
|
|
register: config_stat
|
|
|
|
|
|
|
|
- name: Print second configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.slurp:
|
2021-06-02 12:05:51 +02:00
|
|
|
src: /etc/ssh/sshd_config_custom_second
|
|
|
|
register: config2
|
|
|
|
|
|
|
|
- name: Print the main configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.slurp:
|
2022-04-06 11:20:34 +02:00
|
|
|
src: /etc/ssh/sshd_config
|
2021-06-02 12:05:51 +02:00
|
|
|
register: config3
|
|
|
|
|
|
|
|
- name: Check content of first configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-06-02 12:05:51 +02:00
|
|
|
that:
|
|
|
|
- "'AcceptEnv LANG' in config.content | b64decode"
|
|
|
|
- "'Banner /etc/issue' in config.content | b64decode"
|
|
|
|
- "'Ciphers aes256-ctr' in config.content | b64decode"
|
|
|
|
- "'Compression no' in config.content | b64decode"
|
2022-05-02 16:33:46 +02:00
|
|
|
|
|
|
|
- name: Check Fedora/RHEL9+ defaults are present in the first configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2022-05-02 16:33:46 +02:00
|
|
|
that:
|
|
|
|
- "'Include /etc/ssh/sshd_config.d/*.conf' in config.content | b64decode"
|
|
|
|
- "'AuthorizedKeysFile .ssh/authorized_keys' in config.content | b64decode"
|
|
|
|
when:
|
|
|
|
- ansible_facts['os_family'] == 'RedHat'
|
2024-01-22 16:17:38 +01:00
|
|
|
- ansible_facts['distribution_major_version'] | int > 8
|
2022-05-02 16:33:46 +02:00
|
|
|
|
|
|
|
- name: Check RHEL7 and RHEL8 defaults are present in the first configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2022-05-02 16:33:46 +02:00
|
|
|
that:
|
|
|
|
- "'X11Forwarding yes' in config.content | b64decode"
|
|
|
|
- "'AuthorizedKeysFile .ssh/authorized_keys' in config.content | b64decode"
|
|
|
|
- "'UsePAM yes' in config.content | b64decode"
|
|
|
|
when:
|
|
|
|
- ansible_facts['os_family'] == 'RedHat'
|
2024-01-22 16:17:38 +01:00
|
|
|
- ansible_facts['distribution_major_version'] | int > 6
|
|
|
|
- ansible_facts['distribution_major_version'] | int < 9
|
2022-05-02 16:33:46 +02:00
|
|
|
|
|
|
|
- name: Check RHEL6 defaults are present in the first configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2022-05-02 16:33:46 +02:00
|
|
|
that:
|
|
|
|
- "'Protocol 2' in config.content | b64decode"
|
|
|
|
- "'UsePAM yes' in config.content | b64decode"
|
|
|
|
when:
|
|
|
|
- ansible_facts['os_family'] == 'RedHat'
|
|
|
|
- ansible_facts['distribution_major_version'] == '6'
|
|
|
|
|
|
|
|
- name: Check Debian defaults are present in the first configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2022-05-02 16:33:46 +02:00
|
|
|
that:
|
|
|
|
- "'PrintMotd no' in config.content | b64decode"
|
|
|
|
- "'UsePAM yes' in config.content | b64decode"
|
|
|
|
when:
|
|
|
|
- ansible_facts['os_family'] == 'Debian'
|
2024-01-22 16:17:38 +01:00
|
|
|
- ansible_facts['distribution_major_version'] | int < 22
|
2022-05-02 18:38:50 +02:00
|
|
|
|
2023-10-31 15:11:43 +01:00
|
|
|
- name: Check Ubuntu 20+ defaults are present in the first configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2022-05-02 18:38:50 +02:00
|
|
|
that:
|
|
|
|
- "'Include /etc/ssh/sshd_config.d/*.conf' in config3.content | b64decode"
|
|
|
|
- "'UsePAM yes' in config.content | b64decode"
|
2023-10-31 15:11:43 +01:00
|
|
|
when:
|
|
|
|
- ansible_facts['distribution'] == 'Ubuntu'
|
|
|
|
- ansible_facts['distribution_major_version'] | int >= 20
|
|
|
|
|
|
|
|
- name: Check Ubuntu 22+ defaults are present in the first configuration file
|
|
|
|
ansible.builtin.assert:
|
|
|
|
that:
|
|
|
|
- "'KbdInteractiveAuthentication no' in config.content | b64decode"
|
2022-05-02 18:38:50 +02:00
|
|
|
when:
|
|
|
|
- ansible_facts['distribution'] == 'Ubuntu'
|
2024-01-22 16:17:38 +01:00
|
|
|
- ansible_facts['distribution_major_version'] | int >= 22
|
2021-06-02 12:05:51 +02:00
|
|
|
|
|
|
|
- name: Check content of second configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-06-02 12:05:51 +02:00
|
|
|
that:
|
|
|
|
- "'Banner /etc/issue2' in config2.content | b64decode"
|
|
|
|
- "'Ciphers aes128-ctr' in config2.content | b64decode"
|
|
|
|
- "'HostKey' not in config2.content | b64decode"
|
|
|
|
- "'MaxStartups 100' in config2.content | b64decode"
|
|
|
|
- "'Compression no' not in config2.content | b64decode"
|
|
|
|
|
|
|
|
- name: Check content of the main configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-06-02 12:05:51 +02:00
|
|
|
that:
|
|
|
|
- "'Banner /etc/issue' in config3.content | b64decode"
|
|
|
|
- "'Ciphers aes192-ctr' in config3.content | b64decode"
|
|
|
|
- "'HostKey /tmp/ssh_host_ecdsa_key' in config3.content | b64decode"
|
|
|
|
- "'PasswordAuthentication no' in config3.content | b64decode"
|
|
|
|
- "'MaxStartups 100' not in config3.content | b64decode"
|
|
|
|
- "'Compression no' not in config3.content | b64decode"
|
|
|
|
|
2023-04-20 11:33:45 +02:00
|
|
|
- name: Check the main configuration file contains some default values for RHEL 9
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2022-04-06 16:07:31 +02:00
|
|
|
that:
|
|
|
|
- "'Include /etc/ssh/sshd_config.d/*.conf' in config3.content | b64decode"
|
|
|
|
- "'AuthorizedKeysFile .ssh/authorized_keys' in config3.content | b64decode"
|
2023-04-18 13:21:50 +02:00
|
|
|
- "'Subsystem sftp /usr/libexec/openssh/sftp-server' in config3.content | b64decode"
|
2022-04-06 16:07:31 +02:00
|
|
|
when:
|
|
|
|
- ansible_facts['os_family'] == 'RedHat'
|
2024-01-22 16:17:38 +01:00
|
|
|
- ansible_facts['distribution_major_version'] | int > 8
|
2023-04-20 11:33:45 +02:00
|
|
|
- ansible_facts['distribution'] != 'Fedora'
|
|
|
|
|
|
|
|
- name: Check the main configuration file contains some default values for Fedora
|
|
|
|
ansible.builtin.assert:
|
|
|
|
that:
|
|
|
|
- "'Include /etc/ssh/sshd_config.d/*.conf' in config3.content | b64decode"
|
|
|
|
- "'AuthorizedKeysFile .ssh/authorized_keys' in config3.content | b64decode"
|
|
|
|
- "'Subsystem sftp /usr/libexec/sftp-server' in config3.content | b64decode"
|
|
|
|
when:
|
|
|
|
- ansible_facts['os_family'] == 'RedHat'
|
2024-01-22 16:17:38 +01:00
|
|
|
- ansible_facts['distribution_major_version'] | int > 8
|
2023-04-20 11:33:45 +02:00
|
|
|
- ansible_facts['distribution'] == 'Fedora'
|
2022-04-06 16:07:31 +02:00
|
|
|
|
2021-06-02 12:05:51 +02:00
|
|
|
- name: Check the generated config has requested properties
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-06-02 12:05:51 +02:00
|
|
|
that:
|
|
|
|
- config_stat.stat.exists
|
|
|
|
- config_stat.stat.gr_name == 'nobody'
|
|
|
|
- config_stat.stat.pw_name == 'nobody'
|
|
|
|
- config_stat.stat.mode == '0660'
|
|
|
|
|
|
|
|
- name: "Restore configuration files"
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.include_tasks: tasks/restore.yml
|