Resolving restart httpd service challenge using Ansible.
Priyanka Bharti
Software Engineer @ Samsung | C++ | Android Development | Kotlin | Linux
In automating httpd service using Ansible, we usually restart the service whether we make any changes or not. Restarting httpd service without any reason is not good and also it consumes a lots of time. In this article, you'll come to know how to solve this challenge.
Before moving to the task, let's have a basic knowledge about the tools.
What is Ansible?
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Being simple, secured, agentless and powerful, it's used widely for software automation in IT industry, becoming a exclusive configuration Management tool to use in this space.
To know more about ansible and how it works, do visit my blog: https://priyankaa2023.medium.com/automation-in-it-industry-and-ansible-381f45bd0924
What is Apache Webserver?
The Apache HTTP Webserver, colloquially called Apache, is a free and open-source cross-platform web server software that delivers web content through the internet. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.
Task: Restarting httpd service is not 'idempotence' in nature and also consumes lots of time. How to resolve this using Ansible?
Ansible provides a module, 'handlers', which can be used in the playbook to solve the challenge.
What is Ansible Handlers?
Handlers are tasks in Ansible that, unlike other tasks, run only when notified. Handlers uses a keyword 'notify' to notify the handlers when there is any change in the part of task for which service need to be restarted. 'notify' is a pre-internal keyword in 'tasks' module and can be used anywhere or in any module in Ansible.
Ansible playbook that will configure apache httpd webserver on target nodes with the help of handlers:
- hosts: webserver tasks: - name: "httpd software installation" package: name: "httpd" state: present - name: "Creating web pages" copy: src: "index.html" dest: "/var/www/html/index.html" notify: restart service handlers: - name: restart service service: name: "httpd" state: restarted enabled: yes register: restart - debug: var: restart.stdout - firewalld: port: "80/tcp" state: enabled permanent: yes
Now, as we run the playbook for the first time on a target node (consider ip 192.168.43.64) to be configured for web server, the output will be:
You can see how handler run and performed the restart service. Here, handler run only when it gets notified by notify keyword. And notify will notify handler only when changes made in the web pages of the web service.
If we don't make any changes in the web pages, there is no need to restart the web service. Let's rerun the playbook without making any change in web pages, we will see the output as:
Yeah...!!! This time handler didn't run. So, we succeed in making restart service idempotence using Ansible..!! You can recheck again by making changes in web pages and running the playbook again.
Thanks for reading :)