From 3001317e20079b54fab4e9d7b58745f5789cc04a Mon Sep 17 00:00:00 2001 From: Sam Al-Sapti Date: Wed, 4 Oct 2023 19:35:52 +0200 Subject: [PATCH] Ansible doesn't support looping over a block --- roles/docker/tasks/loop.yml | 24 ++++++++++++++++++++++++ roles/docker/tasks/services.yml | 33 ++++++--------------------------- 2 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 roles/docker/tasks/loop.yml diff --git a/roles/docker/tasks/loop.yml b/roles/docker/tasks/loop.yml new file mode 100644 index 0000000..9ad4773 --- /dev/null +++ b/roles/docker/tasks/loop.yml @@ -0,0 +1,24 @@ +# vim: ft=yaml.ansible +--- +- name: Create volume folder + file: + name: "{{ service.vars.volume_folder }}" + state: directory + +- name: Upload Compose file + template: + src: compose-files/{{ service.name }}.yml.j2 + dest: "{{ service.vars.volume_folder }}/docker-compose.yml" + +- name: Run pre-deployment tasks + include_tasks: pre_deploy/{{ service.name }}.yml + when: service.vars.pre_deploy_tasks is defined and service.vars.pre_deploy_tasks + +- name: Deploy Compose stack + command: docker compose up -d --remove-orphans --pull always + args: + chdir: "{{ service.vars.volume_folder }}" + +- name: Run post-deployment tasks + include_tasks: post_deploy/{{ service.name }}.yml + when: service.vars.post_deploy_tasks is defined and service.vars.post_deploy_tasks diff --git a/roles/docker/tasks/services.yml b/roles/docker/tasks/services.yml index 433a4f5..6943c51 100644 --- a/roles/docker/tasks/services.yml +++ b/roles/docker/tasks/services.yml @@ -5,35 +5,14 @@ name: external_services - name: Service block for all services + include_tasks: + file: loop.yml + vars: + service: "{{ item }}" loop: "{{ services | dict2items(key_name='name', value_name='vars') }}" - loop_control: - loop_var: service when: single_service is not defined and - (service.vars.disabled_in_vagrant is not defined or - not (service.vars.disabled_in_vagrant and vagrant)) - block: - - name: Create volume folder - file: - name: "{{ service.vars.volume_folder }}" - state: directory - - - name: Upload Compose file - template: - src: compose-files/{{ service.name }}.yml.j2 - dest: "{{ service.vars.volume_folder }}/docker-compose.yml" - - - name: Run pre-deployment tasks - include_tasks: pre_deploy/{{ service.name }}.yml - when: service.vars.pre_deploy_tasks is defined and service.vars.pre_deploy_tasks - - - name: Deploy Compose stack - command: docker compose up -d --remove-orphans --pull always - args: - chdir: "{{ service.vars.volume_folder }}" - - - name: Run post-deployment tasks - include_tasks: post_deploy/{{ service.name }}.yml - when: service.vars.post_deploy_tasks is defined and service.vars.post_deploy_tasks + (item.vars.disabled_in_vagrant is not defined or + not (item.vars.disabled_in_vagrant and vagrant)) - name: Service block for a single service when: single_service is defined and single_service in services and