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.