--- # Lock-out signals only: the drop-in is in place, the daemon accepts the config and # runs, and the effective settings keep key authentication on and passwords and # root login off. The remaining hardening values restate the role's own defaults # and would only break on a policy change. - name: Verify hosts: all gather_facts: false tasks: - name: Stat the drop-in ansible.builtin.stat: path: /etc/ssh/sshd_config.d/99-ansible-hardening.conf register: sshd_verify_dropin_stat - name: The drop-in exists ansible.builtin.assert: that: sshd_verify_dropin_stat.stat.exists quiet: true - name: Test the configuration sshd will load ansible.builtin.command: sshd -t changed_when: false - name: Ask systemd about the ssh unit ansible.builtin.systemd: name: ssh register: sshd_verify_unit check_mode: true - name: The ssh unit is active ansible.builtin.assert: that: sshd_verify_unit.status.ActiveState == "active" quiet: true - name: Dump the effective configuration ansible.builtin.command: sshd -T register: sshd_verify_effective changed_when: false - name: Key login stays on, password and root login stay off ansible.builtin.assert: that: - sshd_verify_effective.stdout_lines is contains('pubkeyauthentication yes') - sshd_verify_effective.stdout_lines is contains('passwordauthentication no') - sshd_verify_effective.stdout_lines is contains('permitrootlogin no') quiet: true # The pve profile cannot be converged here (it is not an LXC), so render its drop-in # to a scratch file and let sshd parse it: proves sshd accepts the value and the # profile's algorithm lists, and what it resolves them to. - name: Verify the pve profile parses hosts: all gather_facts: false vars: sshd_profile: pve tasks: - name: Load the role defaults for the pve profile ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml public: true - name: Create a scratch file for the pve drop-in ansible.builtin.tempfile: suffix: .conf register: sshd_verify_pve_file - name: Render the pve drop-in ansible.builtin.copy: content: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/99-ansible-hardening.conf.j2') }}" dest: "{{ sshd_verify_pve_file.path }}" mode: "0600" - name: Dump the effective pve configuration ansible.builtin.command: sshd -T -f {{ sshd_verify_pve_file.path }} register: sshd_verify_pve_effective changed_when: false - name: Root logs in with keys only, passwords stay off ansible.builtin.assert: that: # sshd -T prints the first alias in its table for prohibit-password: without-password. - sshd_verify_pve_effective.stdout_lines is contains('permitrootlogin without-password') - sshd_verify_pve_effective.stdout_lines is contains('passwordauthentication no') - sshd_verify_pve_effective.stdout_lines is contains('pubkeyauthentication yes') quiet: true - name: Remove the scratch file ansible.builtin.file: path: "{{ sshd_verify_pve_file.path }}" state: absent