Discussion:
[ansible-project] tags with roles
Vladimir Ondrus
2015-07-16 11:38:46 UTC
Permalink
Hi All,

I want to use tags inside the roles, here is my usecase:

I want to execute tasks marked with aaaa from testrole:

[***@wildfly ~/ansible]$ cat test.yml
---
- hosts: local
roles:
- {role: testrole, tags: ['aaaa']}


here is the role:

[***@wildfly ~/ansible]$ cat roles/testrole/tasks/main.yml
---
- name: aaaa tag
local_action: command echo
tags:
- aaaa

- name: bbbb tag
local_action: command echo
tags:
- bbbb


but both tasks are executed:

[***@wildfly ~/ansible]$ ansible-playbook -i inventory.ini test.yml

PLAY [local]
******************************************************************

GATHERING FACTS
***************************************************************
ok: [localhost]

TASK: [testrole | aaaa tag]
***************************************************
changed: [localhost -> 127.0.0.1]

TASK: [testrole | bbbb tag]
***************************************************
changed: [localhost -> 127.0.0.1]

PLAY RECAP
********************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0


Is this OK, or bug?


Regards,
Vlado
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0a285795-8acd-40a6-bb6b-1f0f4ccdcbb4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-16 19:07:12 UTC
Permalink
Hi Vlado,

I think you are misunderstanding the feature. There is no sub
-election with tags on role definition, when you:
- {role: testrole, tags: ['aaaa']}

What you are doing is tagging all tasks in testrole with 'aaaa' on top
of existing tags. So it would look like this once imported:

- name: aaaa tag
local_action: command echo
tags:
- aaaa

- name: bbbb tag
local_action: command echo
tags:
- bbbb
- aaaa

You are not saying 'run only those tagged with 'aaaa', that is not
possible when specifying a role to import.
I suggest making 2 roles and only importing the one with the tasks you
want to execute.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8mRn1qWaU2qf0f4NGdtt7WREqvE1aRVw6YL0xMM7gqH0w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Vladimir Ondrus
2015-07-23 11:21:57 UTC
Permalink
Hi Brain,

thanks for the explanation. Here is similar problem we have:
We have a download role which should download something from given
location(web, maven, local).
So when location is maven, I dont want to execute http steps.
But when "when" statement is after include this means that all tasks in
included yml will be executed with it.
But i would like to skip complete yml.
---
- debug: msg="Debug location: {{ location }}"

- include: maven.yml
when: location == "maven"

- include: http.yml
when: location == "http"

- include: local.yml
when: location == "local"

when location is maven i want to completly skip http and local.
I found discussion about this:
https://github.com/ansible/ansible/issues/3324

What would may be help here, would be something like goto statement:
goto: end of the playbook
goto: task="task-name"
goto: exit ansible

With this would be possible to simulate if-else in playbooks.
What do you think about this?

Vlado
Post by Brian Coca
Hi Vlado,
I think you are misunderstanding the feature. There is no sub
- {role: testrole, tags: ['aaaa']}
What you are doing is tagging all tasks in testrole with 'aaaa' on top
- name: aaaa tag
local_action: command echo
- aaaa
- name: bbbb tag
local_action: command echo
- bbbb
- aaaa
You are not saying 'run only those tagged with 'aaaa', that is not
possible when specifying a role to import.
I suggest making 2 roles and only importing the one with the tasks you
want to execute.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ccb5d0dc-7164-461d-923d-f5b07e19dc8b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-23 11:37:12 UTC
Permalink
the when will be applied to each task, so they will be skipped
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8nzCOUj5SGswi%2BgAp64qmR_Nmb-9jHMaOaSx8uV9bs8YA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Vladimir Ondrus
2015-07-23 12:13:32 UTC
Permalink
this is current state:

when you want to implement this
if(x=true)
task1
task2
task3
task4
task5
if(y=true)
task6
task7

you should write when to each task:
task1 when x
task2 when x
task3 when x
task4 when x
task5 when x
task6 when y
task7 when y

but when there would be something like goto, you could save many whens, and
it would have the desired effect that task would be completely skipped:

goto label when x != true
task1
task2
task3
tast4
tast6

label

goto end when y !=true

tast6
tast7
end
Post by Brian Coca
the when will be applied to each task, so they will be skipped
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1e5584c5-5c8d-48dd-b748-7a6be988e7b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-23 12:16:03 UTC
Permalink
you can write the when in the include or in 2.0 use blocks

On Thu, Jul 23, 2015 at 8:13 AM, Vladimir Ondrus
Post by Vladimir Ondrus
when you want to implement this
if(x=true)
task1
task2
task3
task4
task5
if(y=true)
task6
task7
task1 when x
task2 when x
task3 when x
task4 when x
task5 when x
task6 when y
task7 when y
but when there would be something like goto, you could save many whens, and
goto label when x != true
task1
task2
task3
tast4
tast6
label
goto end when y !=true
tast6
tast7
end
Post by Brian Coca
the when will be applied to each task, so they will be skipped
--
Brian Coca
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8nX5Kp9t9X4XOC5eVCLC3h63QvLJ1SPGioGqpr7sseZxw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...