konstruktoid / ansible-role-docker-rootless

Ansible role to install a rootless Docker server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

adding skip_service_restart conditional check to skip docker service restart

scicco opened this issue · comments

commented

Hello,

Is there any way to skip handlers from running after all tasks? After some search on the web, it seems not avoidable.

Can you add a conditional check inside the Restart rootless docker to skip it?

I'll provide an example to be clear:

this is handlers/main.yml

---
- name: Restart rootless docker
  become: true
  become_user: "{{ docker_user }}"
  ansible.builtin.systemd:
    name: docker.service
    state: restarted
    scope: user
  when: not skip_service_restart

where skip_service_restart variable default is false

Alternatively, I could leverage ansible_skip_tags if you can add a tag to the task that I can use to avoid restart, something like:

---
- name: Restart rootless docker
  become: true
  become_user: "{{ docker_user }}"
  ansible.builtin.systemd:
    name: docker.service
    state: restarted
    scope: user
  tags: 
  - docker_rootless_restart_handler

Thank you in advance

Hi @scicco is there any particular reason for this?
The handler should only trigger when a new Docker release has been installed, https://github.com/konstruktoid/ansible-role-docker-rootless/blob/main/tasks/docker_install_rootless.yml#L57

commented

Hello @konstruktoid,
the reason is that my Ansible script is a disaster recovery procedure that builds containers and runs them as the last actions. This procedure is supposed to restore a copy of our application to a new server and make it up and running again. When I run this procedure the handler runs as the last step, so my running containers will shut down because of the docker restart and I have to do a manual login inside the new server to start containers again. If I can skip that handler with a proper tag or even better use a flag to skip that handler it will be handy for me.

to be more clear these are the events:

  1. install several libraries
  2. install docker rootless with this library
  3. install our codebase
  4. setup secrets and other stuff
  5. reboot the server if needed
  6. build containers
  7. run containers
  8. now we are done, but the handler runs as the last step and stops our containers

when: service_restart and service_restart: true as default seems more logical and we don't get any double negatives.

commented

I agree,

so the updated handlers/main.yml will be:

---
- name: Restart rootless docker
  become: true
  become_user: "{{ docker_user }}"
  ansible.builtin.systemd:
    name: docker.service
    state: restarted
    scope: user
  when: service_restart and service_restart: true

when: service_restart is enough since it will trigger when true

can you try #173?

commented

Hello, I've tried. Now our disaster recovery is running smoothly, thank you so much.

Do you have an ETA for the new version on ansible-galaxy?

I'll update it now.

commented

thank you