How to restart Services using Notify and Handlers in Ansible

Gaius Reji
4 min readJan 5, 2021

--

Introduction

You’ve just started out with Ansible … you’ve made yourself a playbook to install a web server, configure your index.html and other good stuff, but there’s one thing that doesn’t sit well with you … your restart service task runs even when there are no changes to your configuration !!!

This might not seem like a big deal but let’s just aim for perfection and run tasks only when required. A simple way to do this is to use the notify and handlers modules in Ansible.

Getting Started

Just to give some context on my playbook, I’ll be showing how I created my playbook from scratch.

The files on the control node:

  • apacheVar.yml : This is the file containing all variables being used in the playbook as well as the configuration file for the web server.
  • sample.conf : This is the template file we’ll be copying into the managed node as the configuration file for the Apache web server
  • my.html : A simple html file just to test our web server
  • web.yml : The main playbook that we’ll run to set up our web server

Ansible modules used:

  • package : To install the httpd web server.
  • file : To create a new document root directory.
  • template : To copy a template file (file contains variables that are formatted using jinja) into the managed node as a configuration file for the web server.
  • copy : To copy the web page into the document root of the managed node.
  • service : To start the httpd service
  • Most importantly we use notify and handlers to restart the service when there are changes made in the sample.conf file.

You might need to stop your firewall for accessing the web page if it’s not running port 80. You can do this action using ansible too.

Running the Playbook

Run the playbook using the command ansible-playbook web.yml

We observe that on our first run, the restart handler task is run.
Try running the playbook again.

This time, the handler task is not run because no changes were made in the sample.conf file.

We can view the webpage on the browser of the host system by entering the address <IP>:<portno>/<webpage> .

Let’s try changing the port number to 8080 in the sample.conf file on the controller node. Since the sample.conf file is a template file that fetches portno variable from apacheVar.yml, we need to change it’s value there.

Now, let’s run the playbook again.

This time, we see that the [Copying configuration template] has changed and thus the [restart] handler is run.

You’ll also see that if you reload the page on your browser on the previous port, it won’t work. You need to enter the new port number (8080) to view the web page.

We can make use of notify & handler in a similar fashion for other services too. Just know which file you need to observe for any changes which should then result in a service restart and add the notify and handler keywords there.

Hope this helped :)
Thankyou.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Gaius Reji
Gaius Reji

Written by Gaius Reji

Cloud | Big Data | Software Development | System Administration | Aspiring to grow my skills in the field of computer science and technology.

Responses (2)

Write a response