0%

0/1 Lessons

Course Introduction

• 16min

0 / 3 lessons complete

IT Lab Setup

• 20min

0 / 3 lessons complete

Installing Ansible

• 35min

0 / 3 lessons complete

Managing your Ansible Inventory

• 2hr 7min

0 / 8 lessons complete

Ansible Basics

• 1hr 43min

0 / 9 lessons complete

Ansible Roles

• 1hr 46min

0 / 8 lessons complete

Ansible Galaxy

• 1hr 33min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 1hr 51min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 1hr 41min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Ansible Tags

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, you will first learn how to execute tasks in an Ansible playbook by specifying a single tag. Then, you will learn how to run tasks with multiple tags. We'll use a playbook named tags-test.yml for these examples, which includes three different tasks, each tagged differently.

Understanding Tags in Ansible

Before diving into the examples, let's briefly explain what a tag is in Ansible. Tags are identifiers assigned to tasks within a playbook, allowing for selective execution of these tasks. When you run a playbook, you can specify one or more tags, and only the tasks that have those tags will be executed. This is particularly useful for running a subset of tasks in a large playbook.

Playbook Example: "tags-test.yml"

Let's create a new playbook named tag-test.yml:

nano ~/tags-test.yml

Here's our sample playbook tags-test.yml, which includes three tasks with unique tags:

---
- hosts: managed-node-1
  tasks:
    - name: Output message for installation
      debug:
        msg: "This is the installation task"
      tags: 
        - install

    - name: Output message for configuration
      debug:
        msg: "This is the configuration task"
      tags: 
        - configure

    - name: Output message for restart
      debug:
        msg: "This is the restart task"
      tags: 
        - restart

Executing a Single Tag

To run tasks associated with a specific tag, use the ansible-playbook command with the --tags option followed by the tag name. For example, to execute only the task tagged as configure, the command would be:

ansible-playbook tags-test.yml --tags configure

This command ensures that only the task with the configure tag gets executed. Tasks tagged with install or restart will be skipped.

Executing Multiple Tags

You can also specify multiple tags in a single playbook run. This allows you to execute tasks that have any of the specified tags. For example, if you want to run tasks tagged with both install and restart, you would use the following command:

ansible-playbook tags-test.yml --tags install,restart

With this command, Ansible will execute tasks tagged with either install or restart. The task tagged with configure will not be executed in this scenario.

Excluding Tags

You can also exclude tags which might be easier to do if you want to run everything except a few tags. This can be done like so:

ansible-playbook tags-test.yml --skip-tags configure

Previewing Tagged Tasks

Before running a playbook, you can preview which tasks will run based on specified tags:

ansible-playbook tags-test.yml --list-tags

Preview tasks for specific tags:

ansible-playbook tags-test.yml --tags "configure" --list-tasks

Where can I apply tags?

Apart from individual tasks, Ansible allows you to apply tags to a group of tasks using blocks, entire plays, roles, or even at the import level. This concept is known as tag inheritance.

Blocks

A block groups several tasks together and allows you to tag all of them at once. For instance:

- name: Configure NTP
  block:
    - name: Install NTP
      yum:
        name: ntp
        state: present

    - name: Configure NTP
      template:
        src: ntp.conf.j2
        dest: /etc/ntp.conf

    - name: Start NTP Service
      service:
        name: ntpd
        state: started
  tags: 
    - ntp

Plays

You can also apply tags at the play level, which will apply the tag to all tasks within the play:

- hosts: all
  tags: 
    - network
  tasks:
    - name: Install network utilities
      yum:
        name: net-tools
        state: present

Roles

When using roles, you can tag all tasks within a role either by tagging each task within the role or by applying tags to the role inclusion:

roles:
  - role: common
    tags: 
      - setup

Imports

For static imports using import_role or import_tasks, tags can be applied to the import statement:

- name: Import common tasks
  import_tasks: common.yml
  tags: 
    - common

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments