Ansible Tags: Run Specific Tasks Selectively (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: installation
Complete guide to Ansible tags. Run specific tasks with --tags, skip tasks, tag roles and includes, and organize playbooks with tagging strategies.
Ansible tags are a feature that allows users to selectively control which tasks in a playbook are executed. They are an essential tool for optimizing automation workflows, especially in complex playbooks. This article explains what Ansible tags are, their usage, and best practices for implementing them.
What Are Ansible Tags?
Tags in Ansible are labels assigned to tasks or plays within a playbook. They enable users to execute specific parts of a playbook, skipping others based on the tags provided during execution.
Key Features:
- Selective Execution: Run only tasks with specified tags.
- Improved Efficiency: Save time by skipping unnecessary tasks.
- Flexibility: Apply multiple tags to a single task or play.
How Do Ansible Tags Work?
Tags are added to tasks, roles, or entire plays using the tags keyword. During playbook execution, you can specify which tags to include or skip using command-line options.
Example: Using Tags in a Playbook
- name: Configure web servers
hosts: webservers
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
tags:
- install
- web
- name: Start Nginx service
service:
name: nginx
state: started
tags:
- start
- webIn this example:
- The
installtag applies to the Nginx installation task. - The
starttag applies to the task starting the Nginx service. - Both tasks share the
webtag.
Running Tagged Tasks
To execute only tasks with a specific tag, use the --tags option:
ansible-playbook playbook.yml --tags installSkipping Tags
To skip tasks with specific tags, use the --skip-tags option:
ansible-playbook playbook.yml --skip-tags startSee also: Can Ansible Be Used to Manage Windows Systems?
Tagging Roles and Plays
Tags can also be applied to roles and entire plays for broader control.
Example: Tagging Roles
- hosts: all
roles:
- role: webserver
tags:
- setupExample: Tagging Plays
- name: Database setup
hosts: dbservers
tags:
- databaseUse Cases for Ansible Tags
- Selective Testing:
- Environment-Specific Tasks:
- Debugging:
- Partial Updates:
See also: Can Ansible Manage Windows? Complete Windows Automation Guide
Best Practices for Using Tags
- Use Descriptive Tags:
install, deploy, restart).
- Organize Tags:
- Avoid Overuse:
- Document Tags:
- Test Tagged Tasks:
Limitations of Tags
- Dependency Issues: Tasks that depend on others may fail if skipped.
- Complexity: Overusing tags can complicate playbooks and execution workflows.
Conclusion
Ansible tags provide a powerful way to control playbook execution, enabling selective task runs and improving efficiency. By implementing tags strategically and following best practices, you can streamline your automation workflows and save valuable time.
See also: Ansible on Windows: Complete Guide to Windows Automation (2026)
Basic Tags
tasks:
- name: Install packages
ansible.builtin.package:
name: nginx
tags: install
- name: Configure nginx
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
tags: configure
- name: Start service
ansible.builtin.service:
name: nginx
state: started
tags: serviceRun or Skip Tags
# Run only tagged tasks
ansible-playbook site.yml --tags "install"
ansible-playbook site.yml --tags "install,configure"
# Skip specific tags
ansible-playbook site.yml --skip-tags "configure"
# List all tags in playbook
ansible-playbook site.yml --list-tagsMultiple Tags Per Task
- name: Deploy app config
ansible.builtin.template:
src: config.j2
dest: /etc/myapp/config.yml
tags:
- configure
- deploy
- myappTag Roles
- hosts: webservers
roles:
- { role: common, tags: common }
- { role: webserver, tags: [web, nginx] }
- { role: monitoring, tags: monitoring }Tag Entire Plays
- hosts: webservers
tags: webservers
tasks:
- name: All tasks inherit the play tag
package:
name: nginxSpecial Tags
| Tag | Description |
|---|---|
always | Always runs (unless --skip-tags always) |
never | Never runs (unless --tags never or explicit) |
tagged | All tagged tasks |
untagged | All untagged tasks |
all | Everything (default) |
- name: Debug info (only when asked)
debug:
msg: "Debug mode active"
tags: never # Must explicitly --tags debug,never
- name: Gather facts (always)
setup:
tags: alwaysTag Inheritance
Tags on blocks, roles, and plays are inherited by child tasks:
- block:
- name: Install packages
package: name=nginx
- name: Start service
service: name=nginx state=started
tags: webserver
# Both tasks get the 'webserver' tagCommon Tag Patterns
# Environment tags
tags: [production, deploy]
# Phase tags
tags: [phase1, install]
# Component tags
tags: [database, postgresql]# Deploy only database in production
ansible-playbook site.yml --tags "database"
# Run everything except monitoring
ansible-playbook site.yml --skip-tags "monitoring"FAQ
Do handlers respect tags?
Handlers run when notified, regardless of tags. A tagged task that changes will trigger its handler even if the handler isn't tagged.
Can I use tags in roles?
Yes — tag the role in the playbook, or tag individual tasks within the role. Role-level tags apply to all tasks in that role.
Should I tag every task?
No — tag logical groups. Over-tagging makes playbooks harder to maintain. Use tags for phases (install, configure, deploy) or components (web, db, monitoring).
Basic Tags
- hosts: all
tasks:
- name: Install packages
apt: { name: nginx, state: present }
tags: [packages]
become: true
- name: Deploy config
template: { src: nginx.conf.j2, dest: /etc/nginx/nginx.conf }
tags: [config]
become: true
- name: Start service
service: { name: nginx, state: started }
tags: [service]
become: trueRun Specific Tags
# Only run "config" tasks
ansible-playbook site.yml --tags config
# Run multiple tags
ansible-playbook site.yml --tags "packages,config"
# Skip specific tags
ansible-playbook site.yml --skip-tags packages
# List all available tags
ansible-playbook site.yml --list-tagsTag Roles
- hosts: webservers
roles:
- role: webserver
tags: [web]
- role: monitoring
tags: [monitoring]# Only run webserver role
ansible-playbook site.yml --tags webTag Includes
- import_tasks: install.yml
tags: [install]
- include_tasks: configure.yml
tags: [configure]Multiple Tags Per Task
- name: Deploy SSL certificate
copy:
src: cert.pem
dest: /etc/ssl/cert.pem
tags:
- ssl
- security
- deploySpecial Tags
# 'always' — runs even when filtering by tags
- name: Gather facts
setup:
tags: always
# 'never' — only runs when explicitly called
- name: Dangerous cleanup
shell: rm -rf /tmp/*
tags: never# Run 'never' tagged tasks explicitly
ansible-playbook site.yml --tags cleanup,neverTag Inheritance
# Tags on blocks apply to all tasks inside
- block:
- apt: { name: nginx }
- apt: { name: certbot }
- template: { src: nginx.conf.j2, dest: /etc/nginx/nginx.conf }
tags: [web]
become: trueTagging Strategy
# Recommended: use consistent tag categories
tasks:
# Component tags
- apt: { name: nginx }
tags: [nginx, packages]
# Phase tags
- template: { src: config.j2, dest: /etc/app.conf }
tags: [nginx, config]
# Action tags
- service: { name: nginx, state: restarted }
tags: [nginx, service, restart]# All nginx tasks
ansible-playbook site.yml --tags nginx
# Only package installation
ansible-playbook site.yml --tags packages
# Everything except restart
ansible-playbook site.yml --skip-tags restartPlay-Level Tags
- hosts: webservers
tags: [web]
tasks:
- apt: { name: nginx } # Inherits 'web' tag
- hosts: dbservers
tags: [database]
tasks:
- apt: { name: postgresql } # Inherits 'database' tagFAQ
Do tags affect handlers?
Handlers run if notified, regardless of tags. However, if the task that notifies is skipped by tags, the handler won't be triggered.
import_tasks vs include_tasks with tags?
import_tasks: tags apply to individual tasks inside. include_tasks: tags apply to the include statement itself, but you can also pass tags to included tasks.
Can I combine --tags and --skip-tags?
Yes: --tags deploy --skip-tags dangerous runs all 'deploy' tasks except those also tagged 'dangerous'.
Should I tag every task?
No — tag strategically. Common patterns: tag by component (nginx, postgres), by phase (install, configure, deploy), or by action (restart, backup).
Related Articles
See also
Category: installation