How to mix a reboot handler with a service restart handler in the same Ansible playbook?

241 views
Skip to first unread message

Ricardo Amaral

unread,
Apr 19, 2020, 5:29:41 AM4/19/20
to Ansible Project
I'm writing an Ansible playbook to set up my Raspberry Pi so that I can keep a versioned configuration and a way for me to quickly get a Pi up and running in case I need to reset it or start from scratch.


This playbook has a bunch of tasks where some of them (very few) notify a handler to reboot the system while others notify different handlers to restart different services (due to configuration changes to those services).


Given how my playbook/roles/tasks as structured, the reboot handler is the "first handler in the list", which means it will be the first one to be executed at the end of the playbook, followed by the rest of the handlers.


The problem is that if the system reboots, Ansible will wait for the system to be back up and continue firing all handlers, like service restarts. The thing is, the services were restarted when the system rebooted, there's no need to restart them again.


I mean, if the system is going to reboot (the task changed something), handlers for service restarts don't need to run (if their tasks changed anything), just reboot the system. But if the task which requires a system reboot didn't change anything but the task that requires a service restart changed something, then that task handler needs to fire.


How can I do this?

David Foley

unread,
Apr 19, 2020, 6:25:52 AM4/19/20
to Ansible Project
Looking at Ansible Reboot a Machine:

- name: Reboot a slow machine that might have lots of updates to apply
  reboot:
    reboot_timeout: 3600
This will Reboot the System and Test the connection: I wonder if you do a Timeout of zero: default is 6000 what happens if you set this to zero, 
Also the Return Value from the above is 
rebooted 
boolean
always
true if the machine was rebooted

Sample:
True

I haven't Tested this code:
- name: Reboot a slow machine that might have lots of updates to apply
  reboot:
    reboot_timeout: 3600
  register: reboot

  - debug:
      var: reboot.rebooted

- name: Service Restart
  service:
    name: httpd
    state: restarted
  when: "{{ reboot }}" == no


   

Stefan Hornburg (Racke)

unread,
Apr 19, 2020, 1:53:50 PM4/19/20
to ansible...@googlegroups.com
On 4/19/20 12:25 PM, David Foley wrote:
> Looking at Ansible Reboot a Machine:
>
> - name: Reboot a slow machine that might have lots of updates to apply
> reboot:
> reboot_timeout: 3600
>
> This will Reboot the System and Test the connection: I wonder if you do a Timeout of zero: default is 6000 what happens
> if you set this to zero,
>
> Also the Return Valuefrom the above is
>
> *rebooted* <https://docs.ansible.com/ansible/latest/modules/reboot_module.html#return-rebooted>
> boolean
> always
> true if the machine was rebooted
>
> *Sample:*
> True
>
>
> I haven't Tested this code:
>
> - name: Reboot a slow machine that might have lots of updates to apply reboot: reboot_timeout: 3600 register: reboot -
> debug: var: reboot.rebooted - name: Service Restart service: name: httpd state: restarted when: "{{ reboot }}" == no
>

The conditions for when and friends are virtually wrapped in a Jinja template, thus curly braces are wrong here.

But you can apply when conditions to handlers as well:

- name: restart ssh
service:
name: ssh
state: restarted
when: not reboot.rebooted

It is fragile though, as it depends on the order of the handlers which is per se unknown.

Regards
Racke


>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/6251ea49-de6e-4b1e-82c3-2be23dd15dfb%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/6251ea49-de6e-4b1e-82c3-2be23dd15dfb%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc
Reply all
Reply to author
Forward
0 new messages