Ansible Tags: Run Only Specific Tasks in a Playbook (Guide)
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
How to run only one task or specific tasks in an Ansible playbook using tags. Use --tags, --skip-tags, always/never tags.

How to run only one task in an Ansible Playbook?
See also: Ansible Tags: Run Specific Tasks in Playbooks (Complete Guide)
How use tags statement in Ansible Playbook?
--tags all--tags tag1--tags [tag1, tag2]--skip-tags [tag3, tag4]--tags tagged--tags untagged
--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
## Playbook How to run only one task in Ansible Playbook?
code
---
- 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: tag2execution default
$ 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=0execution tags tag1
$ 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=0 failed=0 skipped=0 rescued=0 ignored=0execution tags tag2
$ ansible-playbook -i virtualmachines/demo/inventory --tags=tag2 variables/tags.yml
PLAY [tags Playbook] **********************************************************************************
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=0execution tags tagged
$ ansible-playbook -i virtualmachines/demo/inventory --tags tagged 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 untagged
$ ansible-playbook -i virtualmachines/demo/inventory --tags untagged variables/tags.yml
PLAY [tags Playbook] **********************************************************************************
PLAY RECAP ****************************************************************************************
execution tags all
$ ansible-playbook -i virtualmachines/demo/inventory --tags all 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=0See also: Automate Redmine Installation on Ubuntu LTS 22.04 with Ansible
Conclusion
Now you know how to run only one task in Ansible Playbook from the command line.Related Articles
Category: troubleshooting
Watch the video: Ansible Tags: Run Only Specific Tasks in a Playbook (Guide) — Video Tutorial