Ansible Playbook Tags

From bib. source

Tags allow you to run (or exclude) subsets of a playbook’s tasks.

You can tag roles, included files, individual tasks, and even entire plays. The syntax is simple […]

That is, tags are names, or a list of names (in YAML syntax), for the tags field across roles, files, tasks or plays, which allow those things to be grouped by the designated tag names that are shared among them (Geerling 2023, 118). An example of this is the following (Ibid):

---
# You can apply tags to an entire play
- hosts: webservers
  tags: deploy
 
  roles:
    # Tags applied to a role will be applied to tasks in the role.
    - role: tomcat
      tags: ['tomcat', 'app']
  
  tasks:
    - name: Notify on completion
      local_action:
        module: osx_say
        msg: "{{inventory_hostname}} is finished!"
        voice: Zarvox
      tags:
        - notifications
        - say

More on local_action

For more on the module local_action as seen in the example, see 20250518153837-Delegation_in_Ansible.

Then, the tags can be made use of to run only particular groups or sets of tasks, plays, roles or files via command line interface (Geerling 2023, 118):

ansible-playbook tags.yml --tags "tomcat,say"

It can also be used to exclude certain tasks, plays, roles or files (Geerling 2023, 118-119):

ansible-playbook tags.yml --skip-tags "notifications"

data_type data_types array delegation role file task fields configuration_management command_line command_lines command_line_interface command_line_interfaces ad-hoc_command ad-hoc_commands


bibliography

  • “Ansible Playbooks - Beyond the Basics.” In Ansible for DevOps: Server and Configuration Management for Humans, 2nd ed., 84–122. Leanpub, 2023.