Restarting HTTPD Service is not idempotent in nature and also consume more resources suggest a way to rectify this challenge in Ansible Playbook.

Restarting HTTPD Service is not idempotent in nature and also consume more resources suggest a way to rectify this challenge in Ansible Playbook.

What is Idempotent nature in Ansible?

An operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any intervening actions. Ansible follows idempotent nature but some modules don’t support idempotent behaviour like httpd module.

Idempotent Playbooks

When a playbook is executed to configure a system, the system should always have the same, well defined state. If a playbook consists of 10 steps, and the system deviates in step 4 from the desired state, then only this particular step should be applied.

By its nature, Ansible tasks will only change the system if there is something to do. Most Ansible modules provide this idempotency. But some modules can be used in a way that breaks this pattern.

For example: When we run playbook to configure httpd apache webserver, there the httpd service will always start no matter if it is already started in target node. This means that it doesn’t allow idempotancy.

The problem of idempotancy we need to rectify.

To rectify this issue either we can use the when condition which allows to start the service when the condition matches (condition : if httpd listen port changes to other port number) or can use handlers and notify concept which states that when the particular task done the notify will trigger the handlers block of code.

Considering you know how to step up Ansible inventory file, config file and check the connectivity between between control node and managed node

Let’s look into playbook code:-

No alt text provided for this image

Here the playbook will run on the same system where Ansible configured that is the target node will be localhost itself.

Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are 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.

The playbook do the following tasks:

  1. Install the httpd package.
  2. Copy the content into the destination folder (/var/www/html/index.html) and this task uses notify which will trigger the handlers when something change in the content of file and then only handlers will restart the httpd service.
  3. Enabling the port number for httpd which firewall will allow.

Here I use simple example to show the idempotent nature for httpd module. But you can use different files to upload like httpd conf file and use jinja concept and make the code more dynamic like asking file name and port number for webserver at runtime of playbook. Instead of Copy module can use template module.

When content of the file is not change, then handlers task will not execute.

No alt text provided for this image
No alt text provided for this image

When content of file changed then the notify will trigger handlers task.

No alt text provided for this image

Content of file changed

No alt text provided for this image

Client can access newer data

The idempotent issue is solved by using handlers and now httpd will support idempotancy nature also.

Thanks!!





要查看或添加评论,请登录

Akanksha Bhatt的更多文章

  • Task-8 Automation with Python

    Task-8 Automation with Python

    ARTH - Task 8 ??????? Task Description?? ?? Create a Menu Using Python integrating all the concepts that have been…

  • Task-14.3

    Task-14.3

    ???????? ?????????????????????? : Create an Ansible Playbook which will dynamically load the variable file named same…

  • Task-14.2

    Task-14.2

    Ansible playbook that will retrieve new Container IP and dynamically update the inventory and Configure web-server…

  • Task-14.1

    Task-14.1

    Task description :- 14.1 Create a network Topology Setup in such a way so that System A can ping to two Systems System…

  • LOAD BALANCING WITH HAPROXY

    LOAD BALANCING WITH HAPROXY

    Task Description 12.1 Use Ansible playbook to Configure Reverse Proxy i.

    1 条评论
  • ARTH TASK-15

    ARTH TASK-15

    Task Description?? Create an ansible role myapache to configure Httpd WebServer. Create another ansible role…

    1 条评论
  • Neural Networks in Netflix

    Neural Networks in Netflix

    What are Neural Networks? Neural networks are a set of algorithms, they are designed to mimic the human brain, that is…

  • CONFIGURING HADOOP CLUSTER USING ANSIBLE

    CONFIGURING HADOOP CLUSTER USING ANSIBLE

    Welcome you all, in this blog I will cover how we can“ CONFIGURE HADOOP CLUSTER USING ANSIBLE” Before starting the…

    1 条评论
  • How Industries are Solving Challenges Using Ansible??

    How Industries are Solving Challenges Using Ansible??

    What Is Ansible ? ?? Ansible is a software tool that provides simple but powerful automation for cross-platform…

  • Configuration of Apache Web Server in Docker Container using Ansible

    Configuration of Apache Web Server in Docker Container using Ansible

    In this article we will perform various tasks: Configure Docker Start and enable Docker Services Pull the httpd server…

    1 条评论

社区洞察

其他会员也浏览了