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