From 003c4d40b0900c025192ba8e6332f7c0e5cd8a12 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 31 Aug 2023 23:13:51 +0200 Subject: docker: factor out service restart as a handler --- roles/docker/handlers/main.yml | 7 +++++++ roles/docker/tasks/logging.yml | 17 +++++------------ roles/docker/tasks/main.yml | 13 +++++++++---- 3 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 roles/docker/handlers/main.yml diff --git a/roles/docker/handlers/main.yml b/roles/docker/handlers/main.yml new file mode 100644 index 0000000..742a98c --- /dev/null +++ b/roles/docker/handlers/main.yml @@ -0,0 +1,7 @@ +- name: Restart Docker + become: true + ansible.builtin.systemd_service: + daemon_reload: true + name: docker + state: restarted + listen: docker_restart diff --git a/roles/docker/tasks/logging.yml b/roles/docker/tasks/logging.yml index 99d3cab..9dcf103 100644 --- a/roles/docker/tasks/logging.yml +++ b/roles/docker/tasks/logging.yml @@ -4,29 +4,22 @@ become: true ansible.builtin.slurp: src: /etc/docker/daemon.json - register: docker_config + register: current_docker_config - name: Parse daemon.json ansible.builtin.set_fact: - docker_config: '{{ docker_config.content | b64decode | from_json }}' + current_docker_config: '{{ current_docker_config.content | b64decode | from_json }}' rescue: - name: daemon.json is missing ansible.builtin.set_fact: - docker_config: {} + current_docker_config: {} - name: Modify Docker config become: true ansible.builtin.copy: dest: /etc/docker/daemon.json - content: '{{ docker_config | combine({"log-driver": docker_logging_driver}) | to_nice_json }}' + content: '{{ current_docker_config | combine({"log-driver": docker_logging_driver}) | to_nice_json }}' owner: root group: root mode: '644' - register: daemon_json - -- name: Restart Docker - become: true - ansible.builtin.systemd_service: - name: docker - state: restarted - when: daemon_json.changed + notify: docker_restart diff --git a/roles/docker/tasks/main.yml b/roles/docker/tasks/main.yml index c8361a0..639615b 100644 --- a/roles/docker/tasks/main.yml +++ b/roles/docker/tasks/main.yml @@ -5,7 +5,16 @@ name: - docker.io - docker-compose + policy_rc_d: 101 state: present + 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: Clean up Docker data become: true @@ -17,7 +26,3 @@ networks: true volumes: true builder_cache: true - -- name: Configure Docker logging driver - when: docker_logging_driver is defined and docker_logging_driver - ansible.builtin.include_tasks: logging.yml -- cgit v1.2.3