Level 1
0 / 100 XP

Ansible Tags

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:

Text
--- - 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…