Idempotent in Ansible using apache webserver.

Anirudh Bambhania
3 min readJan 13, 2021

--

đź”° Restarting HTTPD Service is not idempotent in nature and also consume more resources. In this article i will show you a way to rectify this challenge in Ansible playbook.

I have create two files in my workspace one is the ansible palybook “http.yml” and the other is the test file which has some random data, that will be send to the target node.

Playbook :

  • As the playbook will run the package module will install the “httpd” program on the target node which has ip 192.168.43.151, after the installation the data inside “test.txt” file will be copied to the “/var/www/html/index.html” file. “/var/www/html” this folder contains all the webpages of the apache webserver. And finally apache httpd webserver service will be started.
  • Now run the playbook.
  • Again run the same playbook
  • As we can see that the service is again restarted even though we have not made any changes. Due to this additional resources are being used to again restart the service and extra load will be placed on the RAM and CPU ,this will eventually impact our performance, so to overcome this we can use “handlers”. We can use “handlers” module of ansible so that when ever some changes are made in the file than only restart the service. For this in the template module use “notify” keyword and give the name of the handler to get notified of the changes. In handlers we have to give a name so that notify can connect and the tasks we wan to run after the changes. Here the service task we can place in the handlers module.
  • Now run the playbook. We can see that now the service task is not run because no changes are made.
  • Let us change the content of the test file and put some random html data just for testing purpose.
  • Now run the playbook
  • We can see that this time notify keyword informed the handler that some changes are made in the file and handler run the service task and restarted the apache httpd webserver.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Thank You.

--

--