From 396797427018cb585b4f3685a708b3a26d4d34ce Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Wed, 29 Nov 2023 22:49:57 +0100 Subject: [PATCH] feat: add vagrant tests for later, add become: true to some tasks to not rely on ansible.cfg --- handlers/main.yml | 2 + molecule/default_vagrant/converge.yml | 7 +++ molecule/default_vagrant/molecule.yml | 35 ++++++++++++ molecule/default_vagrant/requirements.yml | 4 ++ molecule/default_vagrant/verify.yml | 55 +++++++++++++++++++ .../with_custom_config_vagrant/converge.yml | 7 +++ .../group_vars/all.yml | 33 +++++++++++ .../with_custom_config_vagrant/molecule.yml | 35 ++++++++++++ .../requirements.yml | 4 ++ .../with_custom_config_vagrant/verify.yml | 55 +++++++++++++++++++ tasks/configure.yml | 1 + tasks/install.yml | 1 + tasks/remove_existing.yml | 2 + 13 files changed, 241 insertions(+) create mode 100644 molecule/default_vagrant/converge.yml create mode 100644 molecule/default_vagrant/molecule.yml create mode 100644 molecule/default_vagrant/requirements.yml create mode 100644 molecule/default_vagrant/verify.yml create mode 100644 molecule/with_custom_config_vagrant/converge.yml create mode 100644 molecule/with_custom_config_vagrant/group_vars/all.yml create mode 100644 molecule/with_custom_config_vagrant/molecule.yml create mode 100644 molecule/with_custom_config_vagrant/requirements.yml create mode 100644 molecule/with_custom_config_vagrant/verify.yml diff --git a/handlers/main.yml b/handlers/main.yml index 5203b05..5d0f80f 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -3,6 +3,7 @@ - name: "Generate netplan configuration" ansible.builtin.command: netplan generate changed_when: false + become: true listen: "netplan-generate" - name: "Apply netplan configuration" @@ -10,3 +11,4 @@ listen: "netplan-apply" changed_when: false when: manage_netplan_apply + become: true diff --git a/molecule/default_vagrant/converge.yml b/molecule/default_vagrant/converge.yml new file mode 100644 index 0000000..74b1609 --- /dev/null +++ b/molecule/default_vagrant/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ednxzu.manage_netplan" + ansible.builtin.include_role: + name: "ednxzu.manage_netplan" diff --git a/molecule/default_vagrant/molecule.yml b/molecule/default_vagrant/molecule.yml new file mode 100644 index 0000000..2b02360 --- /dev/null +++ b/molecule/default_vagrant/molecule.yml @@ -0,0 +1,35 @@ +--- +dependency: + name: galaxy + options: + requirements-file: ./requirements.yml +driver: + name: vagrant + provider: + name: libvirt +platforms: + - name: instance + box: generic/${MOLECULE_TEST_OS} + cpus: 4 + memory: 4096 +provisioner: + name: ansible + config_options: + defaults: + remote_tmp: /tmp/.ansible +verifier: + name: ansible +scenario: + name: default_vagrant + test_sequence: + - dependency + - cleanup + - destroy + - syntax + - create + - prepare + - converge + - idempotence + - verify + - cleanup + - destroy diff --git a/molecule/default_vagrant/requirements.yml b/molecule/default_vagrant/requirements.yml new file mode 100644 index 0000000..ca250b7 --- /dev/null +++ b/molecule/default_vagrant/requirements.yml @@ -0,0 +1,4 @@ +--- +# requirements file for molecule +roles: + - name: ednxzu.manage_apt_packages diff --git a/molecule/default_vagrant/verify.yml b/molecule/default_vagrant/verify.yml new file mode 100644 index 0000000..5dbba4e --- /dev/null +++ b/molecule/default_vagrant/verify.yml @@ -0,0 +1,55 @@ +--- +- name: Verify + hosts: all + gather_facts: true + tasks: + - name: "Test: file /etc/hosts" + block: + - name: "Stat file /etc/hosts" + ansible.builtin.stat: + path: "/etc/hosts" + register: stat_etc_hosts + + - name: "Verify file /etc/hosts" + vars: + etc_hosts_group: + ubuntu: "adm" + debian: "root" + ansible.builtin.assert: + that: + - stat_etc_hosts.stat.exists + - stat_etc_hosts.stat.isreg + - stat_etc_hosts.stat.pw_name == 'root' + - stat_etc_hosts.stat.gr_name == etc_hosts_group[(ansible_distribution|lower)] + + - name: "Test: directory /etc/netplan" + block: + - name: "Stat directory /etc/netplan" + ansible.builtin.stat: + path: "/etc/netplan" + register: stat_etc_netplan + + - name: "Stat file /etc/netplan/ansible-config.yaml" + ansible.builtin.stat: + path: "/etc/netplan/ansible-config.yaml" + register: stat_etc_netplan_ansible_config_yml + + - name: "Slurp file /etc/netplan/ansible-config.yaml" + ansible.builtin.slurp: + src: "/etc/netplan/ansible-config.yaml" + register: slurp_etc_netplan_ansible_config_yml + + - name: "Verify directory /etc/netplan" + ansible.builtin.assert: + that: + - stat_etc_netplan.stat.exists + - stat_etc_netplan.stat.isdir + - stat_etc_netplan.stat.pw_name == 'root' + - stat_etc_netplan.stat.gr_name == 'root' + - stat_etc_netplan.stat.mode == '0755' + - stat_etc_netplan_ansible_config_yml.stat.exists + - stat_etc_netplan_ansible_config_yml.stat.isreg + - stat_etc_netplan_ansible_config_yml.stat.pw_name == 'root' + - stat_etc_netplan_ansible_config_yml.stat.gr_name == 'root' + - stat_etc_netplan_ansible_config_yml.stat.mode == '0644' + - slurp_etc_netplan_ansible_config_yml.content != '' diff --git a/molecule/with_custom_config_vagrant/converge.yml b/molecule/with_custom_config_vagrant/converge.yml new file mode 100644 index 0000000..74b1609 --- /dev/null +++ b/molecule/with_custom_config_vagrant/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ednxzu.manage_netplan" + ansible.builtin.include_role: + name: "ednxzu.manage_netplan" diff --git a/molecule/with_custom_config_vagrant/group_vars/all.yml b/molecule/with_custom_config_vagrant/group_vars/all.yml new file mode 100644 index 0000000..59074b4 --- /dev/null +++ b/molecule/with_custom_config_vagrant/group_vars/all.yml @@ -0,0 +1,33 @@ +--- +manage_netplan_config_file: /tmp/ansible-config.yaml # this MUST be .yaml, .yml will not be picked up by netplan +manage_netplan_renderer: networkd # supported value is 'NetworkManager' or 'networkd' +manage_netplan_remove_existing: false +manage_netplan_search_domain: example.org +manage_netplan_install: true +manage_netplan_apply: false +manage_netplan_configuration: + network: + version: 2 + ethernets: + eth1: + dhcp4: false + link-local: [] + set-name: eth1 + eth2: + dhcp4: true + mtu: 1500 + link-local: [] + set-name: eth2 + vlans: + vlan20: + id: 20 + link: eth1 + mtu: 1500 + dhcp4: false + link-local: [] + vlan40: + id: 40 + link: eth1 + mtu: 1500 + dhcp4: false + link-local: [] diff --git a/molecule/with_custom_config_vagrant/molecule.yml b/molecule/with_custom_config_vagrant/molecule.yml new file mode 100644 index 0000000..890cdd0 --- /dev/null +++ b/molecule/with_custom_config_vagrant/molecule.yml @@ -0,0 +1,35 @@ +--- +dependency: + name: galaxy + options: + requirements-file: ./requirements.yml +driver: + name: vagrant + provider: + name: libvirt +platforms: + - name: instance + box: generic/${MOLECULE_TEST_OS} + cpus: 4 + memory: 4096 +provisioner: + name: ansible + config_options: + defaults: + remote_tmp: /tmp/.ansible +verifier: + name: ansible +scenario: + name: with_custom_config_vagrant + test_sequence: + - dependency + - cleanup + - destroy + - syntax + - create + - prepare + - converge + - idempotence + - verify + - cleanup + - destroy diff --git a/molecule/with_custom_config_vagrant/requirements.yml b/molecule/with_custom_config_vagrant/requirements.yml new file mode 100644 index 0000000..ca250b7 --- /dev/null +++ b/molecule/with_custom_config_vagrant/requirements.yml @@ -0,0 +1,4 @@ +--- +# requirements file for molecule +roles: + - name: ednxzu.manage_apt_packages diff --git a/molecule/with_custom_config_vagrant/verify.yml b/molecule/with_custom_config_vagrant/verify.yml new file mode 100644 index 0000000..f267f5a --- /dev/null +++ b/molecule/with_custom_config_vagrant/verify.yml @@ -0,0 +1,55 @@ +--- +- name: Verify + hosts: all + gather_facts: true + tasks: + - name: "Test: file /etc/hosts" + block: + - name: "Stat file /etc/hosts" + ansible.builtin.stat: + path: "/etc/hosts" + register: stat_etc_hosts + + - name: "Verify file /etc/hosts" + vars: + etc_hosts_group: + ubuntu: "adm" + debian: "root" + ansible.builtin.assert: + that: + - stat_etc_hosts.stat.exists + - stat_etc_hosts.stat.isreg + - stat_etc_hosts.stat.pw_name == 'root' + - stat_etc_hosts.stat.gr_name == etc_hosts_group[(ansible_distribution|lower)] + + - name: "Test: directory /tmp" + block: + - name: "Stat directory /etc/netplan" + ansible.builtin.stat: + path: "/etc/netplan" + register: stat_etc_netplan + + - name: "Stat file /tmp/ansible-config.yaml" + ansible.builtin.stat: + path: "/tmp/ansible-config.yaml" + register: stat_tmp_ansible_config_yml + + - name: "Slurp file /tmp/ansible-config.yaml" + ansible.builtin.slurp: + src: "/tmp/ansible-config.yaml" + register: slurp_tmp_ansible_config_yml + + - name: "Verify directory /tmp/netplan" + ansible.builtin.assert: + that: + - stat_etc_netplan.stat.exists + - stat_etc_netplan.stat.isdir + - stat_etc_netplan.stat.pw_name == 'root' + - stat_etc_netplan.stat.gr_name == 'root' + - stat_etc_netplan.stat.mode == '0755' + - stat_tmp_ansible_config_yml.stat.exists + - stat_tmp_ansible_config_yml.stat.isreg + - stat_tmp_ansible_config_yml.stat.pw_name == 'root' + - stat_tmp_ansible_config_yml.stat.gr_name == 'root' + - stat_tmp_ansible_config_yml.stat.mode == '0644' + - slurp_tmp_ansible_config_yml.content != '' diff --git a/tasks/configure.yml b/tasks/configure.yml index 604c999..b28ea9c 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -9,6 +9,7 @@ group: root backup: true when: manage_netplan_configuration != [] + become: true notify: - "netplan-generate" - "netplan-apply" diff --git a/tasks/install.yml b/tasks/install.yml index 8b0be68..cee8e66 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -20,3 +20,4 @@ owner: "root" group: "root" mode: '0755' + become: true \ No newline at end of file diff --git a/tasks/remove_existing.yml b/tasks/remove_existing.yml index cb6da07..46d87ce 100644 --- a/tasks/remove_existing.yml +++ b/tasks/remove_existing.yml @@ -5,6 +5,7 @@ paths: /etc/netplan patterns: "*.yml,*.yaml" register: _netplan_configs + become: true - name: "Removing existing configurations" ansible.builtin.file: @@ -13,3 +14,4 @@ loop: "{{ _netplan_configs['files'] }}" when: item['path'] != manage_netplan_config_file and manage_netplan_configuration != [] + become: true