Restarting HTTPD Service is not idempotent in nature and also consume more resources suggest a way to rectify this challenge in Ansible Playbook.

TAMANNA VERMA
3 min readMar 21, 2021

What is Idempotence nature in Ansible?

We can say that an operation is idempotent if the result of performing it once is exactly equal as the result of performing it repeatedly without any intervening actions. Ansible follows idempotent nature but some modules don’t support idempotent behaviour like the httpd module.

Idempotent Playbooks

When a playbook is executed to configure a system, the system should always have the same, well defined state. If a playbook consists of 10 steps, and the system deviates in step 4 from the desired state, then only this particular step should be applied.

By its nature, Ansible tasks will only change the system if there is something to do. Most Ansible modules provide this idempotency. But some modules can be used in a way that breaks this pattern.

For example: When we run playbook to configure httpd apache webserver, there the httpd service will always start no matter if it is already started in target node. This means that it doesn’t allow idempotancy.

The problem of idempotancy we need to rectify.

To rectify this issue either we can use the when condition which allows to start the service when the condition matches (condition : if httpd listen port changes to other port number) or can use handlers and notify concept which states that when the particular task done the notify will trigger the handlers block of code.

Considering you know how to step up Ansible inventory file, config file and check the connectivity between between control node and managed node. If not click here.

Let’s look into playbook code:

Here the playbook will run on the same system where Ansible configured that is the target node will be localhost itself.

Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are only run if the Task contains a notify directive and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.

The playbook do the following tasks:

  1. Install the httpd package.
  2. Copy the content into the destination folder (/var/www/html/index.html) and this task uses notify which will trigger the handlers when something change in the content of file and then only handlers will restart the httpd service.
  3. Enabling the port number for httpd which firewall will allow.

Here I have use simple example to show the idempotence nature for httpd module. But you can use different files to upload like httpd conf file and use jinja concept and make the code more dynamic like asking file name and port number for webserver at runtime of playbook. Instead of Copy module can use template module.

When content of the file is not change, then handlers task will not execute.

We can Access the webpage using IP:port (port which you used)

When content of file changed then the notify will trigger handlers task.

As it can be seen that the Content of file changed

The Client can access newer data

--

--