# include\_roles, with tags from a dictionary loop?

**URL:** <https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189>\
**Category:** Ansible Project\
**Created:** [January 11, 2022, 2:53pm UTC](https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189 "2022-01-11T14:53:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [January 11, 2022, 2:53pm UTC](https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189/1 "2022-01-11T14:53:19Z")

</div>

Hey;

I’m having difficulty coming up with the right syntax to add/include roles, with tags, from a loop such that I can limit execution to specific tags.

In the tasks section, this syntax works:

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 11, 2022, 3:20pm UTC](https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189/2 "2022-01-11T15:20:03Z")

</div>

> Hey;
> 
> I'm having difficulty coming up with the right syntax to add/include roles, with tags, from a loop such that I can limit execution to specific tags.

Hello,

in my opinion tags should be use to control the playbook flow and not to determine which role is getting to applied to a server.

This belongs to the inventory:

- import\_role:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: mysql  
&nbsp;&nbsp;&nbsp;when: "'mysql\_servers' in group\_names"

- import\_role:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: nginx  
&nbsp;&nbsp;&nbsp;&nbsp;when: "'nginx\_servers' in group\_names"

This is more flexible than tags and a better place for the assignment of the roles.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [January 11, 2022, 3:37pm UTC](https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189/3 "2022-01-11T15:37:42Z")

</div>

Hey; Thanks for the reply. For clarity, I’m not using tags to identify what roles get assigned to specific hosts - I’m (going to be) using ansible group membership for that. I have a dynamic inventory script that generates host groups very reliably - I’ve been using it with ad-hoc runs for months to great effect. These two roles - and maybe a couple of more - will be applied to all hosts and I’ll have another set of roles for specific host types.

The root role, for instance, has 4 included tasks:

$ grep include roles/root/tasks/main.yml

- include\_tasks: rootpwd.yml
- include\_tasks: rootbin.yml
- include\_tasks: root\_keys.yml
- include\_tasks: drexercise.yml

The initial use case is testing to verify the new included task actually works; however, when all is said and done, being able to execute just the drexercise for our periodic dr tests, rather than the entire playbook including all applicaiton settings, would be much quicker.

Thanks again for the reply.

Doug

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 11, 2022, 5:15pm UTC](https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189/4 "2022-01-11T17:15:02Z")

</div>

You cannot limit tag execution this way, that ONLY applies the tag to  
the include task itself, then the tag must be selected or rejected  
from CLI --tags/--skip-tags.

The `tags:` keyword is ONLY for assignment of tags, never for selection.

---

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [January 11, 2022, 6:07pm UTC](https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189/5 "2022-01-11T18:07:57Z")

</div>

Hey;

thanks for the reply. I’m not real clear on your meaning, though. I already know this works. When I include the roles explicitly, with tags, I can limit execution to a specific tag via the --tags cli arg. Example:

[[snip]]

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 11, 2022, 7:05pm UTC](https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189/6 "2022-01-11T19:05:25Z")

</div>

yes, sorry, i had incorrectly inferred from your previous email that  
you were trying to do the limiting via `tags:`, also note that while  
tags: on include\_role affects the execution of the 'include\_role'  
itself, it does not tag the included tasks, you nee to use apply: for  
such things (unlike import\_ which ignores the tags: and is always used  
for imported tasks to inherit).

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 11, 2022, 8:02pm UTC](https://forum.ansible.com/t/include-roles-with-tags-from-a-dictionary-loop/36189/7 "2022-01-11T20:02:54Z")

</div>

The “FAILED! …” message you cited is (I’m guessing; if I’m wrong I’ll gladly give you a refund) due to the hopeless ambiguities created by python’s treatment of strings as lists, and because Ansible is more an extension of python than it is an application written in python, so stuff like this — obscure references to inaccessible details — percolates up to the user level. (Why would an _application_ throw implementation-language specific errors like that at users? Because the _application_ is “python with Ansible extensions!”) Try changing your line to

apply:  
tags: “{{ [rolename.t] }}”

or go with

apply:  
tags:

- “{{ rolename.t }}”

or even

apply:  
tags: “{{ rolename.t | join(‘,’) }}”

which should have the same effect, but doesn’t always because, well, python.

But more to the point: you don’t want to do this with tags. In fact I doubt this will work despite your earlier testing which lead you to believe this is a fruitful approach. Defer your insanity by using “tasks\_from: ‘{{ … }}’” and structure your, er, structure to reflect your roles’ task files, possibly moderated by clever used of ansible\_run\_tags if you must.

Or maybe it’ll work! I hope I’m wrong. Really. Good luck.
