[Ansible] – Understanding Handlers

Handlers are used to trigger actions when the tasks are executed. For example, in the case of being required to start some service after an installation then a handler to restart the service could be called. Following an example.

---
- name: Handler exemple
  hosts: all
  tasks:
  - name: Copy content 
    copy:
      content: "Copy content to the index.html file"
      dest: /var/www/html/index.html
    notify: 
      - restart_service 
  handlers: 
  - name: restart_service 
    service: 
      name: mongodb 
      state: restarted

NOTE: The notify attribute is used to call the handler and the handlers are called by the name.

For further information see the Intro to Playbooks documentation.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s