0

My role have two handlers: start service and restart service. I want to run start service only if service was installed, and I want to restart service if I changed service configuration file.

(That were normal ansible handlers, now complicated part starts)

I want to avoid doing restart if I start application (started handler was executed).

Basically, if:

1. package was installed
2. config was changed
=> start service

if:

1. package wasn't changed
2. config was changed
=> restart service

I tried to play with listen, but it's designed for different purposes and can't help here.

Can someone give me an idea who to do this?

George Shuklin
  • 6,952
  • 10
  • 39
  • 80
  • Just a wild idea, nothing tested here: maybe had a `set_fact: just_started: true` in the start handler and a `when: not just_started` in the restart handler? – β.εηοιτ.βε Jun 09 '20 at 12:03
  • Handler can be the single task only. You either start service or set fact. I can `register` in handler for 'start' and use this to `when` for the second handler, but I really dislike idea of having 'register' in handlers. There is a chance they will run more than once, and a single `register` may trash everything. – George Shuklin Jun 09 '20 at 12:26
  • Ansible oddity but you can work around the one task in handler, see https://github.com/ansible/ansible/issues/14270#issuecomment-178775478 – β.εηοιτ.βε Jun 09 '20 at 13:21
  • Is this a systemd service ? Because in this case, `state: restarted` will take care of all scenarii and you can call a single handler. An other way to handle this is to have a stop and start handler call them in order using `meta: flush_handler` inside your play – Zeitounator Jun 09 '20 at 16:16

0 Answers0