Handlers-Ansible

In Ansible, handlers are special tasks that run only when notified by other tasks. They are typically used for operations that need to occur after a change has been made, such as restarting a service after its configuration file has been modified.

Defining and Using Handlers

  1. Define a Handler: Handlers are defined in a playbook similar to regular tasks but are listed under the handlers section. Each handler has a unique name that other tasks can use to reference it.

handlers:
  - name: restart apache
    service:
      name: httpd
      state: restarted        

Notify a Handler: Tasks that can trigger handlers include a notify directive, specifying which handlers to trigger when the task reports a changed status.

tasks:
  - name: copy the httpd configuration file
    copy:
      src: /path/to/httpd.conf
      dest: /etc/httpd/conf/httpd.conf
    notify: restart apache        

Example Playbook with Handlers

Here's a complete example demonstrating the use of handlers in a playbook:

---
- hosts: webservers
  become: yes

  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: copy the httpd configuration file
      copy:
        src: /path/to/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
      notify: restart apache

    - name: start httpd service
      service:
        name: httpd
        state: started

  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted        

Key Points

  1. Handlers Only Run When Notified: Handlers are only executed if they are notified by a task that has changed something. If no tasks notify the handler, it will not run.
  2. Handlers Run Once per Playbook: Even if multiple tasks notify the same handler, it will only run once at the end of the play. This ensures that operations such as service restarts are performed efficiently.
  3. Notification Names Must Match: The name specified in the notify directive must exactly match the name of the handler.
  4. Handlers Are Run in the Order of Notification: If multiple handlers are notified, they will run in the order they were defined.

Thank You for reading.


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

Manasee Suuba的更多文章

  • AWS IAM (Identity and Access Management)

    AWS IAM (Identity and Access Management)

    AWS IAM is a service that enables you to manage access to AWS resources securely. It controls who (users, roles, or…

  • AWS Instance

    AWS Instance

    In AWS (Amazon Web Services), an instance refers to a virtual server that runs applications on the AWS infrastructure…

  • VPC

    VPC

    A VPC (Virtual Private Cloud) in AWS is a logically isolated section of the AWS cloud where you can launch AWS…

  • IAM

    IAM

    AWS IAM (Identity and Access Management) is a web service that helps you securely control access to AWS services and…

  • S3 bucket

    S3 bucket

    Amazon S3 (Simple Storage Service) is a scalable and durable object storage service provided by AWS. S3 Buckets are…

  • Cloudfront

    Cloudfront

    Amazon CloudFront is a content delivery network (CDN) service provided by AWS. It helps deliver content (web pages…

  • Cloudwatch

    Cloudwatch

    Amazon CloudWatch is a monitoring and observability service provided by AWS that helps you track the performance and…

  • CIDR Block

    CIDR Block

    A CIDR block (Classless Inter-Domain Routing block) is a range of IP addresses that is defined by a base IP address and…

  • Creating a project that integrates Terraform and Ansible can be a great way to automate both the provisioning and configuration of infrastructure.

    Creating a project that integrates Terraform and Ansible can be a great way to automate both the provisioning and configuration of infrastructure.

    Project Overview Terraform: Used to provision an EC2 instance in AWS. Ansible: Used to configure the EC2 instance by…

  • Creating infrastructure on AWS using Terraform involves several steps, from setting up Terraform to defining and deploying resources

    Creating infrastructure on AWS using Terraform involves several steps, from setting up Terraform to defining and deploying resources

    1. Install Terraform Download Terraform from the official website.

社区洞察

其他会员也浏览了