Using Handlers in Ansible

Using Handlers in Ansible

ARTH - Task 11.3 ???????

?? Restarting HTTPD Service is not idempotence in nature and also consume more resources suggest a way to rectify this challenge in Ansible playbook

Ansible follows idempotence with most of the tasks . But we some of the tasks it does not follow it. For example while restarting httpd service it does not follow idempotence. So for such cases we use Handlers.

What are Handlers?

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.

Let's see an example-

We have a controller node and a managed node . We will copy a file e.txt from controller to managed node. The file is not present in the managed node . If run this playbook then the file will be copied and so a change is made which will notify the handler to restart httpd.

No alt text provided for this image

But , now if run the playbook again , this time already the file is present in the specific folder of managed node , so it won't be copied due to idempotence nature of Ansible. Here no change is made so the handler is not notified , and hence httpd is not restarted.

No alt text provided for this image

PLAYBOOK-

No alt text provided for this image

Hence we can use Handlers to run tasks that have to be run only when notified , so they don't run every time and consume more resources.




Adarsh Kumar

Cloud Technical Solutions Engineer @ Google

4 年

Great bro Yash Indane ??

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

Yash Indane的更多文章

  • Use of K-mean clustering in security domain

    Use of K-mean clustering in security domain

    Summer Task-10 & ARTH Task 42 Github -> What is K-means Clustering? K-means clustering is one of the simplest and…

  • OSPF Routing Protocol using Dijkastra Algorithm

    OSPF Routing Protocol using Dijkastra Algorithm

    What is OSPF? The OSPF (Open Shortest Path First) protocol is one of a family of IP Routing protocols, and is an…

  • Using Face Recognition for automation

    Using Face Recognition for automation

    SUMMER-TASK-6 (Team Task) and ARTH TASK 38 GitHub -> In this article I will explain how we can use Face Recognition and…

    3 条评论
  • JavaScript use cases in Industry

    JavaScript use cases in Industry

    Summer Task 7.2 What is JavaScript? JavaScript, often abbreviated as JS, is a programming language that conforms to the…

    1 条评论
  • Use of confusion matrix in detecting cyber crime

    Use of confusion matrix in detecting cyber crime

    Summer 2021 Task 05 ??????? Particularly in the last decade, Internet usage has been growing rapidly. However, as the…

  • Running Chrome in Docker container

    Running Chrome in Docker container

    Summer - Task 02 ??????? By default containers don't support GUI, but by some way we can achieve that, let's discuss…

    3 条评论
  • Training a ML model inside a container

    Training a ML model inside a container

    Task 01 ??????? Task Description ?? ?? Pull the Docker container image of CentOS image from DockerHub and create a new…

    4 条评论
  • Deploying WordPress in Amazon EKS with RDS in Backend

    Deploying WordPress in Amazon EKS with RDS in Backend

    ARTH-TASK-23 WordPress is a free and open-source content management system written in PHP and paired with a MySQL or…

    3 条评论
  • How industry uses MongoDB

    How industry uses MongoDB

    ARTH-TASK-32 What is MongoDB? MongoDB is a source-available cross-platform document-oriented database program…

  • Creating a Multicloud Setup of Kubernetes using Ansible Roles

    Creating a Multicloud Setup of Kubernetes using Ansible Roles

    TASK 28 Task Description ?? ?? CREATE A MULTI-CLOUD SETUP of K8S cluster: ?? Lunch node in AWS ?? Lunch node in Azure…

    2 条评论

社区洞察

其他会员也浏览了