0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 54min

0 / 9 lessons complete

Ansible Roles

• 2hr 38min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 28min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 20min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 23min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Ansible Tags


Q&A (0)

Notes (0)

Resources (0)

Saving Progress...


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
    - name: Output message for installation
        msg: "This is the installation task"
        - install

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

    - name: Output message for restart
        msg: "This is the restart task"
        - 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.


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

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

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

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


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

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


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:

  - role: common
      - setup


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
    - 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
Notify of
profile avatar
Inline Feedbacks
View all comments