From 8d4e09be7ca7ea7b0777e99e662459e031380c54 Mon Sep 17 00:00:00 2001 From: SilasPeters <69711739+SilasPeters@users.noreply.github.com> Date: Fri, 12 Jul 2024 19:03:31 +0200 Subject: [PATCH] feat: compose-up.yml to quickly test and run a docker compose file --- ansible/roles/docker/tasks/compose-up.yml | 78 +++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ansible/roles/docker/tasks/compose-up.yml diff --git a/ansible/roles/docker/tasks/compose-up.yml b/ansible/roles/docker/tasks/compose-up.yml new file mode 100644 index 00000000..6d07ec22 --- /dev/null +++ b/ansible/roles/docker/tasks/compose-up.yml @@ -0,0 +1,78 @@ +--- +# NOTE These tasks expect the 'project_source' variable to be set + +- name: "Check if Docker daemon is active" + shell: "systemctl is-active docker" + register: "docker_status" + ignore_errors: "yes" # Will give a non-zero error code if not active + +- name: "Assert Docker daemon is running" + assert: + that: + - "docker_status.rc == 0" + fail_msg: "Docker daemon is not running" + success_msg: "Docker daemon is running" + +- name: "Tear down existing services" + community.docker.docker_compose_v2: + project_src: "{{ project_source }}" + state: "absent" + +- name: "Create and start services" + community.docker.docker_compose_v2: + project_src: "{{ project_source }}" + register: "output" + +# - name: Show results +# ansible.builtin.debug: +# var: output + +- name: "Run `docker compose up` again" + community.docker.docker_compose_v2: + project_src: "{{ project_source }}" + register: "output" + +# - name: Show results +# ansible.builtin.debug: +# var: output + +- name: "Assert that all services properly started" + ansible.builtin.assert: + that: "not output.changed" + + # Remember what services are included in the compose file, for later +- name: "Extract service names from output" + set_fact: + docker_compose_service_names: "{{ output.containers | map(attribute='Name') | list }}" + +- name: "Stop all services" + community.docker.docker_compose_v2: + project_src: "{{ project_source }}" + state: "stopped" + register: "output" + +# - name: Show results +# ansible.builtin.debug: +# var: output + +- name: "Verify that all containers stopped" # TODO how to verify all other services? + ansible.builtin.assert: + that: + - "output.containers | selectattr('Name', 'equalto', item) | map(attribute='State') | list | first == 'exited'" + loop: "{{ docker_compose_service_names }}" + +- name: "Restart services" + community.docker.docker_compose_v2: + project_src: "{{ project_source }}" + state: "restarted" + register: "output" + +# - name: Show results +# ansible.builtin.debug: +# var: output + +- name: "Verify that all containers are running" # TODO how to verify all other services? + ansible.builtin.assert: + that: + - "output.containers | selectattr('Name', 'equalto', item) | map(attribute='State') | list | first == 'running'" + loop: "{{ docker_compose_service_names }}"