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.
After that you can see below the ansible inventory i.e ansible.cfg file
Check the ansible version.
Ping the hosts to check the connectivity between the host and manage node
Here you can see that , there is a proper connectivity between manage node and the hosts.
Now see below the playbook ,
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 .
This configure the SELinux policy like enabling , disabling etc.
This is the var file , where stored all the vars usd in the playbook.
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.
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.
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.
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.
Now you can check in virtual machine whose IP is 192.168.43.83 where I want to deploy web server.
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.
Cloud Technical Solutions Engineer @ Google
3yWell done Pawar suvarna 🌟
InfoSec || Services IT Dev. Program Sr. Associate I || NTT Data
3ygreat work
Associate Software Engineer @Veritas Technologies LLC | Building Kubernetes Protection | Top 1% @Leetcode | Specialist @Codeforces
3yGreat work ✨