0%

0/1 Lessons

Course Introduction

• 16min

0 / 3 lessons complete

IT Lab Setup

• 20min

0 / 3 lessons complete

Installing Ansible

• 35min

0 / 3 lessons complete

Managing your Ansible Inventory

• 2hr 7min

0 / 8 lessons complete

Ansible Basics

• 1hr 43min

0 / 9 lessons complete

Ansible Roles

• 1hr 46min

0 / 8 lessons complete

Ansible Galaxy

• 1hr 33min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 1hr 51min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 1hr 41min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Ansible Handlers

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, you will learn about Ansible handlers, their importance in playbook design, and how to effectively use them to manage service states or perform tasks based on the results of other tasks. Handlers are special tasks in Ansible that run only when notified by another task. This mechanism is particularly useful for optimizing your automation scripts by ensuring certain actions, like restarting a service, are only performed when necessary.

Understanding Ansible Handlers

Handlers are triggered by other tasks in your Ansible playbook that might make changes requiring a service restart or another follow-up action. For instance, if you're deploying a new version of a web application and need to restart the web server to apply the changes, you'd use a handler for the restart action. The beauty of handlers is that they run at the end of your playbook's execution and only if they've been 'notified' by another task, ensuring that a service is not unnecessarily restarted multiple times, which could cause service downtime or other issues.

How to Define and Use Handlers

To define a handler, you'll usually place it under the handlers section in your Ansible playbook or inside role/handlers/main.yml.

Let's create a handler that will restart the Nginx service. Nano the following file to create the handler YAML file:

nano roles/webserver/handlers/handlers.yml

Next, create the handler like so:

- name: restart nginx
  become: yes
  service:
    name: nginx
    state: restarted

Save and close the file.

If you are defining a handler outside of the context of a role, you can define the handler in your playbook below the tasks like so:

handlers:
  - name: restart nginx
    become: yes
    service:
      name: nginx
      state: restarted

This handler, named restart nginx, uses the service module to restart the Nginx service. To trigger this handler, you must notify it from a task. Let's update our webservers main task to include the notify action on our template task with the notify block:

---
# Tasks for webserver role
- name: Install Nginx
  apt:
    name: nginx
    state: present
  become: yes

- name: Template index.html.j2 to web directory
  template:
    src: index.html.j2
    dest: /var/www/html/index.html
  become: yes
  notify: restart nginx

In this task, if the template module changes the nginx.conf file (indicating that the file on the server was replaced with a new version), the restart nginx handler is notified. However, the actual restart doesn't happen immediately but is instead deferred until all tasks in the playbook have run. If multiple tasks notify the handler, the handler still runs only once, avoiding unnecessary restarts.

Let's update our playbook to use the role. Edit first_playbook.yml to use the webserver role:

---
- hosts: webservers
  vars_files:
    - secret.yml
  roles:
    - webserver

Now execute the playbook:

ansible-playbook first_playbook.yml
image
Ansible Playbook Running

What you'll notice is that the restart does not happen because we did not make any changes to the template.

nano roles/webserver/templates/index.html.j2

Best Practices with Handlers

Verifying Handlers

To verify that your handler has executed correctly, you can check the status of the service you expected to restart or the outcome of any action your handler was supposed to perform. For instance, for a service restart, you might run:

systemctl status nginx

If the service status indicates it's active and running, your handler has successfully restarted the service.

Conclusion

Handlers are a powerful feature in Ansible that allows for efficient management of service states and other actions that should only occur in response to changes. By using handlers, you can make your playbooks more efficient and maintainable. Remember to test your playbooks and handlers thoroughly to ensure they perform as expected. Great job on completing this lesson on Ansible handlers! See you in the next lesson.

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments