Restarting HTTPD Service Is Not Idempotence In Nature Using Ansible Playbook

Restarting HTTPD Service Is Not Idempotence In Nature Using Ansible Playbook

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

What is Ansible?

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn't depend on agent software and has no additional security infrastructure, so it's easy to deploy.

How Ansible works

Ansible works by connecting to nodes (clients, servers, or whatever you're configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes. SSH Keys are the most common way to provide access, but other forms of authentication are also supported

What is idempotence???

In general, idempotence is “the property of certain operations in mathematics and computer science that can be applied multiple times without changing the result beyond the initial application”. For Ansible it means after 1 run of a playbook to set things to a desired state, further runs of the same playbook should result in 0 changes. 

But "restarting httpd service" is not idempotence in nature

When we need to restart httpd services???

When ever we make some changes in httpd configuration file then we need to restart the services.

But we can add one more section name "handlers" which contains task, that would be executed only and only when it is notified.

Handlers

Sometimes you want a task to run only when a change is made on a machine. For example, you may want to restart a service if a task updates the configuration of that service, but not if the configuration is unchanged. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified. Each handler should have a globally unique name.For triggering handler we need to use notify directive. Basically when ever any changes are made it will notify handler and hence handler's task would be executed.

Now Lets start the practical :

Here you can see that the myhosts.txt file , where is stored all the manage nodes login details.

No alt text provided for this image

After that you can see below the ansible inventory i.e ansible.cfg file

No alt text provided for this image

Check the ansible version.

No alt text provided for this image

Ping the hosts to check the connectivity between the host and manage node

No alt text provided for this image
No alt text provided for this image

Here you can see that , there is a proper connectivity between manage node and the hosts.

Now see below the playbook ,

No alt text provided for this image
No alt text provided for this image

Here in this playbook , we need to install the ansible.posix.selinux collection , sometimes is is not pre-installed . By using the following command we can install it .

No alt text provided for this image

This configure the SELinux policy like enabling , disabling etc.

This is the var file , where stored all the vars usd in the playbook.

No alt text provided for this image

Now check the syntax of the main playbook ansible-playbook --syntax-check server.yml and after that run this playbook by typing ansible-playbook server.yml. It will give the output like this.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image


Here we can see that playbook run without any error .

Now i am going to the manage nodes and see the configuration , whatever we have did in the playbook.

No alt text provided for this image
No alt text provided for this image

You can see here port number is listening and the httpd software is successfully installed.

Now i am going to the web-browser an check the webpage , that is i am copying from the os to manage hosts.

No alt text provided for this image

Now If you run the playbook again then it will shows that Your service is started so no need the restart again this become possible because of the handlers and notify keyworks in ansible.

Now I change my var file where I store the variables.

No alt text provided for this image
No alt text provided for this image

Now you can check in virtual machine whose IP is 192.168.43.83 where I want to deploy web server.

No alt text provided for this image

Here you can see the configuration .

Now i am going to the web-browser to check the webpages coming or not.

You can check the final output from the browser and type both the port number 8085.

No alt text provided for this image


Thank You ...!!!!!!!!!!!!!!


Adarsh Kumar

Cloud Technical Solutions Engineer @ Google

3y

Well done Pawar suvarna  🌟

Like
Reply
Krishana kant

InfoSec || Services IT Dev. Program Sr. Associate I || NTT Data

3y

great work

Like
Reply
Nilesh Gopale

Associate Software Engineer @Veritas Technologies LLC | Building Kubernetes Protection | Top 1% @Leetcode | Specialist @Codeforces

3y

Great work ✨

Like
Reply

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics