Junior — Middle
What is the role and purpose of Handlers in Ansible for automation tasks?
sobes.tech AI
Answer from AI
Handlers in Ansible are special tasks that are executed only upon notification by other tasks. Their main purpose is to perform actions that should occur after a change in the system's state, such as restarting a service after updating a configuration file.
Features of Handlers:
- They are executed once at the end of the playbook, even if called multiple times.
- They are only triggered if the task that called them has changed the state.
Example of using a handler to restart a service:
- name: Update configuration file
copy:
src: myconfig.conf
dest: /etc/myapp/config.conf
notify: Restart myapp
handlers:
- name: Restart myapp
service:
name: myapp
state: restarted
Thus, handlers help efficiently manage changes and minimize unnecessary service restarts.