# role parameter in combination with with\_items

**URL:** <https://forum.ansible.com/t/role-parameter-in-combination-with-with-items/24633>\
**Category:** Ansible Project\
**Created:** [June 2, 2017, 12:52pm UTC](https://forum.ansible.com/t/role-parameter-in-combination-with-with-items/24633 "2017-06-02T12:52:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastien.barthelemy](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sebastien.barthelemy](https://forum.ansible.com/u/sebastien.barthelemy)\
**Post date:** [June 2, 2017, 12:52pm UTC](https://forum.ansible.com/t/role-parameter-in-combination-with-with-items/24633/1 "2017-06-02T12:52:23Z")

</div>

Hello,

I would like to apply the same role several times to a host, with different parameters.

Sadly, as can be seen in the small example below, using with\_items does not work:  
the role is applied several times, but the parameters are not expanded, and the loop variable (item) does not seem to be available withing the role scope.

Am I missing something?  
Is there a workaround?  
Thank you!

$ cat roleloopvarexp.yaml

- hosts: localhost  
roles:
- role: examplerole  
name: riri
- role: examplerole  
name: “{{ item }}”  
with\_items:
- fifi
- loulou

$ cat roles/examplerole/tasks/main.yaml

- debug:  
msg: “name is: {{ name }}”

$ ansible-playbook roleloopvarexp.yaml  
[WARNING]: provided hosts list is empty, only localhost is available  
PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]  
TASK [examplerole : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “name is: riri”  
}  
TASK [examplerole : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: {{ item }}: ‘item’ is undefined\n\nThe error appears to have been in ‘/home/sbarthelemy/ar/s/motion-data/buildfarm/roles/examplerole/tasks/main.yaml’: line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n ^ here\n”}  
to retry, use: --limit @/home/sbarthelemy/ar/s/motion-data/buildfarm/roleloopvarexp.retry  
PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=0 unreachable=0 failed=1

$ ansible-playbook --version  
ansible-playbook 2.2.0.0  
config file = /etc/ansible/ansible.cfg  
configured module search path = [‘/usr/share/ansible’]

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 2, 2017, 2:15pm UTC](https://forum.ansible.com/t/role-parameter-in-combination-with-with-items/24633/2 "2017-06-02T14:15:31Z")

</div>

- name: test  
hosts: 127.0.0.1  
gather\_facts: False  
tasks:  
&nbsp;&nbsp;&nbsp;- name: test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include\_role:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: test1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- edwin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- henk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testvar: "{{item}}"

---

<div class="post-metadata">

**Author:** ![sebastien.barthelemy](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sebastien.barthelemy](https://forum.ansible.com/u/sebastien.barthelemy)\
**Post date:** [June 2, 2017, 3:13pm UTC](https://forum.ansible.com/t/role-parameter-in-combination-with-with-items/24633/3 "2017-06-02T15:13:48Z")

</div>

Hi Brian,

thank you for the workaround.

Do you know if this limitation of the role directive is documented some where (in the doc or maybe in a ticket ?).

For completeness, here is what your workaround gives when applied to my example. I had to rename the “name” variable to avoid collisions.

$ cat roleloopvarexp.yaml

- hosts: localhost  
tasks:
- include\_role:  
name: examplerole  
vars:  
aname: riri
- include\_role:  
name: examplerole  
with\_items:
- fifi
- loulou  
vars:  
aname: “{{ item }}”

$ cat roles/examplerole/tasks/main.yaml

- debug:  
msg: “aname is: {{ aname }}”

$ ansible-playbook roleloopvarexp.yaml  
[WARNING]: provided hosts list is empty, only localhost is available  
PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]  
TASK [examplerole : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “aname is: riri”  
}  
TASK [include\_role] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TASK [examplerole : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “aname is: fifi”  
}  
TASK [examplerole : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “aname is: loulou”  
}  
PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=4 changed=0 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 2, 2017, 3:20pm UTC](https://forum.ansible.com/t/role-parameter-in-combination-with-with-items/24633/4 "2017-06-02T15:20:19Z")

</div>

Well, roles: have always been statick, with\_ and other directives ONLY  
execute on tasks, so when adding them to static imports (like roles:)  
they should just be inherited by the imported tasks.
