How to Run Only One Task in Ansible Playbook? - Ansible tags statement
How to select single or multiple tasks, include, import, play, block, and role in an Ansible Playbook using tags parameter of ansible-playbook command.


How to run only one task in an Ansible Playbook?
I’m going to show you a live demo 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
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
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
---
- name: tags demo
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
$ ansible-playbook -i virtualmachines/demo/inventory variables/tags.yml
PLAY [tags demo] **********************************************************************************
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
$ ansible-playbook -i virtualmachines/demo/inventory --tags=tag1 variables/tags.yml
PLAY [tags demo] **********************************************************************************
TASK [example 1] **********************************************************************************
ok: [demo.example.com] => {
"msg": "example 1"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
execution tags tag2
$ ansible-playbook -i virtualmachines/demo/inventory --tags=tag2 variables/tags.yml
PLAY [tags demo] **********************************************************************************
TASK [example 2] **********************************************************************************
ok: [demo.example.com] => {
"msg": "example 2"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
execution tags tagged
$ ansible-playbook -i virtualmachines/demo/inventory --tags tagged variables/tags.yml
PLAY [tags demo] **********************************************************************************
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 untagged
$ ansible-playbook -i virtualmachines/demo/inventory --tags untagged variables/tags.yml
PLAY [tags demo] **********************************************************************************
PLAY RECAP ****************************************************************************************
execution tags all
$ ansible-playbook -i virtualmachines/demo/inventory --tags all variables/tags.yml
PLAY [tags demo] **********************************************************************************
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
Recap
Now you know how to run only one task in Ansible Playbook from the command line. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate