2023-10-25 22:26:17 +02:00
|
|
|
---
|
|
|
|
- name: ensure basedir exists
|
|
|
|
become: true
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: "{{ compose_dest_basedir }}"
|
|
|
|
state: directory
|
|
|
|
mode: "{{ compose_dir_permissions }}"
|
|
|
|
owner: "{{ compose_owner }}"
|
|
|
|
group: "{{ compose_group }}"
|
|
|
|
|
|
|
|
# https://codeutility.org/ansible-can-the-templates-module-handle-multiple-templates-directories-stack-overflow/
|
2023-11-12 21:53:11 +01:00
|
|
|
- name: ensure all directories exists
|
2023-10-25 22:26:17 +02:00
|
|
|
ansible.builtin.file:
|
|
|
|
dest: "{{ compose_dest_basedir }}/{{ item | replace(compose_src_basedir + '/', '') }}"
|
|
|
|
state: directory
|
|
|
|
mode: "{{ compose_dir_permissions }}"
|
|
|
|
owner: "{{ compose_owner }}"
|
|
|
|
group: "{{ compose_group }}"
|
|
|
|
with_items: "{{ lookup('pipe', 'find '+ compose_src_basedir +'/ -type d').split('\n') }}"
|
|
|
|
|
2023-11-12 21:53:11 +01:00
|
|
|
- name: ensure all files exists (can take a long time)
|
2023-10-25 22:26:17 +02:00
|
|
|
ansible.builtin.copy:
|
|
|
|
mode: "{{ compose_file_permissions }}"
|
|
|
|
owner: "{{ compose_owner }}"
|
|
|
|
group: "{{ compose_group }}"
|
|
|
|
src: "{{ item }}"
|
|
|
|
dest: "{{ compose_dest_basedir }}/{{ item | replace(compose_src_basedir + '/', '') }}"
|
|
|
|
with_items: "{{ lookup('pipe', 'find '+ compose_src_basedir +'/ -type f -not -name *.j2 ').split('\n') }}"
|
|
|
|
no_log: true
|
|
|
|
|
2023-11-12 21:53:11 +01:00
|
|
|
- name: ensure templated files exists
|
2023-10-25 22:26:17 +02:00
|
|
|
ansible.builtin.template:
|
|
|
|
mode: "{{ compose_file_permissions }}"
|
|
|
|
owner: "{{ compose_owner }}"
|
|
|
|
group: "{{ compose_group }}"
|
|
|
|
src: "{{ item }}"
|
|
|
|
dest: "{{ compose_dest_basedir }}/{{ item | replace(compose_src_basedir + '/', '') | replace('.j2', '') }}"
|
|
|
|
with_items: "{{ lookup('pipe', 'find '+ compose_src_basedir +'/ -type f -name *.j2').split('\n') }}"
|
2024-11-10 15:20:53 +01:00
|
|
|
# no_log: true
|
2023-11-16 20:09:14 +01:00
|
|
|
register: copy_template
|
2023-10-25 22:26:17 +02:00
|
|
|
|
2023-11-12 21:53:11 +01:00
|
|
|
- name: Ensure needed networks exists
|
2023-10-25 22:26:17 +02:00
|
|
|
become: true
|
2023-11-12 21:53:11 +01:00
|
|
|
community.docker.docker_network:
|
|
|
|
name: "{{ item.network }}"
|
|
|
|
state: present
|
|
|
|
internal: false
|
|
|
|
enable_ipv6: false
|
|
|
|
driver: bridge
|
2023-10-25 22:26:17 +02:00
|
|
|
loop: "{{ compose_files }}"
|
|
|
|
when:
|
|
|
|
- item.network is defined
|
|
|
|
|
2024-10-23 17:59:34 +02:00
|
|
|
- name: restart changed container
|
2023-10-25 22:26:17 +02:00
|
|
|
become: true
|
2024-10-23 17:59:34 +02:00
|
|
|
community.docker.docker_compose_v2:
|
|
|
|
project_src: "{{ compose_dest_basedir }}/{{ item.name }}"
|
|
|
|
state: restarted
|
|
|
|
dependencies: true
|
|
|
|
pull: missing
|
2023-10-25 22:26:17 +02:00
|
|
|
loop: "{{ compose_files }}"
|
|
|
|
when:
|
|
|
|
- item.state == "present"
|
|
|
|
- copy_template is changed
|
|
|
|
|
|
|
|
- name: stop old container
|
|
|
|
become: true
|
2024-10-23 17:59:34 +02:00
|
|
|
community.docker.docker_compose_v2:
|
|
|
|
project_src: "{{ compose_dest_basedir }}/{{ item.name }}"
|
|
|
|
state: absent
|
2023-10-25 22:26:17 +02:00
|
|
|
loop: "{{ compose_files }}"
|
|
|
|
when:
|
|
|
|
- item.state == "absent"
|
|
|
|
ignore_errors: true # noqa ignore-errors
|
|
|
|
|
2023-11-12 21:53:11 +01:00
|
|
|
- name: Ensure old networks are absent
|
2023-10-25 22:26:17 +02:00
|
|
|
become: true
|
2023-11-12 21:53:11 +01:00
|
|
|
community.docker.docker_network:
|
|
|
|
name: "{{ item.network }}"
|
|
|
|
state: absent
|
2023-10-25 22:26:17 +02:00
|
|
|
loop: "{{ compose_files }}"
|
|
|
|
when:
|
|
|
|
- item.network is defined
|
2023-11-12 21:53:11 +01:00
|
|
|
- item.state == "absent"
|
2023-10-25 22:26:17 +02:00
|
|
|
|
|
|
|
- name: remove old docker-compose files & directories
|
|
|
|
become: true
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: "{{ compose_dest_basedir }}/{{ item.name }}/"
|
|
|
|
state: absent
|
|
|
|
loop: "{{ compose_files }}"
|
|
|
|
when:
|
|
|
|
- item.state == "absent"
|