From 33847d19fb2a76e91486e63b473afb4aa13de4cb Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 22 Sep 2023 11:04:34 +0200 Subject: docker: avoid duplicate service restarts --- roles/docker/tasks/logging.yml | 38 +++++++++++++++++++++++++++++++------- roles/docker/tasks/main.yml | 20 +++++++++++--------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/roles/docker/tasks/logging.yml b/roles/docker/tasks/logging.yml index b40da18..f93efa0 100644 --- a/roles/docker/tasks/logging.yml +++ b/roles/docker/tasks/logging.yml @@ -16,10 +16,34 @@ - name: Modify Docker config become: true - ansible.builtin.copy: - dest: /etc/docker/daemon.json - content: '{{ docker_current_config | combine({"log-driver": docker_logging_driver}) | to_nice_json }}' - owner: root - group: root - mode: '644' - notify: docker_restart + block: + - name: Create /etc/docker + ansible.builtin.file: + path: /etc/docker + state: directory + owner: root + group: root + mode: '755' + + - name: Write daemon.json + ansible.builtin.copy: + dest: /etc/docker/daemon.json + content: '{{ docker_current_config | combine({"log-driver": docker_logging_driver}) | to_nice_json }}' + owner: root + group: root + mode: '644' + register: docker_config + +- name: Restart Docker if necessary + become: true + when: docker_config.changed + block: + - name: Collect service facts + ansible.builtin.service_facts: + + - name: Restart Docker service + when: | + 'docker' in ansible_facts.services and ansible_facts.services.docker.state == 'running' + ansible.builtin.systemd_service: + name: docker + state: restarted diff --git a/roles/docker/tasks/main.yml b/roles/docker/tasks/main.yml index f4ba814..95f8565 100644 --- a/roles/docker/tasks/main.yml +++ b/roles/docker/tasks/main.yml @@ -1,19 +1,21 @@ +- name: Configure Docker logging driver + when: docker_logging_driver is defined and docker_logging_driver + ansible.builtin.include_tasks: logging.yml + - name: Install Docker become: true - ansible.builtin.apt: + ansible.builtin.package: name: - docker.io - docker-compose state: present - policy_rc_d: 101 - notify: docker_restart -- name: Configure Docker logging driver - when: docker_logging_driver is defined and docker_logging_driver - ansible.builtin.include_tasks: logging.yml - -- name: Restart systemd service if necessary - ansible.builtin.meta: flush_handlers +- name: Enable & start Docker service + become: true + ansible.builtin.systemd_service: + name: docker + enabled: true + state: started - name: Clean up Docker data become: true -- cgit v1.2.3