--- - name: Verify hosts: all gather_facts: false tasks: - name: Read the installed packages ansible.builtin.package_facts: manager: auto - name: The essential packages are installed and the removed one is gone ansible.builtin.assert: that: - "'ufw' in ansible_facts.packages" - "'fail2ban' in ansible_facts.packages" - "'unattended-upgrades' in ansible_facts.packages" - "'tmux' in ansible_facts.packages" - "'telnet' not in ansible_facts.packages" quiet: true - name: Read the journald drop-in ansible.builtin.slurp: src: /etc/systemd/journald.conf.d/50-system-baseline.conf register: system_baseline_verify_journald - name: The journal size is capped ansible.builtin.assert: that: - system_baseline_verify_journald.content | b64decode is search('(?m)^SystemMaxUse=200M$') quiet: true - name: Read the tmux auto-start snippet ansible.builtin.slurp: src: /etc/profile.d/tmux-autostart.sh register: system_baseline_verify_tmux - name: Stat the tmux auto-start snippet ansible.builtin.stat: path: /etc/profile.d/tmux-autostart.sh register: system_baseline_verify_tmux_stat - name: The snippet is root-owned, attaches to the session, and quotes the window command as one argument ansible.builtin.assert: that: - system_baseline_verify_tmux_stat.stat.pw_name == 'root' - system_baseline_verify_tmux_stat.stat.mode == '0644' - system_baseline_verify_tmux.content | b64decode is search('tmux attach-session -t =molecule') - system_baseline_verify_tmux.content | b64decode is search("-n logs 'echo '\"'\"'a b'\"'\"'; true'") quiet: true - name: The snippet is valid shell ansible.builtin.command: /bin/sh -n /etc/profile.d/tmux-autostart.sh changed_when: false - name: Read the apt configuration the role writes ansible.builtin.slurp: src: "{{ item }}" loop: - /etc/apt/apt.conf.d/20auto-upgrades - /etc/apt/apt.conf.d/50unattended-upgrades register: system_baseline_verify_apt - name: Automatic upgrades are on and the reboot setting is applied ansible.builtin.assert: that: - system_baseline_verify_apt.results[0].content | b64decode is search('APT::Periodic::Unattended-Upgrade "1";') - system_baseline_verify_apt.results[1].content | b64decode is search('Unattended-Upgrade::Automatic-Reboot "False";') - system_baseline_verify_apt.results[1].content | b64decode is search('Unattended-Upgrade::Automatic-Reboot-Time "05:00";') quiet: true - name: Read the generated locales ansible.builtin.command: locale -a register: system_baseline_verify_locales changed_when: false - name: The configured locale exists ansible.builtin.assert: that: system_baseline_verify_locales.stdout_lines | map('lower') | map('replace', '-', '') | select('match', 'en_us.utf8') | list | length > 0 quiet: true # The reboot entry point is a supported interface: with system_baseline_reboot_if_required # false it only sets system_baseline_reboot_required. Here and not in converge, because it # creates and removes the marker file, which the idempotence run would report as a change. # Reaching the assert after the marker exists shows the entry point did not reboot. - name: The reboot entry point only computes the fact when told not to reboot hosts: all gather_facts: true tasks: - name: Remove the reboot marker ansible.builtin.file: path: /var/run/reboot-required state: absent - name: Run the reboot entry point without a pending reboot ansible.builtin.include_role: name: mortis.kamino.system_baseline tasks_from: reboot vars: system_baseline_reboot_if_required: false - name: No reboot is required ansible.builtin.assert: that: - system_baseline_reboot_required is defined - not system_baseline_reboot_required quiet: true - name: Create the reboot marker ansible.builtin.copy: dest: /var/run/reboot-required content: "" mode: "0644" - name: Run the reboot entry point with a pending reboot ansible.builtin.include_role: name: mortis.kamino.system_baseline tasks_from: reboot vars: system_baseline_reboot_if_required: false - name: A reboot is required and the host did not reboot ansible.builtin.assert: that: - system_baseline_reboot_required quiet: true - name: Remove the reboot marker again ansible.builtin.file: path: /var/run/reboot-required state: absent # The snippet must do nothing without a terminal: SSH_CONNECTION is set, stdin is not a tty. It must reach the line after it # and leave no tmux server behind. - name: Source the snippet with an SSH connection and no terminal ansible.builtin.command: argv: [/bin/sh, -c, '. /etc/profile.d/tmux-autostart.sh; echo reached'] environment: SSH_CONNECTION: 192.0.2.1 50000 192.0.2.2 22 register: system_baseline_verify_snippet changed_when: false - name: It continued to the next line and started no tmux session ansible.builtin.assert: that: system_baseline_verify_snippet.stdout == 'reached' quiet: true - name: Ask tmux for sessions ansible.builtin.command: tmux list-sessions register: system_baseline_verify_sessions changed_when: false failed_when: system_baseline_verify_sessions.rc == 0 # The inputs reject a trailing newline (a YAML block scalar adds one): the check anchors with \Z, not $. - name: Run the validation with a session name that ends in a newline block: - name: Include the validation ansible.builtin.include_role: name: mortis.kamino.system_baseline tasks_from: validate vars: system_baseline_system_type: ubuntu_lxc system_baseline_tmux_autostart: true system_baseline_tmux_session: "main\n" - name: Unreachable when validation works ansible.builtin.fail: msg: a session name with a trailing newline was accepted rescue: - name: The validation refused it ansible.builtin.assert: that: "'system_baseline_tmux_session must be' in ansible_failed_result.msg | default('')" quiet: true - name: Run the validation with a window command that ends in a newline block: - name: Include the validation ansible.builtin.include_role: name: mortis.kamino.system_baseline tasks_from: validate vars: system_baseline_system_type: ubuntu_lxc system_baseline_tmux_autostart: true system_baseline_tmux_windows: - name: logs command: "journalctl -f\n" - name: Unreachable when validation works ansible.builtin.fail: msg: a window command with a trailing newline was accepted rescue: - name: The validation refused it ansible.builtin.assert: that: "'system_baseline_tmux_session must be' in ansible_failed_result.msg | default('')" quiet: true # The removal path runs last, because it deletes the snippet the checks above read. It lives here and not in converge: a # converge that installs and then removes the file would not be idempotent. - name: Run the tmux tasks with autostart off ansible.builtin.include_role: name: mortis.kamino.system_baseline tasks_from: tmux vars: system_baseline_system_type: ubuntu_lxc system_baseline_tmux_autostart: false - name: Stat the snippet again ansible.builtin.stat: path: /etc/profile.d/tmux-autostart.sh register: system_baseline_verify_removed - name: The snippet is gone ansible.builtin.assert: that: not system_baseline_verify_removed.stat.exists quiet: true # The pve origins pin, run on this Ubuntu host: Ubuntu's 50unattended-upgrades sets Allowed-Origins, so the merged # configuration showing only the pinned patterns proves the drop-in clears the earlier lists instead of appending to them. # The patterns match no Ubuntu origin, which only means nothing would be upgraded; the role's dry run still passes. Here # and not in converge, because switching profiles and back would not be idempotent. - name: The pve profile pins the unattended-upgrades origins hosts: all gather_facts: false tasks: - name: Stat the origins drop-in after the ubuntu_lxc converge ansible.builtin.stat: path: /etc/apt/apt.conf.d/90unattended-upgrades-origins register: system_baseline_verify_origins_before - name: The ubuntu_lxc profile pins nothing ansible.builtin.assert: that: not system_baseline_verify_origins_before.stat.exists quiet: true - name: Run the unattended-upgrades tasks with the pve profile ansible.builtin.include_role: name: mortis.kamino.system_baseline tasks_from: unattended_upgrades vars: system_baseline_system_type: pve - name: Dump the merged apt configuration ansible.builtin.command: apt-config dump register: system_baseline_verify_apt_config changed_when: false - name: Only the pinned Debian origins are allowed ansible.builtin.assert: that: - >- system_baseline_verify_apt_config.stdout_lines | select('match', 'Unattended-Upgrade::Origins-Pattern::\s') | list == ['Unattended-Upgrade::Origins-Pattern:: "origin=Debian,codename=${distro_codename},label=Debian";', 'Unattended-Upgrade::Origins-Pattern:: "origin=Debian,codename=${distro_codename},label=Debian-Security";', 'Unattended-Upgrade::Origins-Pattern:: "origin=Debian,codename=${distro_codename}-security,label=Debian-Security";'] - system_baseline_verify_apt_config.stdout_lines | select('match', 'Unattended-Upgrade::Allowed-Origins::\s') | list == [] quiet: true - name: Run the unattended-upgrades tasks with the ubuntu_lxc profile again ansible.builtin.include_role: name: mortis.kamino.system_baseline tasks_from: unattended_upgrades vars: system_baseline_system_type: ubuntu_lxc - name: Dump the merged apt configuration again ansible.builtin.command: apt-config dump register: system_baseline_verify_apt_config_restored changed_when: false - name: The drop-in is gone and the distribution origins are back ansible.builtin.assert: that: - system_baseline_verify_apt_config_restored.stdout_lines | select('match', 'Unattended-Upgrade::Allowed-Origins::\s') | list | length > 0 - system_baseline_verify_apt_config_restored.stdout is not search('origin=Debian') quiet: true