--- # The rendered file, the backup of the legacy body and the single reload. The last play is the # role's lock-out protection: a failing validation must put the previous file back. It # changes the container, so it lives here and not in converge.yml, where the idempotence # run would see it change twice. It works on its own copy of the file. - name: Verify the converged layout hosts: all gather_facts: false tasks: - name: Read the interfaces file, its backup and the reload marker ansible.builtin.slurp: src: "{{ item }}" loop: - /etc/network/interfaces - /etc/network/interfaces.bak - /var/tmp/network-interfaces-reload.log register: network_interfaces_verify_files - name: The file holds the loopback, the manual interface and both bridges ansible.builtin.assert: that: - lines is contains('iface lo inet loopback') - lines is contains('iface eno1 inet manual') - lines is contains('auto vmbr0') - lines is contains('iface vmbr0 inet static') - lines is contains(' address 10.0.0.2') - lines is contains(' netmask 255.255.255.0') - lines is contains(' gateway 10.0.0.1') - lines is contains(' bridge_ports eno1') - lines is contains(' bridge_stp off') - lines is contains(' bridge_vlan_aware yes') - lines is contains(' bridge_vids 2-4094') - lines is contains('auto vmbr1') - lines is contains('iface vmbr1 inet manual') - lines is contains(' bridge_ports none') - lines is contains(' bridge_stp on') - lines is contains(' bridge_fd 5') - lines | select('search', 'bridge_fd') | list | length == 1 - lines | select('search', 'bridge_vlan_aware') | list | length == 1 - "'legacy seed' not in content" quiet: true vars: content: "{{ network_interfaces_verify_files.results[0].content | b64decode }}" lines: "{{ content.split('\n') }}" - name: The backup is the legacy body and the reload ran once ansible.builtin.assert: that: - "'# legacy seed' in (network_interfaces_verify_files.results[1].content | b64decode)" - "'vmbr0' not in (network_interfaces_verify_files.results[1].content | b64decode)" - (network_interfaces_verify_files.results[2].content | b64decode).split('\n') | select | list | length == 1 quiet: true - name: A failing validation restores the previous file hosts: all gather_facts: false vars: network_interfaces_file: /etc/network/interfaces.rollback network_interfaces_backup: /etc/network/interfaces.rollback.bak network_interfaces_validate_command: /bin/false # A handler notified by the write is still pending when the block is rescued, and it # runs at the end of the play, so it must not touch the reload marker. network_interfaces_apply_command: /bin/true network_interfaces_layout: manual: [eno1] bridges: - name: vmbr9 tasks: - name: Read the file before ansible.builtin.slurp: src: /etc/network/interfaces.rollback register: network_interfaces_verify_before - name: Apply the role with a validation that fails block: - name: Include the role ansible.builtin.include_role: name: mortis.kamino.network_interfaces - name: Fail when the invalid configuration was accepted ansible.builtin.fail: msg: MOLECULE-ACCEPTED rescue: - name: The role reverted and refused ansible.builtin.assert: that: "'Previous config restored' in ansible_failed_result.msg | default('')" quiet: true - name: Read the file and the backup after ansible.builtin.slurp: src: "{{ item }}" loop: - /etc/network/interfaces.rollback - /etc/network/interfaces.rollback.bak register: network_interfaces_verify_after - name: The previous file is back and the bad one is gone ansible.builtin.assert: that: - network_interfaces_verify_after.results[0].content == network_interfaces_verify_before.content - network_interfaces_verify_after.results[1].content == network_interfaces_verify_before.content - "'vmbr9' not in (network_interfaces_verify_after.results[0].content | b64decode)" quiet: true