homeserver/roles/mgrote_docker_compose_inline/tasks/main.yml

93 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
- 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: (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
ansible.builtin.command: docker-compose down # noqa no-changed-when
args:
chdir: "{{ compose_dest_basedir }}/{{ item.name }}"
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"