# Seemingly simple template driving me mad

**URL:** <https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857>\
**Category:** Ansible Project\
**Created:** [November 23, 2014, 11:35am UTC](https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857 "2014-11-23T11:35:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Tan](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Stephen\_Tan](https://forum.ansible.com/u/Stephen_Tan)\
**Post date:** [November 23, 2014, 11:35am UTC](https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857/1 "2014-11-23T11:35:36Z")

</div>

Hi - I’m doing something which seems very simple - trying to create a file from a jinja template.

Here are my vars:

export\_root:  
host: “81.187.209.128/27”  
options: “rw,fsid=0,insecure,no\_subtree\_check,async”  
exports:  
video:  
host: “81.187.209.128/27”  
options: “rw,nohide,insecure,no\_subtree\_check,async”  
shared\_dir: “/mnt/video”

Here is the template which I’m using the test var fred:

{{export\_root\_dir}} {{export\_root.host}}({{export\_root.options}})  
{% for fred in exports %}  
{{ fred }}  
{{ fred.options }}  
{% endfor %}

For some reason, fred.options fails with:

fatal: [[althea.home.stanandliz.net](http://althea.home.stanandliz.net)] =\> {‘msg’: “One or more undefined variables: ‘str object’ has no attribute ‘options’”, ‘failed’: True}  
fatal: [[althea.home.stanandliz.net](http://althea.home.stanandliz.net)] =\> {‘msg’: “One or more undefined variables: ‘str object’ has no attribute ‘options’”, ‘failed’: True}

I have no idea why this should be the case. When I access fred, the template prints fine. It’s when I try to access the dictionary in fred that I fail.

I’m sure there is a very simple fix to this, but after about an hour, I just can’t get this to work. Can I have a few debugging clues?

many thanks in advance

Stephen

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [November 24, 2014, 2:43pm UTC](https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857/2 "2014-11-24T14:43:57Z")

</div>

In this case "fred" is the key to the exports dictionary. It can't be  
both the key and the entry itself. Try something like:

&nbsp;&nbsp;{{ fred }}  
&nbsp;&nbsp;{{ exports[fred].options }}

---

<div class="post-metadata">

**Author:** ![Quentin\_Stafford-Fra](https://avatars.discourse-cdn.com/v4/letter/q/b2d939/32.png) [@Quentin\_Stafford-Fra](https://forum.ansible.com/u/Quentin_Stafford-Fra)\
**Post date:** [November 24, 2014, 2:47pm UTC](https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857/3 "2014-11-24T14:47:18Z")

</div>

Hi Stephen -

There may be some clever Jinja magic going on here, but if this were just Python…

- exports, as you’ve defined it, is a dictionary
- ‘for fred in exports’ would give you each of the _keys_ in turn, of which the first is ‘video’ - a string
- so you’d need something like exports[fred][‘options’]

Any good?  
Q

---

<div class="post-metadata">

**Author:** ![Stephen\_Tan](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Stephen\_Tan](https://forum.ansible.com/u/Stephen_Tan)\
**Post date:** [November 24, 2014, 5:52pm UTC](https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857/4 "2014-11-24T17:52:54Z")

</div>

Thank you for the response - I see - I’ll test later - I am sure you are correct.

thanks again

---

<div class="post-metadata">

**Author:** ![Stephen\_Tan](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Stephen\_Tan](https://forum.ansible.com/u/Stephen_Tan)\
**Post date:** [November 24, 2014, 5:53pm UTC](https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857/5 "2014-11-24T17:53:01Z")

</div>

Thank you for the response - I see - I’ll test later - I am sure you are correct.

thanks again

---

<div class="post-metadata">

**Author:** ![Stephen\_Tan](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Stephen\_Tan](https://forum.ansible.com/u/Stephen_Tan)\
**Post date:** [November 25, 2014, 8:56am UTC](https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857/6 "2014-11-25T08:56:16Z")

</div>

Yes, that worked - I am kicking myself a bit - thanks again. It’s a bit of a counter-intuitive view to the data structure for me. One you explained the logic, it became clear. Thanks for the explanation.

---

<div class="post-metadata">

**Author:** ![Stephen\_Tan](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Stephen\_Tan](https://forum.ansible.com/u/Stephen_Tan)\
**Post date:** [November 25, 2014, 8:57am UTC](https://forum.ansible.com/t/seemingly-simple-template-driving-me-mad/16857/7 "2014-11-25T08:57:31Z")

</div>

Yes, that worked - I am kicking myself a bit - thanks again. It’s a bit of a counter-intuitive view to the data structure for me. One you explained about fred being the key it became clear. Thanks for the explanation.
