HTTP Service is not Idempotence in Nature — Make it using Ansible
By: Simran Kukareja
Tags: Idempotency, Ansible, Ansible Playbook, Apache, Httpd

What is Ansible??

As we know when we work in heterogeneous environment we face many difficulties. We don’t know which command that server supports. There are many different types of Operating Systems which has different commands to do operations.

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, etc. Ansible is agentless, temporarily connecting remotely via SSH to do its tasks.

What is Apache Web Server??

The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.

What is Idempotence in Ansible??

The principle that enables Ansible to be declarative and yet reliable is idempotence, a concept borrowed from mathematics. An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application, such as multiplication by zero. Ansible modules are idempotent.

TASK DESCRIPTION:

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

TASK COMPLETION:

Pre-requisite for this task :

Two RHEL OS on your system with yum configuration.

Controller Node

Managed Node

So let’s start for this task.

First we launch one OS on which we install ansible and worked as a Controller Node .

2. For the target node we can launch one managed node.

Ansible knows about its target node from inventory file that we have to create inventory and give the path of that inventory file to ansible configuration file → /etc/ansible/ansible.cfg .

To see all target nodes run command -

ansible all --list-hosts

To see all nodes are connected or ping run command -

ansible all -m ping

3.Now our system is configured. So let’s start to write code in one ansible playbook.

We use YAML language to code in ansible playbooks .As all the target work that are going to be done by ansible are divided into different tasks which form a play .

First we install httpd (Apache) package using package module using ansible script.

Let’s see in target node that package is installed or not.

It’s installed.

What is Handlers??

Handlers are just like normal tasks in an Ansible playbook but they run only when if the Task contains a “notify” directive. It also indicates that it changed something.

4. Now copy web page in /var/www/html folder and notify service module to start.

Output be like

Let’s see in target node that web pages are copied or not

5. Using Handlers we start httpd service and allow firewall.

Output be like

6.Now we run complete playbook

7. Now we are ready to connect the webserver we configured.

Yeahhh…. It’s working.

8.When we run again this playbook it never executes handlers.

As we can see handler is not executed because of no any change in copy module and httpd service is already started.

If there will any change in copy or package module it again restart service i.e. execution of handler will be done.

It proves that Httpd service can be idempotent using ansible handlers.

For better understanding please see video attached below.