Seemingly simple template driving me mad

61 views
Skip to first unread message

Stephen Tan

unread,
Nov 23, 2014, 6:35:36 AM11/23/14
to ansible...@googlegroups.com
Hi - I'm doing something which seems very simple - trying to create a file from a jinja template.

Here are my vars:

export_root:
  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] => {'msg': "One or more undefined variables: 'str object' has no attribute 'options'", 'failed': True}
fatal: [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

Michael Peters

unread,
Nov 24, 2014, 9:44:00 AM11/24/14
to ansible...@googlegroups.com
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:

{{ fred }}
{{ exports[fred].options }}
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/0b1cdc31-1ab2-449b-a91f-6f138cc5516c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Quentin Stafford-Fraser

unread,
Nov 24, 2014, 9:47:18 AM11/24/14
to ansible...@googlegroups.com
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

Stephen Tan

unread,
Nov 24, 2014, 12:52:54 PM11/24/14
to ansible...@googlegroups.com
Thank you for the response - I see - I'll test later - I am sure you are correct. 

thanks again

Stephen Tan

unread,
Nov 24, 2014, 12:53:01 PM11/24/14
to ansible...@googlegroups.com
Thank you for the response - I see - I'll test later - I am sure you are correct. 

thanks again

On Monday, 24 November 2014 14:47:18 UTC, Quentin Stafford-Fraser wrote:

Stephen Tan

unread,
Nov 25, 2014, 3:56:17 AM11/25/14
to ansible...@googlegroups.com
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.

Stephen Tan

unread,
Nov 25, 2014, 3:57:31 AM11/25/14
to ansible...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages