homeserver/roles/mgrote_docker_compose_inline/tasks/main.yml
Michael Grote 1c6c59d598
All checks were successful
ansible-lint / gitleaks (push) Successful in 6s
ansible-lint / Ansible Lint (push) Successful in 51s
fix role docker_compose: pull and restart (#243)
This reverts commit c8e7fe9dc6.

Reviewed-on: #243
Co-authored-by: Michael Grote <michael.grote@posteo.de>
Co-committed-by: Michael Grote <michael.grote@posteo.de>
2024-11-15 21:37:29 +01:00

102 lines
3.3 KiB
YAML

---
- 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: "{{ no_debug | default('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: "{{ no_debug | default('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: Pull images
become: true
ansible.builtin.command: docker-compose pull
args:
chdir: "{{ compose_dest_basedir }}/{{ item.name }}"
register: pull_result # speichere ergebnis in var
changed_when: "pull_result.rc == 0" # markiere tasks als changed when exit-code == 0
loop: "{{ compose_files }}"
- name: (re)start container
become: true
ansible.builtin.command: docker-compose up -d
args:
chdir: "{{ compose_dest_basedir }}/{{ item.name }}"
register: start_result # speichere ergebnis in var
changed_when: "start_result.rc == 0" # markiere tasks als changed when exit-code == 0
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"