Wednesday, February 5, 2025

The Difference between Ansible Tasks and Handlers.

 

Double-click on the image to zoom-out ...Larger.

To return to Home page: Refresh Page or Take ESC Button on Keyboard

 The difference between tasks and handlers with reference to Ansible playbook

In Ansible playbooks, tasks and handlers serve distinct purposes:

  1. Tasks:

    • Tasks are the basic units of work in an Ansible playbook. They define the actions that should be executed on the managed nodes.
    • Each task specifies a module (e.g., apt, yum, copy, file, etc.) and the parameters required for that module to perform the desired operation.
    • Tasks run sequentially, and they are always executed when the playbook is run, unless conditions such as when statements or changed_when are used to control execution.

    Example:

    # yaml

    - name: Install a package apt: name: vim state: present
  2. Handlers:

    • Handlers are special types of tasks that are only executed when notified by other tasks.
    • A handler is typically used for tasks that should only run if something has changed, such as restarting a service after a configuration file has been modified.
    • Handlers are defined in the handlers section of a playbook and are triggered by other tasks using the notify directive.

    Example:

    # yaml

    - name: Restart nginx service: name: nginx state: restarted when: nginx_config_changed handlers: - name: Restart nginx service: name: nginx state: restarted

Key Differences:

  • Execution: Tasks always run when the playbook is executed, 
  • whereas, handlers run only when they are notified by other tasks.
  • Purpose: Tasks define the main actions to be performed (e.g., install software), 
  • Meanwhile, handlers typically perform actions triggered by changes (e.g., restart a service after a configuration change).

No comments:

Post a Comment

Amazon DynamoDB Global Tables | Deep Dive.

A deep into  Amazon  DynamoDB Global Tables . Think of this as an “architect’s + operator’s ”  View:  How they work,  Why they exist,  Subt...