0

I have a role setup as follows

roles/test/task/main.yml

- name: Generate people files
  template: src=test.j2 dest=/tmp/{{ item.name}}.cfg
  loop: "{{people}}"

roles/test/template/test.j2

First Var: {{ item.var1 }}
Second Var: {{ item.var2 }}

roles/test/vars/main.yml

---
people:
        - name: TheSimpsons
          var1: homer
          var2: simpson

        - name: StarWars
          var1: han
          var2: solo

roles/test/defaults/main.yml

people:
   - var2: skywalker

my playbook

 - hosts: localhost
   roles:
    - test

When I run my playbook everything works as expect. I get two new files in /tmp with the correct text. However if I remove this var2 line from my vars/main.yml file...

          var2: solo

I would expect the var2 value from my defaults/main.yml to show up in the output, but all I get is this error

failed: [localhost] (item={u'var1': u'han', u'name': u'StarWars'}) => {
    "changed": false,
    "item": {
        "name": "StarWars",
        "var1": "han"
    },
    "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'var2'"
}

I have tried formatting my defaults/main.yml about 10 different ways but get the same error each time.

If I setup a test that doesn't loop and defaults/main.yml and vars/main.yml are flat "key: value" pairs I can get it to pull values from defaults/main/yml just fine.

Something about the looping I'm just not getting. What am I doing wrong?

pizzaguy39
  • 1
  • 1
  • 1

0 Answers0