passing variables in ansible 2.3 not working

Hi - I want to pass variable to template… Tried known methods to pass variable to template but gives errors.

test Playbook :

- name: Creating template
   template:
    src:  value1.test.yml.j2
    dest: "/u01/{{ansible_hostname}}.test.yml"
    backup: yes

value1.test.yml.j2
template:

{% for items in {{var2}} %}
{% for thing1,thing2 in items.iteritems() %}
do
thing1: thing2
{% endfor %}
{% endfor %}

loading vars:

foo:
 - site: xyz.com
    owner: test
- site: goole.com
    owner: corp

when I run ansible-playbook test.yml --extra-vars "var2=foo"
gives error:
AnsibleError: template error while templating string: expected token ':', got '}'.

If I substitute the value of var2 with foo in template playbook runs successfully.
I tried below things ansible fails throwing same error

a, with_items: {{foo}} and did {{item}} in playbook it doesn't work.
b. set_fact
-var2: foo

ACTUAL RESULTS

hostname.yml should contain

site: xyz.com

owner: test



dont stack moustaches:

{% for items in var2 %}
{% for thing1,thing2 in i

Thanks Brian for taking time. I was stuck for few days with this error. Now this gives me different error.

AnsibleUndefinedVariable: ‘str object’ has no attribute ‘iteritems’

replaced template with

items.items()

MSG:

AnsibleUndefinedVariable: ‘unicode object’ has no attribute ‘items’

if I replace var2 with foo it works as expected.

well, its a string, not a list nor dictionary ...

Thanks Brian for taking time. I got whole playbook working with static values.
Below is how I structured foo. If I substitute template var2 with foo. it works fine.

vars:

foo:
 - site: [xyz.com](http://xyz.com/)
   owner: test
- site: [goole.com](http://www.google.com/url?q=http%3A%2F%2Fgoole.com&sa=D&sntz=1&usg=AFQjCNFV0Dhi_6h15IkdBf2L187hCekBiQ)
  owner: corp

template:

{% for items in var2 %}
{% for thing1,thing2 in items.iteritems() %}
do
thing1: thing2
{% endfor %}
{% endfor %}

Playbook:

- name: Creating template {{var2}}
   template:
    src:  value1.test.yml.j2
    dest: "/u01/{{ansible_hostname}}.test.yml"
    backup: yes

I am not really sure why var2 is taken as string in template.
I was thinking it would iterate over var2 items in template.

I just realized you dont want the value of var2, but to use it as an alias

{% for item in hostvars[inventory_hostname][var2] %}

Thanks Brian - I am looking for away so that var2 passed to ansible-playbook template as extra-vars is interpreted as dict in vars/main.yml.
When I add var2=foo in inventory. I get dict object has no attribute u’foo’

I now see var2 passed to template. ansible complains

  1. Error: str has no iteritems.
    when template has var2. where var2 is passed as extra_vars.

  2. Error: dict object has no attribute ‘foo’
    when var2 =foo was added inventory variables.

you never showed where/how you load 'foo'

Thanks Brian Sorry my mistake. I am using roles.

in vars directory I have main.yml which defines foo.

Below is directory structure.

Roles
test
vars → main.yml defines foo.
template → value1.test.yml.j2

tasks → create template task.
test.yml -->includes test role.

vars/main.yml

foo:
 - site: [xyz.com](http://xyz.com/)
    owner: test
- site: [goole.com](http://www.google.com/url?q=http%3A%2F%2Fgoole.com&sa=D&sntz=1&usg=AFQjCNFV0Dhi_6h15IkdBf2L187hCekBiQ)
    owner: corp

tasks/main.yml

- name: Creating template
   template:
    src:  value1.test.yml.j2
    dest: "/u01/{{ansible_hostname}}.test.yml"
    backup: yes

template/value1.test.yml.j2

{% for items in {{var2}} %}
{% for thing1,thing2 in items.iteritems() %}
do
thing1: thing2
{% endfor %}
{% endfor %}

test.yml

  • hosts:
    sudo: yes
    connection: local
    sudo_user: ansidevadm
    gather_facts: yes
    roles:
  • test

I am hitting dead end with ansible way of substituting template variable via roles.
I wrote command task to replace template variable before template task is called.

Thanks Brian for your help/

That should not be the case, it should work fine with the role as you
defined it.