Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run only tasks with a certain tag or untagged

Tags:

ansible

In this playbook:

- hosts: all
  tasks:

  - name: task A
    debug: msg="task A"
    tags:
       - A

  - name: task B
    debug: msg="task B"
    tags:
       - B

  - name: untagged task
    debug: msg="untagged task"

I would like to run the task with the tag A and the untagged task.

I know about the --skip-tags option, but this a simplified example. My use case is for a very complex playbook with many tags and roles with tasks with different tags, and interdependencies between tagged and non-tagged tasks. Long story short, the only 'clean' option for me is to run only specified tags and those untagged.

Is there a way to call ansible-playbook so that it'll run specific tags and untagged tasks only? I'm using ansible 2.9.6

like image 400
Chirlo Avatar asked Jan 30 '26 22:01

Chirlo


1 Answers

This is possible, indeed:

For example, if you wanted to just run the “configuration” and “packages” part of a very long playbook, you can use the --tags option on the command line:

ansible-playbook example.yml --tags "configuration,packages"

Source: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html#tags

Along with:

There are another 3 special keywords for tags: tagged, untagged and all, which run only tagged, only untagged and all tasks respectively.

Source: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html#special-tags

So your solution is:

ansible-playbook --tags "untagged,A" playbook.yml
like image 168
β.εηοιτ.βε Avatar answered Feb 03 '26 01:02

β.εηοιτ.βε



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!