Is there a way to run tasks that have ALL of the tasks given as parameters to ansible-playbook?

For example, let’s pretend we have these tasks:

`

  • name: Task 1
    tags:

  • foo

  • bar

  • name: Task 2
    tags:

  • foo

  • name: Task 3
    tags:

  • bar

`

Is there a way to only execute tasks that have both of foo and bar tags? That’s, only execute Task 1?

Good question

I have also wondered if you can run tasks with all tags. Eg -t all.

I don’t think so. In this case, I would just give tasks that have both foo and bar as tags a third tag that will allow those tasks to be run. It’s a little cumbersome, but it would work. However, as far as I know, there is no AND type of operator for tags.

I would just give tasks that have both foo and bar as tags a third tag that will allow those tasks to be run

Unfortunately this won’t work for me. I have multiple roles imported in my playbook:

  • { role: a, tags: [“a”] }
  • { role: b, tags: [“b”] }
  • { role: b, tags: [“c”] }
  • { role: c, tags: [“d”] }

So all tasks within role a inherit the tag a, the tasks within role b inherit the tag b, and so forth.

Inside these roles, some tasks are tagged with, say “x”. I want to execute the “x” task in role “b”.

You can achieve this by using when on the role to check a variable that you provide with extra_vars on the command line and keep the tags in the roles.