homeserver/roles/mgrote_docker_compose_inline/tasks/main.yml

94 lines
2.9 KiB
YAML
Raw Normal View History

---
- 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/
- name: ensure all directories exists
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') }}"
- name: ensure all files exists (can take a long time)
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
- name: ensure templated files exists
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') }}"
no_log: true
register: copy_template
- name: Ensure needed networks exists
become: true
community.docker.docker_network:
name: "{{ item.network }}"
state: present
internal: false
enable_ipv6: false
driver: bridge
loop: "{{ compose_files }}"
when:
- item.network is defined
- name: restart changed container
become: true
community.docker.docker_compose_v2:
project_src: "{{ compose_dest_basedir }}/{{ item.name }}"
state: restarted
dependencies: true
pull: missing
loop: "{{ compose_files }}"
when:
- item.state == "present"
- copy_template is changed
- name: stop old container
become: true
community.docker.docker_compose_v2:
project_src: "{{ compose_dest_basedir }}/{{ item.name }}"
state: absent
loop: "{{ compose_files }}"
when:
- item.state == "absent"
ignore_errors: true # noqa ignore-errors
- name: Ensure old networks are absent
become: true
community.docker.docker_network:
name: "{{ item.network }}"
state: absent
loop: "{{ compose_files }}"
when:
- item.network is defined
- item.state == "absent"
- 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"