If you have a large playbook, it becomes useful to be able to run only a specific part of it rather than running everything in the playbook. Ansible supports a tag attribute for this reason.
When you apply tags on things, then you can control whether they are executed by adding command-line options.
When you execute a playbook, you can filter tasks based on the tags in two ways, such as:
In Ansible, tags can be applied to many structures, but its simplest use is with individual tasks. Let's see an example that tags two tasks with different tags, such as:
If you want to run the configuration and packages part of a very long playbook, then you can use the -tags option on the command line.
And if you want to run a playbook without certain tagged tasks, then you can use the -skip-tags command-line option.
We can apply the same tag to more than one task. By using the "--tags" command line options, all tasks with that tag name will be run.
For example: In below example, we use one tag "ntp" for several tasks, such as:
"always" is a unique tag that will always run a task, unless specifically skipped (--skip-tags always)
New in version 2.5
Here is another unique tag that is "never" which prevents a task from running unless a tag is specifically requested.
In the above example, the task will only run when the "never" or "debug" tag is explicitly requested.
Here are another three special keywords for tags:
"tagged" which run only tagged,
"untagged" which run only untagged, and
"all" which run all tasks respectively.
By default, Ansible runs as if "--tags" all had been specified.