Ansible Tags: Run Specific Tasks in Playbooks (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: installation
How to use Ansible tags to run or skip specific tasks. Tag tasks, roles, and plays. Use --tags and --skip-tags to control execution. Practical YAML playbook examples.
Ansible Tags: Run Specific Tasks in Playbooks (Complete Guide)
Ansible tags let you selectively run or skip tasks within a playbook. Instead of running every task, you can execute only the ones tagged for configuration, deployment, or testing — saving time during development and targeted operations.
Basic Usage
Tag a Task
Run Only Tagged Tasks
Tag Syntax Options
Single Tag
Multiple Tags on One Task
Tag an Entire Block
Tag a Role
Tag an Entire Play
Special Tags
always
Tasks tagged always run every time (unless explicitly skipped):
never
Tasks tagged never only run when explicitly requested:
Tag Patterns
Environment-Based Tags
Phase-Based Tags
Tags with include_tasks and import_tasks
import_tasks (tags inherited)
include_tasks (tags on include only)
List and Preview Tags
Best Practices Use consistent tag names across playbooks (packages, config, service, deploy) Tag always sparingly — only for truly universal tasks like fact gathering Use never for dangerous operations — cleanup, data deletion, debugging Don't over-tag — if everything is tagged, tags lose their value Document your tags in the playbook header or README
FAQ
What are Ansible tags?
Tags are labels you attach to tasks, blocks, roles, or plays. They let you selectively run (--tags) or skip (--skip-tags) specific parts of a playbook without modifying the YAML.
How do I run only specific tasks in an Ansible playbook?
Use --tags tag_name: ansible-playbook playbook.yml --tags config. Only tasks with that tag (plus tasks tagged always) will execute.
What does the "always" tag do in Ansible?
Tasks tagged always run regardless of which --tags you specify. Use it for essential tasks like gathering facts or displaying deployment information that should always execute.
What does the "never" tag do in Ansible?
Tasks tagged never are skipped during normal execution. They only run when explicitly requested with --tags tag_name where tag_name is another tag on that task.
Can I use multiple tags on one task?
Yes. Use a YAML list: tags: [config, nginx, setup]. The task runs if ANY of its tags match the --tags argument.
Conclusion
Ansible tags provide fine-grained control over playbook execution. Use them for phase-based deployment, environment targeting, and development workflows. Combine --tags and --skip-tags to run exactly the tasks you need.
Related Articles • Ansible Playbook: Complete Guide • Ansible Playbook --limit: Run on Specific Hosts • How to Run Only One Task with Ansible Tags
Category: installation