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

**URL:** <https://forum.ansible.com/t/how-to-mix-a-reboot-handler-with-a-service-restart-handler-in-the-same-ansible-playbook/32729>\
**Category:** Ansible Project\
**Created:** [April 19, 2020, 9:29am UTC](https://forum.ansible.com/t/how-to-mix-a-reboot-handler-with-a-service-restart-handler-in-the-same-ansible-playbook/32729 "2020-04-19T09:29:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ricardo\_Amaral](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@Ricardo\_Amaral](https://forum.ansible.com/u/Ricardo_Amaral)\
**Post date:** [April 19, 2020, 9:29am UTC](https://forum.ansible.com/t/how-to-mix-a-reboot-handler-with-a-service-restart-handler-in-the-same-ansible-playbook/32729/1 "2020-04-19T09:29:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [April 19, 2020, 10:25am UTC](https://forum.ansible.com/t/how-to-mix-a-reboot-handler-with-a-service-restart-handler-in-the-same-ansible-playbook/32729/2 "2020-04-19T10:25:52Z")

</div>

Looking at Ansible Reboot a Machine:

```auto
- name: Reboot a slow machine that might have lots of updates to apply
  reboot:
    reboot_timeout: 3600

```

```auto
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, 

```

```auto
Also the Return Value from the above is 

```

> **rebooted**   
> boolean  
> | always |   
> true if the machine was rebooted  
>   
>   
> **Sample:**  
> True  
> |
> 
> - | - | - |

```auto

```

```auto
I haven't Tested this code:

```

```auto
- 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

```

```auto

```

```auto

```

```auto
   

```

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 19, 2020, 5:53pm UTC](https://forum.ansible.com/t/how-to-mix-a-reboot-handler-with-a-service-restart-handler-in-the-same-ansible-playbook/32729/3 "2020-04-19T17:53:20Z")

</div>

> Looking at Ansible Reboot a Machine:
> 
> - name: Reboot a slow machine that might have lots of updates to apply  
> &nbsp;&nbsp;reboot:  
> &nbsp;&nbsp;&nbsp;&nbsp;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](https://docs.ansible.com/ansible/latest/modules/reboot_module.html#return-rebooted&gt);  
> boolean  
> &nbsp;&nbsp;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  
&nbsp;&nbsp;service:  
&nbsp;&nbsp;&nbsp;&nbsp;name: ssh  
&nbsp;&nbsp;&nbsp;&nbsp;state: restarted  
&nbsp;&nbsp;when: not reboot.rebooted

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

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
