--- # The production profile: Ubuntu 24.04 LXCs. Only the profile name and the user are supplied. - name: Converge the role with a valid profile hosts: all vars: sshd_profile: ubuntu_lxc ansible_user: root roles: - role: mortis.kamino.sshd # A play without sshd_profile: the role must refuse it and leave the config # converged above untouched. The block proves the refusal (not its wording); the play # still passes, so the idempotence run stays clean. - name: Converge the role without a profile hosts: all gather_facts: false tasks: - name: Missing sshd_profile is rejected block: - name: Apply the role without sshd_profile ansible.builtin.include_role: name: mortis.kamino.sshd - name: Fail when the missing profile was accepted ansible.builtin.fail: msg: the sshd role accepted a run without sshd_profile rescue: - name: The role refused the run ansible.builtin.debug: msg: Rejected as expected # The plays below only load the role's defaults and run validate.yml, which changes nothing # on the container. They cover the profiles the converge above cannot run and the guards. - name: Every profile passes validation and keeps its root login policy hosts: all gather_facts: false tasks: - name: Validate each profile ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml public: true vars: sshd_profile: "{{ item }}" loop: [pve, ubuntu_lxc, ubuntu_vm] - name: Assert the profile data ansible.builtin.assert: that: - sshd_profiles.pve.PermitRootLogin == "prohibit-password" - sshd_profiles.ubuntu_lxc.PermitRootLogin == "no" - sshd_profiles.ubuntu_vm.PermitRootLogin == "no" quiet: true # Proxmox cluster SSH needs RSA with SHA-2, never SHA-1 ssh-rsa; the others stay ed25519 only. - name: Assert the key algorithms per profile ansible.builtin.assert: that: - sshd_profiles.pve.HostKeyAlgorithms == "ssh-ed25519,rsa-sha2-512,rsa-sha2-256" - sshd_profiles.pve.PubkeyAcceptedAlgorithms == "ssh-ed25519,rsa-sha2-512,rsa-sha2-256" - sshd_settings_defaults.HostKeyAlgorithms == "ssh-ed25519" - sshd_settings_defaults.PubkeyAcceptedAlgorithms == "ssh-ed25519" - "'HostKeyAlgorithms' not in sshd_profiles.ubuntu_lxc" - "'HostKeyAlgorithms' not in sshd_profiles.ubuntu_vm" quiet: true # Play vars outrank the role defaults, so the override reaches the merged settings. - name: A consumer override wins and keeps the rest hosts: all gather_facts: false vars: sshd_profile: ubuntu_vm sshd_settings: MaxAuthTries: "5" tasks: - name: Load the role defaults ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml public: true - name: Assert the override ansible.builtin.assert: that: - sshd_settings_effective.MaxAuthTries == "5" - sshd_settings_effective.PermitRootLogin == "no" - sshd_settings_effective.PubkeyAuthentication == "yes" quiet: true - name: The lock-out guard refuses a lock-out hosts: all gather_facts: false vars: sshd_profile: ubuntu_lxc sshd_settings: PubkeyAuthentication: "no" tasks: - name: Apply a lock-out configuration block: - name: Include the validation ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml - name: Fail when the lock-out was accepted ansible.builtin.fail: msg: the sshd role accepted a lock-out configuration rescue: - name: The lock-out guard stopped the play ansible.builtin.assert: that: "'lock you out' in ansible_failed_result.msg | default('')" quiet: true - name: The Proxmox guard accepts every value that allows root key login hosts: all gather_facts: false tasks: - name: Validate the pve profile with each accepted value ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml vars: sshd_profile: pve sshd_settings: PermitRootLogin: "{{ item }}" loop: [prohibit-password, without-password, "yes", Prohibit-Password] - name: The Proxmox guard refuses a pve profile without root login hosts: all gather_facts: false vars: sshd_profile: pve sshd_settings: PermitRootLogin: "no" tasks: - name: Apply a pve configuration without root login block: - name: Include the validation ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml - name: Fail when the Proxmox guard was bypassed ansible.builtin.fail: msg: the sshd role accepted a pve profile without root login rescue: - name: The Proxmox guard stopped the play ansible.builtin.assert: that: "'needs root public-key login' in ansible_failed_result.msg | default('')" quiet: true - name: The Proxmox guard refuses forced-commands-only hosts: all gather_facts: false vars: sshd_profile: pve sshd_settings: PermitRootLogin: forced-commands-only tasks: - name: Apply a pve configuration with forced commands only block: - name: Include the validation ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml - name: Fail when the Proxmox guard was bypassed ansible.builtin.fail: msg: the sshd role accepted forced-commands-only for the pve profile rescue: - name: The Proxmox guard stopped the play ansible.builtin.assert: that: "'needs root public-key login' in ansible_failed_result.msg | default('')" quiet: true - name: The Proxmox guard refuses a pve profile without RSA keys hosts: all gather_facts: false vars: sshd_profile: pve sshd_settings: PubkeyAcceptedAlgorithms: "ssh-ed25519" tasks: - name: Apply a pve configuration without RSA keys block: - name: Include the validation ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml - name: Fail when the RSA guard was bypassed ansible.builtin.fail: msg: the sshd role accepted a pve profile without RSA keys rescue: - name: The RSA guard stopped the play ansible.builtin.assert: that: "'must both include rsa-sha2-512' in ansible_failed_result.msg | default('')" quiet: true - name: The guard refuses SHA-1 ssh-rsa hosts: all gather_facts: false vars: sshd_profile: pve sshd_settings: HostKeyAlgorithms: "ssh-ed25519, rsa-sha2-512, ssh-rsa" tasks: - name: Apply a configuration that allows ssh-rsa block: - name: Include the validation ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml - name: Fail when the ssh-rsa guard was bypassed ansible.builtin.fail: msg: the sshd role accepted ssh-rsa rescue: - name: The ssh-rsa guard stopped the play ansible.builtin.assert: that: "'must not list ssh-rsa' in ansible_failed_result.msg | default('')" quiet: true - name: An unknown profile is refused hosts: all gather_facts: false vars: sshd_profile: not_a_profile tasks: - name: Apply the role with an unknown profile block: - name: Include the validation ansible.builtin.include_role: name: mortis.kamino.sshd tasks_from: validate.yml - name: Fail when the unknown profile was accepted ansible.builtin.fail: msg: the sshd role accepted an unknown profile rescue: - name: The profile check stopped the play ansible.builtin.assert: that: "'is invalid' in ansible_failed_result.msg | default('')" quiet: true