Difference between revisions of "Ansible Handlers"
Jump to navigation
Jump to search
(Created page with "= Handlers= *Handlers are executed at the end of the play once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services. = Ansible Menu= Category:Ansible") |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
= Handlers= | = Handlers= | ||
*Handlers are executed at the end of the play once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services. | *Handlers are executed at the end of the play once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services. | ||
<pre> | |||
--- | |||
- name: Verify apache installation | |||
hosts: localhost | |||
tasks: | |||
- name: Ensure apache is at the latest version | |||
yum: | |||
name: httpd | |||
state: latest | |||
- name: Copy updated apache config file | |||
copy: | |||
src: /tmp/httpd.conf | |||
dest: /etc/httpd.conf | |||
notify: | |||
- Restart apache | |||
- name: Ensure apache is running | |||
service: | |||
name: httpd | |||
state: started | |||
handlers: | |||
- name: Restart apache | |||
service: | |||
name: httpd | |||
state: restarted | |||
</pre> | |||
<pre> | |||
--- | |||
- name: Enable service on firewalld | |||
hosts: localhost | |||
tasks: | |||
- name: Open port for http | |||
firewalld: | |||
service: http | |||
permanent: true | |||
state: enabled | |||
notify: | |||
- Reload firewalld | |||
- name: Ensure firewalld is running | |||
service: | |||
name: firewalld | |||
state: started | |||
handlers: | |||
- name: Reload firewalld | |||
service: | |||
name: firewalld | |||
state: reloaded | |||
</pre> | |||
=[[Ansible| Ansible Menu]]= | =[[Ansible| Ansible Menu]]= | ||
[[Category:Ansible]] | [[Category:Ansible]] | ||
Latest revision as of 18:07, 14 July 2022
Handlers
- Handlers are executed at the end of the play once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services.
---
- name: Verify apache installation
hosts: localhost
tasks:
- name: Ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: Copy updated apache config file
copy:
src: /tmp/httpd.conf
dest: /etc/httpd.conf
notify:
- Restart apache
- name: Ensure apache is running
service:
name: httpd
state: started
handlers:
- name: Restart apache
service:
name: httpd
state: restarted
---
- name: Enable service on firewalld
hosts: localhost
tasks:
- name: Open port for http
firewalld:
service: http
permanent: true
state: enabled
notify:
- Reload firewalld
- name: Ensure firewalld is running
service:
name: firewalld
state: started
handlers:
- name: Reload firewalld
service:
name: firewalld
state: reloaded