fix: handlers running in unpredictable ways

this patch adds support for in-role management of reload and restarts of the systemd service
it also adds support for ubuntu 24
This commit is contained in:
Bertrand Lanson 2024-11-09 11:04:13 +01:00
parent fee75fb57c
commit f39e1f95d0
Signed by: lanson
SSH Key Fingerprint: SHA256:/nqc6HGqld/PS208F6FUOvZlUzTS0rGpNNwR5O2bQBw
18 changed files with 141 additions and 83 deletions

View File

@ -12,6 +12,7 @@ galaxy_info:
versions:
- focal
- jammy
- noble
- name: Debian
versions:
- bullseye

View File

@ -3,7 +3,7 @@ docker_systemd_service_container_name: "nginx"
docker_systemd_service_image: nginx
docker_systemd_service_container_env: {}
docker_systemd_service_container_pull_image: true
docker_systemd_service_container_pull_force_source: true
docker_systemd_service_container_pull_force_source: false
docker_systemd_service_flags: []
docker_systemd_service_container_cmd: []
docker_systemd_service_name: "{{ docker_systemd_service_container_name }}_container"

View File

@ -0,0 +1,48 @@
---
dependency:
name: galaxy
options:
requirements-file: ./requirements.yml
driver:
name: openstack
platforms:
- name: instance.example.com
description: Molecule test instance.
flavor: a2-ram4-disk20-perf1
image: Debian 12 bookworm
user: debian
network:
name: pcp-w3rxsrj-backend-network
create: false
security_group:
name: molecule__docker_systemd_service__with_custom_flags_os
create: true
description: Molecule test security group.
rules:
- proto: tcp
port: -1
port_min: 0
port_max: 0
cidr: 0.0.0.0/0
type: IPv4
provisioner:
name: ansible
config_options:
defaults:
remote_tmp: /tmp/.ansible
verifier:
name: ansible
scenario:
name: default_os
test_sequence:
- dependency
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy

View File

@ -1,35 +0,0 @@
---
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

View File

@ -1,10 +1,10 @@
---
docker_systemd_service_container_name: "nginx"
docker_systemd_service_image: nginx
docker_systemd_service_image: nginx:1.27
docker_systemd_service_container_env:
TEST_ENV: test
docker_systemd_service_container_pull_image: true
docker_systemd_service_container_pull_force_source: true
docker_systemd_service_container_pull_force_source: false
docker_systemd_service_flags:
- privileged
- network: host

View File

@ -0,0 +1,48 @@
---
dependency:
name: galaxy
options:
requirements-file: ./requirements.yml
driver:
name: openstack
platforms:
- name: instance.example.com
description: Molecule test instance.
flavor: a2-ram4-disk20-perf1
image: Debian 12 bookworm
user: debian
network:
name: pcp-w3rxsrj-backend-network
create: false
security_group:
name: molecule__docker_systemd_service__with_custom_flags_os
create: true
description: Molecule test security group.
rules:
- proto: tcp
port: -1
port_min: 0
port_max: 0
cidr: 0.0.0.0/0
type: IPv4
provisioner:
name: ansible
config_options:
defaults:
remote_tmp: /tmp/.ansible
verifier:
name: ansible
scenario:
name: with_custom_flags_os
test_sequence:
- dependency
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy

View File

@ -1,35 +0,0 @@
---
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_flags_vagrant
test_sequence:
- dependency
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy

View File

@ -1,32 +1,41 @@
---
# task/install file for docker_systemd_service
- name: "Create ENV file(s) for docker service(s)"
- name: "Docker systemd service | Create ENV file(s) for docker service(s)"
ansible.builtin.template:
src: env.j2
dest: "{{ docker_systemd_service_sysconf_dir }}/{{ docker_systemd_service_container_name }}"
owner: root
group: root
mode: '0600'
notify: systemctl-restart-service
register: _docker_systemd_service_env_file
- name: "Pull docker image(s)"
- name: "Docker systemd service | Pull docker image(s)"
community.docker.docker_image:
name: "{{ docker_systemd_service_image }}"
force_source: "{{ docker_systemd_service_container_pull_force_source | bool }}"
source: pull
register: _docker_systemd_service_image_pull
when: docker_systemd_service_container_pull_image
notify: systemctl-restart-service
- name: "Create unit file(s) for service(s)"
- name: "Docker systemd service | Create unit file(s) for service(s)"
ansible.builtin.template:
src: unit.j2
dest: "/etc/systemd/system/{{ docker_systemd_service_name }}.service"
owner: root
group: root
mode: '0644'
notify: systemctl-restart-service
registr: _docker_systemd_service_unit_file
- name: "Enable and start service(s)"
- name: "Docker systemd service | Set reload-check & restart-check variable"
ansible.builtin.set_fact:
_docker_systemd_service_need_daemon_reload: >-
{{ _docker_systemd_service_unit_file.changed | bool }}
_docker_systemd_service_need_restart: true
when: _docker_systemd_service_env_file.changed or
_docker_systemd_service_unit_file.changed or
docker_systemd_service_container_pull_image.changed
- name: "Docker systemd service | Enable and start service(s)"
ansible.builtin.systemd:
name: '{{ docker_systemd_service_name }}.service'
daemon_reload: true

View File

@ -1,9 +1,31 @@
---
# task/main file for docker_systemd_service
- name: "Import install.yml"
- name: "Docker systemd service | Set reload-check & restart-check variable"
ansible.builtin.set_fact:
_docker_systemd_service_need_daemon_reload: false
_docker_systemd_service_need_restart: false
- name: "Docker systemd service | Import install.yml"
ansible.builtin.include_tasks: install.yml
when: docker_systemd_service_state != "absent"
- name: "Import uninstall.yml"
- name: "Docker systemd service | Import uninstall.yml"
ansible.builtin.include_tasks: uninstall.yml
when: docker_systemd_service_state == "absent"
- name: "Docker systemd service | Enable service: {{ docker_systemd_service_name }}"
ansible.builtin.service:
name: "{{ docker_systemd_service_name }}"
enabled: true
- name: "Docker systemd service | Reload systemd daemon"
ansible.builtin.systemd:
daemon_reload: true
when: _docker_systemd_service_need_daemon_reload
- name: "Docker systemd service | Start service: {{ docker_systemd_service_name }}"
ansible.builtin.service:
name: "{{ docker_systemd_service_name }}"
state: restarted
throttle: 1
when: _docker_systemd_service_need_restart

View File

@ -1,3 +1,3 @@
{% for k,v in docker_systemd_service_container_env|dictsort %}
{{ k }}={{ v }}
{% endfor %}
{% endfor %}