Ansible Tags

Overview

Simply enough, Ansible Tags let you run specific tasks in a play. If you have a lengthy playbook or are testing tasks within a playbook, you can assign tags to tasks that let you run a specific task vs the entire playbook.

This is simply a summary of the uses of Ansible Tags. More of a cheat sheet than trying to instruct you in how to use Ansible Tags. The Ansible Tags Documentation is fairly short and does a good job explaining how to use Ansible Tags.

Uses

Examples

$ ansible-playbook -i inventory dns-update.yaml --tags bind9               # only run tasks tagged with bind9
$ ansible-playbook -i inventory dns-update.yaml --skip-tags bind9          # run all tasks except the ones tagged with bind9
$ ansible-playbook -i inventory dns-update.yaml --tags "bind9,restart"     # run tasks tagged with bind9 and restart
$ ansible-playbook -i inventory dns-update.yaml --tags untagged            # only run untagged tasks
$ ansible-playbook -i inventory dns-update.yaml --tags tagged              # only run tagged tasks
# ansible-playbook -i inventory dns-update.yaml --tags all                 # run all tasks (default)

You can assign a tag to one or more tasks.

Tasks can have multiple tags.

When you create a block of tasks, you can assign a tag to that block and all tasks within the block are run when the tag is used.

An interesting idea might be to add a debug tag to all the debug statements in your playbooks and then when ready to run live, pass the –skip-tags debug flag to the playbook. Then only the tasks are executed.

Special Tags

If you assign an always tag to a task, it will always run no matter what the passed –tags value is unless you specially pass –skip-tags always.

If you assign a never tag to a task, it will not run unless you call it out specifically. Something like calling the playbook with –tags all,never.

Tag Inheritance

There are two types of statements that add tasks. A Dynamic include_role, include_tasks, and include_vars, and a Static import_role and import_tasks.

If you tag a task that contains an include_role or include_tasks function, only tasks within that included file that are similarly tagged will run when the tag is passed.

If you tag a task that contains an import_role or import_tasks function, all tasks within that imported file will be run when the tag is passed.

Listing Tags

By using the –list-tags option to ansible-playbooks, it lists all the tags and exits the playbook without running anything.

References

There are several sites that provide information on tags but the obvious one is the Ansible Documentation

This entry was posted in ansible, Computers and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *