Restarting HTTPD Service is not idempotence in nature and also consume more resources suggest a way to rectify this challenge in Ansible playbook
To solve this problem, we can use Handlers
Handlers in ansible to be triggered only when there are some changes occur in particular file (any file, can be configuration file also) and then handler will do the task accordingly
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.
If you run the playbook again then it will shows that Your service is started so no need the restart again this become possible because of the handlers and notify keyword in ansible.
Creating a playbook to configure yum and intalling httpd package, wrting the webpage and to start httpd services
Runing the Playbook:
Giving the document root name as /var/www/main and port no as 84
Again running the same playbook with same document root name and port no; as there is no changes in configuration file of httpd so no need to restart the services (handler will do this for us)
Thank you for reading!