Ansible - multiple roles -


i trying run multiple roles using with_items command, getting error:

"error! 'item' undefined"

role.yml:

--- - hosts: '{{ host }}'   become: yes    roles:     - role: "{{item}}"       with_items: "{{ roles }}" 

here command:

ansible-playbook -i ./inventory/dev ./playbooks/role.yml --extra-vars='{"host": "db", "roles": ["mysql", "apache"]}' 

you cannot way. with_ loops not valid roles.

if anything, need provide list of roles roles: directive, syntax list of host groups hosts: '{{ host }}'. problem is: ansible not resolve variable roles, roles: '{{ roles }}' not work.


what can do, however, use include_role module in can access variables.

no, include_role module doesn't take {{ item }} with_items value name either.

so workaround can think of (assuming don't want process json beforehand) include roles statically:

tasks:   - include_role:       name: "mysql"     when: "'mysql' in roles"   - include_role:       name: "apache"     when: "'apache' in roles" 

the roles need exist on control machine anyway, names predefined.


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -