# passing variables in  ansible 2.3 not working

**URL:** <https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461>\
**Category:** Ansible Project\
**Created:** [September 12, 2017, 2:40pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461 "2017-09-12T14:40:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yz\_Xyz](https://avatars.discourse-cdn.com/v4/letter/y/a698b9/32.png) [@Yz\_Xyz](https://forum.ansible.com/u/Yz_Xyz)\
**Post date:** [September 12, 2017, 2:40pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/1 "2017-09-12T14:40:25Z")

</div>

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

test Playbook :

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

```

value1.test.yml.j2  
template:

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

```

loading vars:

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

```

```auto

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

```

```auto

```

```auto

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 12, 2017, 2:43pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/2 "2017-09-12T14:43:08Z")

</div>

dont stack moustaches:

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

---

<div class="post-metadata">

**Author:** ![Yz\_Xyz](https://avatars.discourse-cdn.com/v4/letter/y/a698b9/32.png) [@Yz\_Xyz](https://forum.ansible.com/u/Yz_Xyz)\
**Post date:** [September 12, 2017, 3:30pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/3 "2017-09-12T15:30:30Z")

</div>

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

```auto
items.items()

```

MSG:

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

if I replace var2 with foo it works as expected.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 12, 2017, 3:51pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/4 "2017-09-12T15:51:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yz\_Xyz](https://avatars.discourse-cdn.com/v4/letter/y/a698b9/32.png) [@Yz\_Xyz](https://forum.ansible.com/u/Yz_Xyz)\
**Post date:** [September 12, 2017, 4:20pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/5 "2017-09-12T16:20:26Z")

</div>

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:

```auto
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:

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

```

Playbook:

```auto
- 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 12, 2017, 4:25pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/6 "2017-09-12T16:25:59Z")

</div>

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

{% for item in hostvars[inventory\_hostname][var2] %}

---

<div class="post-metadata">

**Author:** ![Yz\_Xyz](https://avatars.discourse-cdn.com/v4/letter/y/a698b9/32.png) [@Yz\_Xyz](https://forum.ansible.com/u/Yz_Xyz)\
**Post date:** [September 12, 2017, 5:07pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/7 "2017-09-12T17:07:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 12, 2017, 6:14pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/8 "2017-09-12T18:14:13Z")

</div>

you never showed where/how you load 'foo'

---

<div class="post-metadata">

**Author:** ![Yz\_Xyz](https://avatars.discourse-cdn.com/v4/letter/y/a698b9/32.png) [@Yz\_Xyz](https://forum.ansible.com/u/Yz_Xyz)\
**Post date:** [September 12, 2017, 6:36pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/9 "2017-09-12T18:36:50Z")

</div>

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

```auto
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

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

```

template/value1.test.yml.j2

```auto
{% 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

---

<div class="post-metadata">

**Author:** ![Yz\_Xyz](https://avatars.discourse-cdn.com/v4/letter/y/a698b9/32.png) [@Yz\_Xyz](https://forum.ansible.com/u/Yz_Xyz)\
**Post date:** [September 12, 2017, 7:48pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/10 "2017-09-12T19:48:29Z")

</div>

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/

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 12, 2017, 9:51pm UTC](https://forum.ansible.com/t/passing-variables-in-ansible-2-3-not-working/25461/11 "2017-09-12T21:51:26Z")

</div>

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