Seemingly simple template driving me mad

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] => {‘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

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 }}

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

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

thanks again

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

thanks again

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.

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.