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

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?

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



   

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&gt;
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