ansible handerls
时间: 2023-10-28 17:13:41 浏览: 73
ansible离线安装包
Ansible handlers are a type of task that is triggered only when a task that precedes it in the playbook has made a change. They are used to perform additional actions that are dependent on the success or failure of a task. Handlers are defined separately from tasks and are only executed if the task they are associated with makes a change to the system.
Handlers are useful for tasks that require a change to be made only once, such as restarting a service or updating a configuration file. They can also be used to trigger actions on multiple hosts in a playbook.
To define a handler, use the "handlers" section in an Ansible playbook. Here is an example:
```
handlers:
- name: restart apache
service:
name: apache
state: restarted
```
In this example, the handler is named "restart apache" and it uses the Ansible service module to restart the Apache service. The handler will only be triggered if a task preceding it in the playbook makes a change to the Apache service.
Handlers can also be called explicitly using the "notify" keyword in a task. Here is an example:
```
tasks:
- name: update configuration file
template:
src: config.j2
dest: /etc/myapp.conf
notify: restart apache
```
In this example, the "update configuration file" task will trigger the "restart apache" handler if it makes a change to the configuration file.
Overall, handlers are a powerful feature of Ansible that allow for more precise control over playbook execution and can help prevent unnecessary actions on systems.
阅读全文