Skip to main content

Working with Tags

1. Adding Task Tags

Property Values Description
tags list List of tags to apply to a task

Example: Adding tags to a task

- name: "Install Apache"
  apt:
    name: apache2
    state: present
  tags:
    - web
    - http

Example: Using tags with a loop

- name: "Create directories"
  file:
    path: "/var/www/{{ item }}"
    state: directory
  loop:
    - site1
    - site2
    - site3
  tags:
    - web
    - directories

2. Running Specific Tags

Option Description Usage
--tags Run only tasks tagged with specified tags ansible-playbook site.yml --tags "web"
--skip-tags Skip tasks tagged with specified tags ansible-playbook site.yml --skip-tags "db"

Example: Running Playbook with Specific Tags

ansible-playbook site.yml --tags "apache,nginx"

Example: Skipping Tags

ansible-playbook site.yml --skip-tags "database"
Note: Tags are defined in the playbook using tags: directive on tasks.

3. Skipping Tags

Option Values Description
--skip-tags string Skip tasks tagged with the specified tags
--tags string Only run tasks tagged with the specified tags

Example: Skipping Tags During Playbook Run

ansible-playbook site.yml --skip-tags "db_backup"

Example: Including Specific Tags Only

ansible-playbook site.yml --tags "web"
Note: Tags can be applied to tasks, handlers, and playbooks. Use --skip-tags to exclude specific tags from execution.

4. Listing Available Tags

Command Description Example
ansible-playbook --list-tags List all tags in a playbook ansible-playbook site.yml --list-tags
ansible-playbook --list-tags -t tag1,tag2 List tags for specific tags only ansible-playbook site.yml --list-tags -t web,db
ansible-playbook --list-tags --tags all List all tags including those not used in the current run ansible-playbook site.yml --list-tags --tags all

Example: Listing Tags in a Playbook

ansible-playbook site.yml --list-tags

This command will output a list of all tags defined in the playbook, grouped by play and task.

5. Using Multiple Tags

Tag Description Usage
tag1, tag2 Multiple tags applied to a task or play tags: [tag1, tag2]
tag1 Single tag applied to a task or play tags: tag1

Example: Using Multiple Tags

---
- hosts: all
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
      tags:
        - web
        - apache

    - name: Install MySQL
      apt:
        name: mysql-server
        state: present
      tags:
        - db
        - mysql

    - name: Configure Firewall
      ufw:
        rule: allow
        port: 80
      tags:
        - web
        - firewall

Example: Running Playbooks with Multiple Tags

ansible-playbook site.yml --tags "web,mysql"
Note: When using multiple tags, Ansible will execute tasks that match any of the specified tags.

6. Using Special Tags

Tag Description Usage
always Runs the task unconditionally tags: always
never Skips the task entirely tags: never
untagged Runs tasks that do not have any tags tags: untagged

Example: Using always tag

- name: "Always run this task"
  command: echo "This runs every time"
  tags:
    - always

Example: Using never tag

- name: "Skip this task"
  command: echo "This never runs"
  tags:
    - never
Note: The always and never tags override any other tag filtering.

Example: Using untagged tag

- name: "Run only untagged tasks"
  command: echo "This runs if no tags are set"
  tags:
    - untagged

7. Tagging Plays

Property Description Default
tags List of tags to apply to a play None
skip_tags List of tags to exclude from execution None

Example: Basic Play with Tags

---
- name: Example Play with Tags
  hosts: all
  tags:
    - web
    - production
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

Example: Skip Tags in Play

---
- name: Example Play with Skip Tags
  hosts: all
  skip_tags:
    - database
  tasks:
    - name: Update System Packages
      apt:
        update_cache: yes
Note: Tags can be used to selectively run or skip plays and tasks during execution.

8. Tagging Roles

Property Description Example
tags List of tags to apply to a role tags: [web, production]
skip_tags Tags to exclude when running playbooks skip_tags: [database]

Example: Using Tags in Roles

---
- hosts: all
  roles:
    - role: webserver
      tags:
        - web
        - frontend
    - role: database
      tags:
        - db
        - backend

Example: Skipping Tags

ansible-playbook site.yml --skip-tags "db"

9. Tagging Blocks

Property Description Values
tags List of tags to apply to a task or block ['tag1', 'tag2']
when Conditional execution based on tags tags is defined and 'tag1' in tags

Example: Using tags to control task execution

- name: Run only on web servers
  hosts: web_servers
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
      tags:
        - web
        - nginx

    - name: Configure firewall
      ufw:
        rule: allow
        port: 80
      tags:
        - firewall
        - web

Example: Conditional execution using tags

- name: Conditional task execution
  hosts: all
  tasks:
    - name: Task A
      debug:
        msg: "Task A executed"
      tags:
        - task_a

    - name: Task B
      debug:
        msg: "Task B executed"
      when: tags is defined and 'task_a' in tags

10. Understanding Tag Inheritance

Tag Scope Description Behavior
Host-level tags Tags assigned directly to individual hosts Applied to all plays targeting that host
Group-level tags Tags assigned to host groups Inherited by all hosts in the group
Play-level tags Tags defined at the play level Applied to all tasks in that play
Task-level tags Tags defined directly on tasks Applied only to that specific task

Example: Tag Inheritance in Playbook

---
- hosts: webservers
  tags:
    - web
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
      tags:
        - install
        - apache

    - name: Start Apache
      systemd:
        name: apache2
        state: started
      tags:
        - start
        - apache
Note: Tags are inherited in order of scope: host > group > play > task. A task can override inherited tags with its own.

11. Using Tag Patterns

Pattern Description Example
tag1,tag2 Match hosts with either tag ansible-playbook site.yml --tags "web,db"
tag1,tag2,tag3 Match hosts with any of the tags ansible-playbook site.yml --tags "web,db,cache"
tag1:tag2 Match hosts with both tags (AND) ansible-playbook site.yml --tags "web:db"
!tag1 Exclude hosts with tag ansible-playbook site.yml --tags "!skip"
tag1,!tag2 Include tag1, exclude tag2 ansible-playbook site.yml --tags "web,!maintenance"
* Match all tags (default) ansible-playbook site.yml --tags "*"

Example: Using Tag Patterns in Playbook

---
- hosts: all
  tasks:
    - name: Install web server
      apt:
        name: nginx
        state: present
      tags:
        - web
        - install

    - name: Configure database
      template:
        src: db.conf.j2
        dest: /etc/db.conf
      tags:
        - db
        - config

    - name: Update cache
      command: /usr/bin/update-cache.sh
      tags:
        - cache
        - maintenance

Example: Running Playbook with Tag Patterns

# Run only web and db tasks
ansible-playbook site.yml --tags "web,db"

# Run all tasks except maintenance
ansible-playbook site.yml --tags "!maintenance"

# Run only web tasks, but not cache tasks
ansible-playbook site.yml --tags "web,!cache"

# Run all tasks (default)
ansible-playbook site.yml --tags "*"
Note: Tag patterns can be combined with other Ansible options like --limit to further refine which hosts are targeted.

Was this article helpful?