38

Imagine this ansible playbook:

- name: debug foo
  debug: msg=foo
  tags:
     - foo

- name: debug bar
  debug: msg=bar
  tags:
     - bar

- name: debug baz
  debug: msg=baz
  tags:
     - foo
     - bar

How can I run only the debug baz task? I want to say only run tasks which are tagged with foo AND bar. Is that possible?

I tried this, but it will run all 3 tasks:

ansible-playbook foo.yml -t foo,bar
2
  • 5
    My only workaround so far has been to use compound tags like tags: ['foo', 'bar', 'foo-bar'] which is a little ugly... :-(
    – chmac
    May 4, 2015 at 17:26
  • This one works like a charm! Thank you very much. Sad, this was not accepted as answer.
    – Judge
    Nov 21, 2019 at 9:40

5 Answers 5

31

Ansible tags use "or" not "and" as a comparison. Your solution to create yet another tag is the appropriate one.

5

Try when directive:

- name: debug foo
  debug: msg=foo
  tags:
     - foo

- name: debug bar
  debug: msg=bar
  tags:
     - bar

- name: debug baz
  debug: msg=baz
  when:
    - '"foo" in ansible_run_tags'
    - '"bar" in ansible_run_tags'
2
  • 1
    This won't run baz if only one tag (foo or bar) are given. Neither will this run if ansible is called without tags
    – wolfrevo
    Jan 23, 2020 at 11:17
  • This worked for me, thanks. @wolfrevo, that is the expected behavior. Jun 15 at 15:53
2

Use the following to run the task if both fooand bar are given (i.e. ansible-playbook foo.yml -t foo,bar:

- debug:
    msg: "(foo and bar)"
  tags:
    - "{{ 'always' if 'foo' in ansible_run_tags and 'bar' in ansible_run_tags else ''}}"

If you need it to run either when foo or bar or both fooand bar are given (i.e. ansible-playbook foo.yml -t foo, ansible-playbook foo.yml -t bar or ansible-playbook foo.yml -t foo,bar) then use the following:

- debug:
    msg: "(foo and bar) or foo or bar"
  tags:
    - "{{ 'always' if 'foo' in ansible_run_tags and 'bar' in ansible_run_tags else ''}}"
    - foo
    - bar
-2

I believe the correct syntax is:

- name: debug baz
  debug: msg=baz
  tags: foo, bar
3
  • sorry the markup was incorrect. It's actually how it would look in the playbook.
    – foofunner
    Apr 18, 2016 at 8:54
  • OK, think that's about assigning multiple tags to a single element, but don't think it helps to run only elements which have multiple tags...
    – chmac
    Apr 19, 2016 at 9:04
  • 1
    Ok, my bad, I misunderstood your question. You can't do this unfortunately via tags. Tags will always run the union of the tags. It will apply them like an OR operator vs an AND operator. So I think Bruce is correct
    – foofunner
    Apr 19, 2016 at 11:09
-2

If you use this way:

- name: debug baz
  debug: msg=baz
  tags:
    - foo
    - bar

You made a operation OR. So, if you use the command:

ansible-playbook -i inventory test.yml --tags foo

or

ansible-playbook -i inventory test.yml --tags bar

Will execute this task.

If you use:

- name: debug baz
  debug: msg=baz
  tags:
    - foo, bar

You made a operation AND. So only the command:

ansible-playbook -i inventory test.yml --tags foo, bar

Will execute this task.

2
  • 2
    That does not work. A task defined as in your "AND" example is not executed when called like that.
    – Judge
    Nov 21, 2019 at 9:29
  • 1
    If you have a space in between the tags, you need to put the tags in quotes: --tags "foo, bar"
    – rubo77
    Feb 6, 2021 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.