Skip to content

Commit

Permalink
Update Use of Molecule (ansible-collections#1303)
Browse files Browse the repository at this point in the history
  • Loading branch information
eb4x authored Jun 30, 2024
1 parent 54e8174 commit 28c4921
Show file tree
Hide file tree
Showing 23 changed files with 220 additions and 256 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ jobs:
python -m pip install --upgrade pip
pip install -r molecule/requirements.txt
- name: Install ansible.netcommon collection
run: ansible-galaxy collection install ansible.netcommon

- name: Build the collection
run: |
# Pin versions to speed up CI
Expand Down
4 changes: 4 additions & 0 deletions molecule/default/collections.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
collections:
- name: community.docker
version: ">=3.0.0"
47 changes: 47 additions & 0 deletions molecule/default/create.docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
- name: Create
hosts: localhost
connection: local

gather_facts: false

pre_tasks:
- name: Create network
community.docker.docker_network:
name: zabbix

tasks:
- name: Create OS container(s)
community.docker.docker_container:
state: started
recreate: true
name: "{{ item.name }}"
image: "{{ item.image }}"
privileged: "{{ item.privileged | default(omit) }}"
command: "{{ item.command | default(omit) }}"
networks: "{{ item.networks | default(omit) }}"
volumes: "{{ item.volumes | default(omit) }}"
cgroupns_mode: "{{ item.cgroupns_mode | default(omit) }}"
loop: "{{ molecule_yml.platforms }}"

- name: Create database container(s)
community.docker.docker_container:
name: "{{ item.name }}-db"
image: "{{ _container[_db_type].image }}"
state: started
recreate: true
networks:
- name: zabbix
env: "{{ _container[_db_type].env }}"
loop: "{{ molecule_yml.platforms }}"
when: item.groups | intersect(_database_groups)
vars:
_database_groups: ['mysql', 'pgsql']
_db_type: "{{ item.groups | intersect(_database_groups) | first }}"
_container:
mysql:
image: "mysql:8.0"
env: { MYSQL_ROOT_PASSWORD: changeme }
pgsql:
image: "postgres:13"
env: { POSTGRES_PASSWORD: changeme }
27 changes: 27 additions & 0 deletions molecule/default/destroy.docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
- name: Destroy
hosts: localhost
connection: local

gather_facts: false

tasks:
- name: Destroy OS container(s)
community.docker.docker_container:
name: "{{ item.name }}"
state: absent
force_kill: "{{ item.force_kill | default(True) }}"
loop: "{{ molecule_yml.platforms }}"

- name: Destroy database container(s)
community.docker.docker_container:
name: "{{ item.name }}-db"
state: absent
force_kill: true
loop: "{{ molecule_yml.platforms }}"

post_tasks:
- name: Destroy network
community.docker.docker_network:
name: zabbix
state: absent
18 changes: 6 additions & 12 deletions molecule/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# Install CI dependencies for the Zabbix Roles
#ansible==8.0.0 # commented out to avoid installing collections in site-packages/ansible_collections
ansible-compat==3.0.0
ansible-core==2.15.11
docker==6.1.3
molecule<5
molecule-docker @ git+https://github.com/ansible-community/molecule-docker@main
netaddr==1.2.1
pytest==7.2.1
pytest-testinfra==7.0.0
ipaddr==2.2.0
ipaddress==1.0.23
requests==2.31.0
ansible-core<2.17
docker
molecule>=6,<24.7.0
netaddr>=1.2.1
pytest<8 # newer versions require python>=3.8
pytest-testinfra<9 # newer versions require python>=3.9
4 changes: 4 additions & 0 deletions molecule/zabbix_agent_tests/common/collections.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
collections:
- name: community.docker
version: ">=3.0.0"
17 changes: 14 additions & 3 deletions molecule/zabbix_agent_tests/common/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
---
dependency:
name: galaxy
enabled: true
options:
requirements-file: common/collections.yml

driver:
name: docker
name: default

platforms:
- name: zabbix-agent-${MY_MOLECULE_CONTAINER:-centos}
Expand All @@ -20,15 +26,18 @@ platforms:
provisioner:
name: ansible
playbooks:
create: ../../common/playbooks/create.yml
prepare: ../../common/playbooks/prepare.yml
converge: ../../common/playbooks/converge.yml
destroy: ../../common/playbooks/destroy.yml
env:
ANSIBLE_REMOTE_TMP: /tmp/
ANSIBLE_COLLECTIONS_PATHS: $HOME/.ansible/collections/ansible_collections/community/zabbix
ANSIBLE_ROLES_PATH: $HOME/.ansible/collections/ansible_collections/community/zabbix/roles
# https://github.com/ansible/molecule/issues/4015#issuecomment-1680859724
ANSIBLE_ROLES_PATH: ../../../../roles
inventory:
group_vars:
all:
ansible_connection: community.docker.docker
zabbix_agent_server: 192.168.3.33
zabbix_agent_serveractive: 192.168.3.33
zabbix_agent_listenip: 0.0.0.0
Expand All @@ -42,6 +51,7 @@ provisioner:
zabbix_agent_version: 6.4
v60:
zabbix_agent_version: 6.0

scenario:
test_sequence:
- dependency
Expand All @@ -56,5 +66,6 @@ scenario:
- verify
- cleanup
- destroy

verifier:
name: testinfra
25 changes: 25 additions & 0 deletions molecule/zabbix_agent_tests/common/playbooks/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
- name: Create
hosts: localhost
connection: local

gather_facts: false

pre_tasks:
- name: Create network
community.docker.docker_network:
name: zabbix

tasks:
- name: "Create OS container"
community.docker.docker_container:
state: started
recreate: true
name: "{{ item.name }}"
image: "{{ item.image }}"
privileged: "{{ item.privileged | default(omit) }}"
command: "{{ item.command | default(omit) }}"
networks: "{{ item.networks | default(omit) }}"
volumes: "{{ item.volumes | default(omit) }}"
cgroupns_mode: "{{ item.cgroupns_mode | default(omit) }}"
loop: "{{ molecule_yml.platforms }}"
20 changes: 20 additions & 0 deletions molecule/zabbix_agent_tests/common/playbooks/destroy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: Destroy
hosts: localhost
connection: local

gather_facts: false

tasks:
- name: Destroy OS container(s)
community.docker.docker_container:
name: "{{ item.name }}"
state: absent
force_kill: "{{ item.force_kill | default(True) }}"
loop: "{{ molecule_yml.platforms }}"

post_tasks:
- name: Destroy network
community.docker.docker_network:
name: zabbix
state: absent
19 changes: 15 additions & 4 deletions molecule/zabbix_javagateway/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
---
dependency:
name: galaxy
enabled: true
options:
requirements-file: molecule/default/collections.yml

driver:
name: docker
name: default

platforms:
- name: zabbix-server-${MY_MOLECULE_VERSION:-v70}-${MY_MOLECULE_DATABASE:-mysql}-${MY_MOLECULE_CONTAINER:-rockylinux8}
- name: zabbix-jgw-${MY_MOLECULE_VERSION:-v70}-${MY_MOLECULE_CONTAINER:-rockylinux8}
image: geerlingguy/docker-${MY_MOLECULE_IMAGE:-rockylinux8}-ansible:latest
privileged: true
pre_build_image: true
Expand All @@ -15,14 +21,19 @@ platforms:
cgroupns_mode: host
groups:
- ${MY_MOLECULE_VERSION:-v70}

provisioner:
name: ansible
playbooks:
create: ../default/create.docker.yml
destroy: ../default/destroy.docker.yml
env:
ANSIBLE_COLLECTIONS_PATHS: $HOME/.ansible/collections/ansible_collections/community/zabbix
ANSIBLE_ROLES_PATH: $HOME/.ansible/collections/ansible_collections/community/zabbix/roles
# https://github.com/ansible/molecule/issues/4015#issuecomment-1680859724
ANSIBLE_ROLES_PATH: ../../roles
inventory:
group_vars:
all:
ansible_connection: community.docker.docker
zabbix_repo_apt_priority: 1
zabbix_repo_yum_gpg_check: 1
v70:
Expand Down
1 change: 1 addition & 0 deletions molecule/zabbix_javagateway/prepare.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
- name: Prepare
hosts: all

pre_tasks:
- name: "Installing packages on CentOS"
ansible.builtin.yum:
Expand Down
23 changes: 0 additions & 23 deletions molecule/zabbix_proxy/destroy.yml

This file was deleted.

17 changes: 14 additions & 3 deletions molecule/zabbix_proxy/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
---
dependency:
name: galaxy
enabled: true
options:
requirements-file: molecule/default/collections.yml

driver:
name: docker
name: default

platforms:
- name: zabbix-proxy-${MY_MOLECULE_VERSION:-v70}-${MY_MOLECULE_DATABASE:-mysql}-${MY_MOLECULE_CONTAINER:-rockylinux8}
image: geerlingguy/docker-${MY_MOLECULE_IMAGE:-rockylinux8}-ansible:latest
Expand All @@ -18,12 +25,16 @@ platforms:

provisioner:
name: ansible
playbooks:
create: ../default/create.docker.yml
destroy: ../default/destroy.docker.yml
env:
ANSIBLE_COLLECTIONS_PATHS: $HOME/.ansible/collections/ansible_collections/community/zabbix
ANSIBLE_ROLES_PATH: $HOME/.ansible/collections/ansible_collections/community/zabbix/roles
# https://github.com/ansible/molecule/issues/4015#issuecomment-1680859724
ANSIBLE_ROLES_PATH: ../../roles
inventory:
group_vars:
all:
ansible_connection: community.docker.docker
zabbix_repo_apt_priority: 1
zabbix_repo_yum_gpg_check: 1
v70:
Expand Down
43 changes: 1 addition & 42 deletions molecule/zabbix_proxy/prepare.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,8 @@
---
- name: Prepare
hosts: localhost
connection: local
pre_tasks:
- name: "Create MySQL Container"
docker_container:
name: "{{ item.name }}-db"
image: mysql:8.0
state: started
recreate: true
networks:
- name: zabbix
env:
MYSQL_ROOT_PASSWORD: changeme
no_log: true
with_items: "{{ molecule_yml.platforms }}"
when:
- '"mysql" in item.groups'

- name: "Create postgresql Container"
docker_container:
name: "{{ item.name }}-db"
image: postgres:13
state: started
recreate: true
networks:
- name: zabbix
env:
POSTGRES_PASSWORD: changeme
no_log: true
with_items: "{{ molecule_yml.platforms }}"
when:
- '"pgsql" in item.groups'

- name: Prepare
hosts: all
tasks:
- name: "Create group for imaginary host"
add_host:
name: imaginary-host
groups:
- mysql
- postgresql
changed_when: false

tasks:
- name: "Installing packages on CentOS"
ansible.builtin.yum:
name:
Expand Down
23 changes: 0 additions & 23 deletions molecule/zabbix_repo/destroy.yml

This file was deleted.

Loading

0 comments on commit 28c4921

Please sign in to comment.