How to make Apache services idempotent using ansible?
Apache is the most widely used web server software. Developed and maintained by Apache Software Foundation, Apache is an open source software available for free. It runs on 67% of all web servers in the world. It is fast, reliable, and secure. It can be highly customized to meet the needs of many different environments by using extensions and modules. Most WordPress hosting providers use Apache as their web server software. However, WordPress can run on other web server software as well.
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.
What is idempotency in ansible?
Idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application. This principle enables Ansible to be declarative and yet reliable is idempotence, a concept borrowed from mathematics.
Some prerequisite for the task:
- Ansible must be configured in “Controller Node”
- Ansible inventory must have the IP of the system which we will configure as a web server(i.e. Managed node).
- Managed Node must have YUM configured
Initially, Apache is not idempotent in nature. This makes apache use more memory and resources every time we run the ansible playbook to configure our web server on the managed node. Because every time we run the ansible playbook it will run the whole process from scratch as it is making it “non-idempotent’’. We can make apache idempotent by using handlers in our ansible playbook in the following manner.
What are Handlers in ansible?
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.
In our case, “httpd” and firewall services will restart only if we will make any changes on the html file or in the ansible inventory.
The below image shows the following output, if we run the playbook after making some changes in any of the html file,
But , now if run the playbook again , this time already the file is present in the specific folder of managed node , so it won’t be copied due to idempotence nature of Ansible. Here no change is made so the handler is not notified , and hence “httpd” is not restarted.
Check out the GitHub to know more about the complete codes used to make apache idempotent: https://github.com/yashraj24/Making-apache-idempotent-using-ansible
Thank you for reading!!…