I'm trying to build a configuration file dynamically with Ansible, using a Jinja2 template.
In Ansible, I've defined a role in which I have the template and the set of variables.
I want my output file to look like this :
  models:
    model1:
      username: user1
      password: password1
    model2:
      username: user2
      password: password2
I have defined my variables like so :
model_list:
  name:
    - model1
    - model2
  user:
    - user1
    - user2
  pass:
    - password1
    - password2
My .j2 template :
{% for model in vars[model_list] %}
  {{ model.name }}
    username: {{ model.user }}
    password: {{ model.pass }}
{% endfor %}
My playbook is quite simple :
- name: Building config file
template:
  src: ./config.j2
  dest: my/path/config
When I run the playbook I've got the following error :
fatal: [FRADEV048]: FAILED! => {"changed": false, "msg": 
"AnsibleUndefinedVariable: dict object has no element {u'user': 
u'user1', u'name': u'model1', u'pass': u'password1'}"}
I'm quite new in programming so I don't really see where my error is ... Any clues ?
Thanks in advance for your help,
Simon
 
    