Enabling Idempotence in HTTPD server using Ansible
Shubham Bhardwaj
AWS Community Builder | 3X Redhat Certified Engineer(DO180, RH294, RHCSA) | Aspiring SRE (Ops) | DevOps | Ansible? | OpenShift? | Terraform | Kubernetes | AWS | GCP | Azure | OpenStack | Jenkins | Big Data Ecosystem
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. Ansible works with Modules.
In some use-cases, Ansible lacks its idempotency. One of the situations is restarting any service using the service module. Even though the service module is idempotent in nature but if we use it to restart the service then the service will be restarted every time when this module runs. Restarting of service every time is not a good practice because service restart can take some seconds of time that can lead to some downtime of the service on the client-side. And in real production environments, we can never think of a single second of downtime.
Handlers are just like regular tasks in an Ansible playbook but will only run if the Task contains a notify directive and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler. This means services can be bounced only if they need to be restarted. Handlers can be used for things other than service restarts, but service restarts are the most common usage.
Finally we updated our webpage code. Now we want our webserver to restart.
We can see that on updating webpage code handlers automatically run as its notified by copy module as its task "changed".
Thanks for reading!!!