Reputation: 19805
My objective is to dynamically generate a list of dictionaries and use it for a role I need.
I've managed to generate the variable, but now I cannot pass it to the role.
This is my play:
- hosts:
- some-hosts
tasks:
- name: create zkh var
vars:
zkh: []
set_fact:
zkh: "{{ zkh + [ {'host': item.1, 'id': item.0} ] }}"
with_indexed_items: "{{ groups.some-hosts }}"
roles:
- {role: ansible-zookeeper, zookeeper_hosts: "{{ zkh }}" }
In the task I generate my variable, but then I cannot pass it to the role as (I imagine), I cannot access the scope where it is defined ("AnsibleUndefinedVariable: ERROR! ERROR! 'zkh' is undefined"}
).
Defining a play-level variable and having a new task to assign it with set_fact
and the value of zkh
doesn't help.
How can I fix this?
Upvotes: 0
Views: 558
Reputation: 1179
You should use pre_tasks
to define variables needed in a role. pre_tasks
are just a list of tasks that are executed before applying a role.
You also have post_tasks
, if you need to do something after applying roles.
Upvotes: 1
Reputation: 23771
Can you try this playbook structure:
- hosts:
- some-hosts
tasks:
- name: create zkh var
vars:
zkh: []
set_fact:
zkh: "{{ zkh + [ {'host': item.1, 'id': item.0} ] }}"
with_indexed_items: "{{ groups.some-hosts }}"
- {role: ansible-zookeeper, zookeeper_hosts: "{{ zkh }}" }
I am assuming that your set_fact
are working properly.
Upvotes: 0