feat!(molecule): switch from docker to vagrant to test system related roles like firewall

This commit is contained in:
NaeiKinDus 2023-12-25 00:00:00 +00:00
parent 639b01c351
commit d25d077253
Signed by: WoodSmellParticle
GPG key ID: 8E52ADFF7CA8AE56
8 changed files with 123 additions and 113 deletions

View file

@ -3,92 +3,45 @@
- name: Create
hosts: localhost
gather_facts: false
vars:
molecule_inventory:
all:
hosts: {}
molecule: {}
connection: local
tasks:
- name: Create a container
community.docker.docker_container:
name: "{{ item.name }}"
image: "{{ item.image }}"
detach: true
state: started
log_driver: json-file
cgroupns_mode: private
cgroup_parent: docker.slice
mounts:
- target: /run
type: tmpfs
- target: /run/lock
type: tmpfs
- target: /tmp:exec
type: tmpfs
register: result
loop: "{{ molecule_yml.platforms }}"
- name: Print some info
- name: Create instances
vagrant:
instances: "{{ molecule_yml.platforms }}"
default_box: "{{ molecule_yml.driver.default_box | default('debian/bookworm64') }}"
provider_name: "{{ molecule_yml.driver.provider.name | default(omit, true) }}"
provision: "{{ molecule_yml.driver.provision | default(omit) }}"
cachier: "{{ molecule_yml.driver.cachier | default(omit) }}"
parallel: "{{ molecule_yml.driver.parallel | default(omit) }}"
state: up
register: server
- name: VMs info
ansible.builtin.debug:
msg: "{{ result.results }}"
msg: "{{ server.results }}"
- name: Create molecule instances configuration
when: server is changed # noqa no-handler
block:
- name: Populate instance config dict
ansible.builtin.set_fact:
instance_conf_dict:
{
"instance": "{{ item.Host }}",
"address": "{{ item.HostName }}",
"user": "{{ item.User }}",
"port": "{{ item.Port }}",
"identity_file": "{{ item.IdentityFile }}",
}
loop: "{{ server.results }}"
loop_control:
label: "{{ item.Host }}"
register: instance_config_dict
- name: Fail if container is not running
when: >
item.container.State.ExitCode != 0 or
not item.container.State.Running
ansible.builtin.include_tasks:
file: tasks/create-fail.yml
loop: "{{ result.results }}"
loop_control:
label: "{{ item.container.Name }}"
- name: Convert instance config dict to a list
ansible.builtin.set_fact:
instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
- name: Add container to molecule_inventory
vars:
inventory_partial_yaml: |
all:
children:
molecule:
hosts:
"{{ item.name }}":
ansible_connection: community.docker.docker
custom_base_user_account: root
custom_common:
apt:
deb822_format: true
ansible.builtin.set_fact:
molecule_inventory: >
{{ molecule_inventory | combine(inventory_partial_yaml | from_yaml) }}
loop: "{{ molecule_yml.platforms }}"
loop_control:
label: "{{ item.name }}"
- name: Dump molecule_inventory
ansible.builtin.copy:
content: |
{{ molecule_inventory | to_yaml }}
dest: "{{ molecule_ephemeral_directory }}/inventory/molecule_inventory.yml"
mode: 0600
- name: Force inventory refresh
ansible.builtin.meta: refresh_inventory
- name: Fail if molecule group is missing
ansible.builtin.assert:
that: "'molecule' in groups"
fail_msg: |
molecule group was not found inside inventory groups: {{ groups }}
run_once: true # noqa: run-once[task]
# we want to avoid errors like "Failed to create temporary directory"
- name: Validate that inventory was refreshed
hosts: molecule
gather_facts: false
tasks:
- name: Check uname
ansible.builtin.raw: uname -a
register: result
changed_when: false
- name: Display uname info
ansible.builtin.debug:
msg: "{{ result.stdout }}"
- name: Dump instance config
ansible.builtin.copy:
content: "{{ instance_conf | to_json | from_json | to_yaml }}"
dest: "{{ molecule_instance_config }}"
mode: "0600"