Ansible Handlers

From rbachwiki
Jump to navigation Jump to search

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

Ansible Menu