Calling a role, but only run tasks that use a certain tag

40 views
Skip to first unread message

Johannes Kastl

unread,
Mar 9, 2016, 2:52:51 PM3/9/16
to ansible...@googlegroups.com
Hi all,

I think I am not the only one who is being confused by how tags on
roles work:

- roles:
- { role: xyz, tags: 'whatever'}

This does *not* call only the parts of the role that are tagged
'whatever', instead all tasks in this role are being tagged whatever
and are being called.

Is there any way to just run the parts of a role that are having a
certain tag?

One workaround would be to add a certain variable to the role call,
and just run the task in the role when this variable has the right
value. But how to do that?

Thanks in advance,

Johannes

signature.asc

Mike Biancaniello

unread,
Mar 9, 2016, 4:51:38 PM3/9/16
to Ansible Project
I have seen others ask for this. Hopefully, there will eventually be a more elegant method. However, I believe that the only way to do this is, as you said, use a variable. I do this for debug or testing tasks that I don't want to run normally, but if I pass -e '{"run_tests":true}' when I run the playbook they will execute.

#roles/stuff/tasks/main.yml
---
- name: Run Tests (when run_tests is true)
  include
: tests.yml
 
when: run_tests|default(false)



and then you can either pass run_tests as an extra-var, or define it in a playbook:

- name: Testing stuff
  hosts
: things

  vars
:
    run_tests
: true

  roles
:
 
- { "role":"stuff", "tags":["logit"] }

or

- name: Testing stuff
  hosts
: things

  roles
:
 
- { "role":"stuff", "tags":["logit"], "run_tests": true }



Johannes Kastl

unread,
Mar 10, 2016, 12:11:06 PM3/10/16
to ansible...@googlegroups.com
Hi Mike,

Am 09.03.16 schrieb Mike Biancaniello:
> I have seen others ask for this. Hopefully, there will eventually be a more
> elegant method.

Does ansible have a bugtracker/wishlist other than the github issues?
I'll have to check that...

> However, I believe that the only way to do this is, as you
> said, use a variable. I do this for debug or testing tasks that I don't
> want to run normally, but if I pass -e '{"run_tests":true}' when I run the
> playbook they will execute.

Thanks for confirmation, than that's the way to go for me.

Johannes

signature.asc
Reply all
Reply to author
Forward
0 new messages