Ansible is one of the most famous tools for automating tasks. Ansible is an open source tool that is used for configuration management and application deployment. It has widely gained popularity since it’s inception in 2012.
There are various benefits of using ansible for automating tasks over any other language like python. This is because ansible provides a Resource Abstraction Layer(RAL). I won’t talk about it much today. If you would like to know more about ansible check out my article here.
Today I would like to talk about idempotence in Ansible and how we can achieve it in case of httpd setup.
What is idempotence?
One of the most interesting feature that ansible provides is the idempotence. To give a better idea of what idempotence is, let me give an example. Let’s say you have a certain set of tasks.
- You first create a folder
- Create a file and then edit that file.
- Lastly delete the directory
This is a fairly simple task. Let’s say you automate the first two tasks and later add the last task. If you again run this set of tasks there is an issue. The first two tasks have already been accomplished. So all we need to do is delete the directory. This property is called idempotence. Ansible has idempotence. This means that it can understand which tasks have already been done and skip them so that no time or resources are wasted on doing redundant tasks.
This is a very important feature because in case of scenerios where the tasks that need to be done are large, this saves up time as well as resources.
At times this also helps in avoiding errors. Let’s say you want to create a directory and the task is already done. Then if you try to redo this task the OS will throw an error because two directories can’t have the same name in the same location. This and various other problems don’t even occur when using Ansible .
So what’s the problem today?
Most of the modules have support for idempotence. But let me tell the problem we are going to deal with.
This is the task to restart the httpd services. The problem with this piece of code is that it always run whenever we run the ansible playbook. However this is against the idempotence property.
So when do we want this task to run? We want it to run whenever there is any change in the configuration of other tasks. Otherwise we can just skip this task.
So how are we supposed to do this?
Handlers in Ansible
Handlers are like functions in any other programming language. We can allow them to run according to a certain condition that we can set using the notify .
So we want the services to restart once we update the webpages when we are dealing with the httpd services.
So Copy web pages is changed only when there is any changes in the webpages. When there is a change in the webpage the notify prompts the Start services to run just like a function.
To allow this to happen we need to place the task within the handlers.
So the services are restarted only when the webpages are updated. This helps in achieving the idempotence in the ansible when some conditions do not allow them on their own.
There is not something out of the box we did here and some of the people might think that this is too much of a hassle to begin with. However this comes in very handy when there are thousands of tasks running and we are trying to save up every bit of resources we can.
Thanks : )