AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Tags: Run Only Specific Tasks in a Playbook (Guide) — Video Tutorial
How to run only one task or specific tasks in an Ansible playbook using tags. Use --tags, --skip-tags, always/never tags.
What You'll Learn
- How to run only one task in an Ansible Playbook?
- How use tags statement in Ansible Playbook?
- Links
- code
- execution default
- execution tags tag1
- execution tags tag2
- execution tags tagged
- Conclusion
- Related Articles
Full Tutorial Content
How to run only one task in an Ansible Playbook?
I'm going to show you a live Playbook with some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
How use tags statement in Ansible Playbook?
- `--tags all`
- `--tags tag1`
- `--tags [tag1, tag2]`
- `--skip-tags [tag3, tag4]`
- `--tags tagged`
- `--tags untagged`
Today we're talking about Ansible tags statement.
When your Ansible Playbook starts to grow up it's useful to define tags to allow a more granular execution.
You could define one or multiple tags at the individual task, include, import, play, block, role level.
Tags also could have tag inheritance properties.
The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the "ansible-playbook" command.
The default behavior is to execute all the tags in your Playbook with `--tags all`.
You could specify to execute a single tag with `--tags tag1` or a list of tags `--tags [tag1, tag2]`
You could specify also and use the negate logic to exclude some tags`--skip-tags [tag3, tag4]`.
Another convenient way is to execute only the code with tag `--tags tagged` or without `--tags untagged`.
Links
- [Tags](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html)
## Playbook
How to run only one task in Ansible Playbook?
I'm going to show you one simple Ansible Playbook with two tasks and two tags and how to select one or another via the command line.
code
```yaml
---
- name: tags Playbook
hosts: all
gather_facts: false
tasks:
- name: example 1
ansible.builtin.debug:
msg: "example 1"
tags: tag1
- name: example 2
ansible.builtin.debug:
msg: "example 2"
tags: tag2
```
execution default
```bash
$ ansible-playbook -i virtualmachines/demo/inventory variables/tags.yml
PLAY [tags Playbook] **********************************************************************************
TASK [example 1] **********************************************************************************
ok: [demo.example.com] => {
"msg": "example 1"
}
TASK [example 2] **********************************************************************************
ok: [demo.example.com] => {
"msg": "example 2"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
execution tags tag1
```bash
$ ansible-playbook -i virtualmachines/demo/inventory --tags=tag1 variables/tags.yml
PLAY [tags Playbook] **********************************************************************************
TASK [example 1] **********************************************************************************
ok: [demo.example.com] => {
"msg": "example 1"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 3 min
- Category: troubleshooting
Read the full written article: Ansible Tags: Run Only Specific Tasks in a Playbook (Guide)
Related Video Tutorials
- Creating Custom Ansible Plugins to Fetch API Data Easily — Learn how to create custom Ansible plugins to fetch data from APIs. Enhance your automation tasks with this detailed step-by-step guide.
- Ansible selectattr Filter: Filter Lists by Attributes (Guide) — How to use Ansible selectattr filter to filter lists by attributes. Select items matching conditions, combine with map and reject filters with examples.
- ansible_date_time: Format Dates, Time & Timestamps in Playbooks — How to use ansible_date_time fact in playbooks. Access date, time, epoch, ISO8601 timestamps. Format dates with strftime filter, create time-based filenames.
- ansible_date_time: Use Date, Time & Timestamps in Ansible Playbooks — Learn how to use date, time, and timestamp in Ansible playbooks without facts. Follow our Playbook and simple Ansible code examples for quick solutions.
- Ansible terminology - What is an Ansible Playbook? — A step-by-step guide inside the Ansible Playbook anatomy: play, tasks, modules, conditional, loop, handler, variable, list.
- Mastering Ansible-Creator: Simplify Your Ansible Collection Development — Discover how to install and use Ansible-Creator to simplify Ansible Collection development. Enhance your automation projects with this powerful tool and Visual.