Day 63 of 90 Days of DevOps Challenge: Ansible Handlers, Tags, and Variables

Vaishnavi DVaishnavi D
4 min read

Yesterday, I focused on mastering the YAML syntax, the backbone of Ansible Playbooks, and practiced writing structured playbooks to automate various tasks such as pinging managed nodes, creating files, copying content to files, and installing and configuring services like Apache (httpd)

I also explored important playbook commands like syntax checks, listing target hosts, step-wise execution, and verbose output options, which help ensure the correctness and clarity of playbooks.

Today, I advanced my Ansible automation skills by learning about Handlers, Tags, and Variables, which are essential for creating dynamic and efficient playbooks.

Handlers & Tags

By default, all tasks in an Ansible playbook execute sequentially from top to bottom. However, in real-world automation scenarios, some tasks should only run conditionally, specifically when a preceding task leads to a change in state. This is where Handlers come into play.

Handlers

Handlers are tasks that only execute when explicitly triggered by another task.

  • The notify directive is used within a task to call a handler when that task reports a changed state.

This approach is efficient for tasks like restarting a service only after a configuration file has been modified, avoiding unnecessary restarts.

Example Scenario:
If Task 2 updates a configuration file, the handler can be notified to restart the service in Task 3 — but only if Task 2 caused a change.

Tags

  • Tags allow you to associate specific identifiers with tasks in a playbook.

  • This enables selective execution of tasks within a playbook, offering more control during playbook runs.

Useful Tag Commands:

# List all tags in a playbook
ansible-playbook handlers_tags.yml --list-tags

# Execute tasks tagged with 'install'
ansible-playbook handlers_tags.yml --tags "install"

# Execute tasks tagged with both 'install' and 'copy'
ansible-playbook handlers_tags.yml --tags "install,copy"

# Skip tasks tagged with 'install' and 'copy'
ansible-playbook handlers_tags.yml --skip-tags "install,copy"

Variables in Ansible

Variables in Ansible provide a dynamic way to store and reuse data in a key-value format. This flexibility allows playbooks to adapt to different environments, hosts, and configurations without hardcoding values.

Basic Example of Key-Value Variables

id: 100
name: meena
age: 20
gender: Female

Types of Variables in Ansible

  1. Runtime Variables: Passed at playbook execution time.

  2. Playbook Variables: Defined directly within the playbook.

  3. Group Variables: Assigned to a group of hosts.

  4. Host Variables: Defined for specific hosts.

1) Runtime Variables

These are supplied during playbook execution using the --extra-vars flag.

Example Playbook:

---
- hosts: webservers
  become: true
  tasks:
    - name: Install package
      yum:
        name: "{{ package_name }}"
        state: latest

Run Command:

ansible-playbook install_package.yml --extra-vars package_name=httpd

2) Playbook Variables

Variables can be defined within the playbook itself using the vars section.

Example:

---
- hosts: webservers
  become: true
  vars:
    package_name: httpd
  tasks:
    - name: Install package
      yum:
        name: "{{ package_name }}"
        state: latest

3) Group Variables

Group variables are defined per host group and stored in the group_vars directory relative to the inventory file.

Inventory Example (/etc/ansible/hosts):

[webservers]
webserver1 ansible_host=172.31.0.95
webserver2 ansible_host=172.31.0.96

[dbservers]
172.31.5.185
172.31.5.186

Group Variables Directory:

/etc/ansible/group_vars/webservers.yml
/etc/ansible/group_vars/dbservers.yml

webservers.yml:

package_name: java

dbservers.yml:

package_name: mysql

Using this, a single playbook can dynamically install different packages on respective server groups.

4) Host Variables

Host-specific variables are stored in the host_vars directory.

Location:

/etc/ansible/host_vars/

Example Structure:

/etc/ansible/host_vars/webserver1.yml
/etc/ansible/host_vars/webserver2.yml

webserver1.yml:

package_name: java

webserver2.yml:

package_name: python

Precedence of Variables

  1. Playbook-defined variables override

  2. Host variables, which override

  3. Group variables

This hierarchy ensures the right variable is applied at the correct level of granularity.

Final Thoughts

Today’s session helped me understand how to make Ansible playbooks more intelligent and adaptable using Handlers, Tags, and Variables. These features not only improve automation precision but also help maintain clean, efficient, and maintainable infrastructure code.

Next, I’ll be exploring Ansible Conditionals, Loops, and Facts, which will enable even more powerful and flexible automation workflows.

Stay tuned for Day 64 as I continue building smarter automation pipelines with Ansible!

0
Subscribe to my newsletter

Read articles from Vaishnavi D directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vaishnavi D
Vaishnavi D