Tags in Ansible
Suppose you have a large playbook, but for you, it is only helpful to run certain parts of it in place of running the entire playbook. In such a situation, you can use Ansible Tags.
You can use Ansible Tags to execute or skip the selected tasks. This is a two-step procedure:
- Tag your jobs individually or by tag inheritance from a block, play, role, or import.
- When you execute your playbook, you can choose to include or exclude tags.
Tags Keyword
Tags can be added to a single task or include. You can also apply tags to numerous jobs by defining them at the block, play, role, or import level. All of these use scenarios are addressed by the keyword tags.
Tags are always defined and added to tasks using the tags keyword; it does not choose or skip jobs for execution. When running a playbook, you can only pick or skip tasks based on tags at the command line.
We can give one or more tags to a single task at the most basic level. Tags can be added to tasks in playbooks, task files, or roles.
Adding Tags to Includes
-
Tags can be applied to dynamic includes in a playbook.
-
Tags on an include_* task, like tags on an individual task, apply solely to the include itself.
-
If you add mytag to a dynamic include and then run that playbook with –tags mytag, Ansible runs the include, and runs any tasks in the included file or role that are tagged with mytag, and skips any tasks in the included file or role that are not tagged with that tag.
-
Tags are added to includes in the same way that tags are added to any other task
.
-
A tag can only be added to a role's dynamic include.
We add tags to includes just like we add to any other tasks. Here’s an example.
- name: Dynamic re-use of database tasks
include_tasks: db.yml
tags: db
Here’s an example of adding tags only to the dynamic role.
---
- hosts: webservers
tasks:
- name: Include the bar role
include_role:
name: bar
tags:
- foo
Tag Inheritance
You can define tags at the level of your play or block, or when you add a role or import a file, if you wish to apply the same tag or tags to several jobs without adding a tags line to each task. Ansible assigns the tags to all child jobs down the dependency chain.
When using roles and imports, Ansible appends the tags defined by the roles section or import to any tags placed on individual tasks or blocks inside the role or imported file when using roles and imports. This is referred to as tag inheritance. Tag inheritance is advantageous since it eliminates the need to tag each job individually. However, the tags continue to apply to the jobs separately.
Adding Tags to Blocks
If you want to apply a tag to many, but not all, of the jobs in your play, you can use a block and define the tags at that level.
Adding Tags to Plays
If all of the tasks in a play should have the same tag, you can add the tag at the play level. For example, you could tag the entire play, if you had a play with only the NTP tasks:
- hosts: all
tags: ntp
tasks:
- name: Install ntp
ansible.builtin.yum:
name: ntp
state: present
- name: Configure ntp
ansible.builtin.template:
src: ntp.conf.j2
dest: /etc/ntp.conf
notify:
- restart ntpd
- name: Enable and run ntpd
ansible.builtin.service:
name: ntpd
state: started
enabled: yes
- hosts: fileservers
tags: filesharing
tasks:
...
Adding Tags to Roles
You can add Tags to roles in 3 ways:
-
Set tags under roles to apply the same tag or tags to all tasks in the role.
-
Set tags on a static import_role in your playbook to apply the same tag or tags to all tasks in the role.
- The addition of a tag or tags to individual tasks or blocks within the role is the only method that allows you to choose or skip specific activities inside the role. To pick or skip tasks within a role, you must have tags defined on specific tasks or blocks, utilize the dynamic include_role in your playbook, and include the same tag or tags.
roles:
- role: webserver
vars:
port: 5000
tags: [ web, foo ]
Adding Tags to Imports
You can additionally tag all of the tasks imported by the static import_role and import_tasks statements.
---
- hosts: webservers
tasks:
- name: Import the foo role
import_role:
name: foo
tags:
- bar
- baz
- name: Import tasks from foo.yml
import_tasks: foo.yml
tags: [ web, foo ]
Special Tags
Ansible reserves two special tags, these are for special behavior. These tags are ‘always’ and ‘never’.
If you add the always tag to a task or play, Ansible will perform it always, unless you explicitly skip it (--skip-tags always).
tasks:
- name: Print a message
ansible.builtin.debug:
msg: "Always runs"
tags:
- always
- name: Print a message
ansible.builtin.debug:
msg: "runs when you use tag1"
tags:
- tag1
If you add the never tag to a task or play, Ansible will skip it unless you explicitly request it (--tags never).
tasks:
- name: Run the rarely-used debug task
ansible.builtin.debug:
msg: '{{ showmevar }}'
tags: [ never, debug ]
Skipping or Selecting Tags
When you run ansible-playbook, you can selectively perform or skip tasks depending on their tags. Ansible runs or skips any jobs with tags that match the tags you supply at the command line. This can be done once you have already added tags to your tasks, blocks, includes, imports, roles, and plays.
If you add a tag at the block or play level, with roles, or through an import, that tag applies to all tasks within that block, play, role, or imported role or file.
There are five tag-related command line options that Ansible offers:
-
--tags all - Used to run all tasks, ignore tags, this is the default behavior.
-
--tags [tag1, tag2] - Used to run only tasks with either the tag tag1 or the tag tag2.
-
--skip-tags [tag3, tag4] - Used to run all tasks except those with either the tag tag3 or the tag tag4.
-
--tags tagged - Used to run only tasks with at least one tag.
-
--tags untagged - Used to run only tasks with no tags.
In a very long playbook, for example, to run only tasks and blocks tagged configuration and packages:
ansible-playbook example.yml --tags "configuration,packages"
To not run the tagged packages but all other tasks:
ansible-playbook example.yml --skip-tags "packages"
Preview the Results

When running a role or playbook, you may not know or recall which tasks have which tags, if any tags exist at all.
Ansible provides two command-line flags for ansible-playbook that aid in the management of tagged playbooks:
-
--list-tags - This is used to generate a list of available tags.
-
--list-tasks - This when used with --tags tag name or --skip-tags tag name, is used to generate a preview of tagged tasks.
Below are the examples to preview the tags used:
ansible-playbook example.yml --list-tags
ansible-playbook example.yml --tags "configuration,packages" --list-tasks
Frequently Asked Questions
What exactly are tags?
When dealing with a large playbook, it is sometimes more convenient to run only a portion of it than the full thing. That's why tags exist.
In terms of tags, how do you filter out tasks?
Tasks can be filtered in one of two ways: (i) On the command line, use the -tags or -skip-tags arguments. (ii)Use the TAGS_RUN and TAGS_SKIP parameters in Ansible configuration settings.
How to set up Ansible?
To set up Ansible, you can use Python installer or a Linux-based installation process, like apt or yum.
What is a "playbook" in Ansible?
A playbook is made up of YAML-based files that use scripts to transmit commands to distant machines. Rather than utilizing individual commands to setup computers remotely via the command line, developers can create entire complicated environments by delivering a script to the needed systems. Playbooks are the tool's building blocks.
What are Ansible server requirements?
A virtual machine with Linux installed, and Python version 2.6 or higher is required.
Conclusion
In this article, we had a detailed look at Ansible Tags. We started with an introduction to Ansible playbooks which are the building blocks of this technology. We then discussed tags in detail and how we could use them. This article also explains the special tags in Ansible and command line options for skipping and selecting tags.
To learn more about Ansible and its services, refer to the following blogs:
Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.
Happy Learning!