I am trying to run multiple roles using with_items
command, however I am getting error:
"ERROR! 'item' is undefined"
role.yml
:
---
- hosts: '{{ host }}'
become: yes
roles:
- role: "{{item}}"
with_items: "{{ roles }}"
Here is my command:
ansible-playbook -i ./inventory/Dev ./playbooks/role.yml --extra-vars='{"host": "db", "roles": ["mysql", "apache"]}'
You cannot do it this way. with_
loops are not valid for roles.
If anything, you need to provide a list of roles to the roles:
directive, so the syntax would be just like for the list of host groups hosts: '{{ host }}'
. The problem is: Ansible does not resolve the variable for roles, so roles: '{{ roles }}'
does not work.
What you can do, however, is to use include_role module in which you can access the variables.
No, include_role module doesn't take {{ item }}
from the with_items
as a value for name
either.
So the only workaround I can think of (assuming you don't want to process the JSON beforehand) is to the include the roles statically:
tasks:
- include_role:
name: "mysql"
when: "'mysql' in roles"
- include_role:
name: "apache"
when: "'apache' in roles"
The roles need to exist on the control machine anyway, so all their names are predefined.