ansible-infra/playbooks/roles/docker_compose/tasks/main.yaml

97 lines
3.3 KiB
YAML

- name: make sure the `/ansible_docker_compose` directory exists
ansible.builtin.file:
path: /ansible_docker_compose
state: directory
mode: "0755"
owner: root
group: root
become: true
- name: deploy the Compose file
ansible.builtin.copy:
content: "{{ docker_compose__compose_file_content }}"
dest: /ansible_docker_compose/compose.yaml
mode: "0644"
owner: root
group: root
become: true
notify: docker compose down
- name: make sure the `/ansible_docker_compose/configs` directory exists
ansible.builtin.file:
path: /ansible_docker_compose/configs
state: directory
mode: "0755"
owner: root
group: root
become: true
- name: set `docker_compose__config_files_to_exist` fact initially to an empty list
ansible.builtin.set_fact:
docker_compose__config_files_to_exist: [ ]
- name: add names from `docker_compose__configuration_files` to `docker_compose__config_files_to_exist` fact
ansible.builtin.set_fact:
docker_compose__config_files_to_exist: "{{ docker_compose__config_files_to_exist + [ item.name ] }}" # noqa: jinja[spacing]
loop: "{{ docker_compose__configuration_files }}"
- name: find configuration files to remove
ansible.builtin.find:
paths: /ansible_docker_compose/configs/
recurse: false
excludes: "{{ docker_compose__config_files_to_exist }}"
register: docker_compose__config_files_to_remove
- name: remove all configuration files, which should be removed
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
become: true
loop: "{{ docker_compose__config_files_to_remove.files }}"
notify: docker compose down
- name: make sure all given configuration files are deployed
ansible.builtin.copy:
content: "{{ item.content }}"
dest: "/ansible_docker_compose/configs/{{ item.name }}"
mode: "0644"
owner: root
group: root
become: true
loop: "{{ docker_compose__configuration_files }}"
notify: docker compose down
- name: Flush handlers to make "docker compose down" handler run now
ansible.builtin.meta: flush_handlers
- name: docker compose ps --format json before docker compose up
ansible.builtin.command:
cmd: /usr/bin/docker compose ps --format json
chdir: /ansible_docker_compose
become: true
changed_when: false
register: docker_compose__ps_json_before_up
- name: docker compose up --detach --pull always --build
ansible.builtin.command:
cmd: /usr/bin/docker compose up --detach --pull always --build
chdir: /ansible_docker_compose
become: true
changed_when: false
# The changed for this task is tried to be determined by the "potentially
# report changed" task together with the "docker compose ps --format json
# [...]" tasks.
- name: docker compose ps --format json after docker compose up
ansible.builtin.command:
cmd: /usr/bin/docker compose ps --format json
chdir: /ansible_docker_compose
become: true
changed_when: false
register: docker_compose__ps_json_after_up
- name: potentially report changed
ansible.builtin.debug:
msg: "If this reports changed, then the docker compose containers changed."
changed_when: (docker_compose__ps_json_before_up.stdout | from_json | community.general.json_query('[].ID') | sort)
!= (docker_compose__ps_json_after_up.stdout | from_json | community.general.json_query('[].ID') | sort)