tags with roles

68 views
Skip to first unread message

Vladimir Ondrus

unread,
Jul 16, 2015, 7:50:58 AM7/16/15
to ansible...@googlegroups.com
Hi All,

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

I want to execute tasks marked with aaaa from testrole:

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


here is the role:

[root@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:

[root@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

Brian Coca

unread,
Jul 16, 2015, 3:07:17 PM7/16/15
to ansible...@googlegroups.com
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

Vladimir Ondrus

unread,
Jul 23, 2015, 7:21:57 AM7/23/15
to Ansible Project
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:

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

Brian Coca

unread,
Jul 23, 2015, 7:37:17 AM7/23/15
to ansible...@googlegroups.com
the when will be applied to each task, so they will be skipped



--
Brian Coca

Vladimir Ondrus

unread,
Jul 23, 2015, 8:13:32 AM7/23/15
to Ansible Project, bc...@ansible.com
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

Brian Coca

unread,
Jul 23, 2015, 8:16:08 AM7/23/15
to Vladimir Ondrus, Ansible Project
you can write the when in the include or in 2.0 use blocks
--
Brian Coca
Reply all
Reply to author
Forward
0 new messages