Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency.
What is idempotency?
The principle that enables Ansible to be declarative and yet reliable is idempotence, a concept borrowed from mathematics. An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application, such as multiplication by zero.
In lay-man words, idempotence is the concept of re-doing an operation without bringing a change in it. Like you multiply a number with 0,will always give you the same result no matter how many times the action has been performed.
Restarting the service!
Started/Stopped are idempotent actions that will not run commands unless necessary, i.e if a service is already running, then ansible will skip this task of starting a service and not consume CPU time same applies to a stopped service,but restarted will always restart a service even if changes were not made.
For eg- Consider a basic playbook for setting up HTTPD in the target node.
Now when you run the service
Certain changes take place as shown in the picture, but when you again run the playbook (ansible-playbook <filename>.yml).
Again changes are made on the same playbook for some reason, hence it contradicts idempotancy.
To solve this problem HANDLERS are used.
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.
As it is clearly visible that no change takes place, hence handlers are useful for maintaining idempotancy in ansible!