role parameter in combination with with_items

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’]

- name: test
hosts: 127.0.0.1
gather_facts: False
tasks:
   - name: test
     include_role:
       name: test1
     with_items:
       - edwin
       - henk
     vars:
       testvar: "{{item}}"

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

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.