Role do something after handler?

60 views
Skip to first unread message

Dylan Martin

unread,
Nov 11, 2015, 1:41:34 PM11/11/15
to Ansible Project
Hi all, I have a strange one that I hope you can help me with.

I'm trying to make a role for systemd service unit files.  This is the newfangled replacement for good ol' sysV init scripts in Rhel7, any recent Fedora and a lot of other linux distros.

The workflow is supposed to go something like this.

1) edit/install a unit file, like /etc/systemd/system/myservice.service
2) run systemctl daemon-reload 
3) run systemctl enable myservice
4) run systemctl start myservice

daemon-reload tells the systemd daemon to re-read the unit files so it will know you changed something

In ansible, I interpret that as looking like

1) template out unit file
2) notify daemon-reload handler if unit file changed
3) service enable=true state=started name=myservice

The problem is, if I've changed the unit file, the daemon-reload runs after the service enable/start, so systemd doesn't know to re-read the unit file before trying to start/enable the service.

I could just make it run daemon-reload every time the role runs, which kindof sucks.  Also, if I'm installing a boatload of unit files, I'd like to do a daemon-reload only once to get them all.  I could make the daemon-reload a normal task that only runs when the template changes, but that means I'd be running the daemon-reload once for every unit file, which sucks in the case where I'm installing a boatload of them.  I looked at post_task, but that's not for roles.  

Does anyone know a good way to do this?

Thanks!
-Dylan

Joanna Delaporte

unread,
Nov 11, 2015, 3:46:01 PM11/11/15
to Ansible Project
Can you have a handler notify another handler?

Otherwise, you could use a post-task in your playbook, I suppose.

Ulrich Hochholdinger

unread,
Nov 13, 2015, 1:32:44 AM11/13/15
to Ansible Project
Hi,
xou can write a handler for every single Action, and notify all desired handlers when needed. Handlers are executed in the order inside the handlers list.

theoretical example:

tasks:
   - name: change config
     action: do something
     notify:
      - enable_handler
      - reload_handler
      - start_handler
    

handlers:
   - name: reload_handler
     service: name=daemon state=reloaded
   - name: enable_handler
     service: name=daemon enabled=yes
   - name: start_handler
     service: name=daemon state=restarted  


=> the handlers are executed in the order of the handlers list as reload .. enable .. start independend of the order in the notify list. ;-)

Cheers
     Ulli

Ulrich Hochholdinger

unread,
Nov 13, 2015, 1:32:59 AM11/13/15
to Ansible Project
Hi,
xou can write a handler for every single Action, and notify all desired handlers when needed. Handlers are executed in the order inside the handlers list.

theoretical example:

tasks:
   - name: change config
     action: do something
     notify:
      - enable_handler
      - reload_handler
      - start_handler
    

handlers:
   - name: reload_handler
     service: name=daemon state=reloaded
   - name: enable_handler
     service: name=daemon enabled=yes
   - name: start_handler
     service: name=daemon state=restarted  


=> the handlers are executed in the order of the handlers list as reload .. enable .. start independend of the order in the notify list. ;-)

Cheers
     Ulli

Am Mittwoch, 11. November 2015 19:41:34 UTC+1 schrieb Dylan Martin:
Reply all
Reply to author
Forward
0 new messages