variable interpolation in hostvars

219 views
Skip to first unread message

Matt Peterson

unread,
Jul 16, 2014, 4:40:32 PM7/16/14
to ansible...@googlegroups.com
Given the following example:

foo: "y"
boo: "x is {{ foo }}"

This will renders in a template "x is y" (when referenced as {{ boo }}). However when called as hostvars[host]['boo'] this renders as "x is {{ foo }}". From what I can tell this is an interpolation or merging problem. I've tried different YAML formatting options to no avail. Appreciate any help or direction to share, thank you.

--Matt

Brian Coca

unread,
Jul 16, 2014, 4:48:22 PM7/16/14
to ansible...@googlegroups.com
the reason it 'works' in some places is because there is double interpolation in some directives and templates, when accessing the value directly you CORRECTLY get "x is {{ foo }}", YAML will NOT evaluate this on loading, this evaluates in 'templated' directives and templates using jinja2.


--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Tomasz Kontusz

unread,
Jul 16, 2014, 5:02:03 PM7/16/14
to ansible...@googlegroups.com
Nothing gets templated when accessed through hostvars, see https://github.com/ansible/ansible/issues/7844 (disclaimer: I've opened this issue).

--Matt
--
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/CAFN0R25WbD7P_SdMm26J0rN9Cmj%2BKOEHvF2yAGuy%2B7eS-V4zQA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Jul 16, 2014, 7:51:27 PM7/16/14
to ansible...@googlegroups.com
Note that comments on old tickets are not something that will get red, there are simply too many posts of this nature.

Original ticket was rather unclear about what it was about, so it got closed.

To be honest, nested variable evaluation in hostvars is a can of worms to resolve and I don't think it will be a thing.




Tomasz Kontusz

unread,
Jul 17, 2014, 2:06:20 AM7/17/14
to ansible...@googlegroups.com


Michael DeHaan <mic...@ansible.com> napisał:
>To be honest, nested variable evaluation in hostvars is a can of worms
>to
>resolve and I don't think it will be a thing.
Can you point to any specific problems? I'm using a filter that just resolves host's vars in the context of hostvars[host], and it seems to work ok
><https://groups.google.com/d/msgid/ansible-project/CAFN0R25WbD7P_SdMm26J0rN9Cmj%2BKOEHvF2yAGuy%2B7eS-V4zQA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> --
>> 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/53C6E845.4050709%40gmail.com
>>
><https://groups.google.com/d/msgid/ansible-project/53C6E845.4050709%40gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>

--
Wysłane za pomocą K-9 Mail.

Michael DeHaan

unread,
Jul 17, 2014, 8:29:49 AM7/17/14
to ansible...@googlegroups.com
I could be very very wrong about my assumption.

Can we see the source to your filter?  It might be instructive about what this might take.




Tomasz Kontusz

unread,
Jul 17, 2014, 4:34:48 PM7/17/14
to ansible...@googlegroups.com
Dnia czw, 17 lip 2014, 14:29:45 Michael DeHaan pisze:
> I could be very very wrong about my assumption.
>
> Can we see the source to your filter? It might be instructive about
> what this might take.
I kind of hope you are, as it would be really handy to have this in
Ansible core ;-)

My filter is attached. I'm afraid it ignores variables set with Jinja2
in current template (as it only looks at the context with "basedir"
attribute), and I'm not sure about role's and play's vars. So far I've
only used it to access variables that could be resolved without even
looking at playbook, only with knowledge from inventory and
{group,host}_vars.
Example usage: {{ ('other_host' | get_hostvars).other_hosts_variable }}


> On Thu, Jul 17, 2014 at 2:06 AM, Tomasz Kontusz
> <tomasz....@gmail.com <mailto:tomasz....@gmail.com>> wrote:
>
>
>
> Michael DeHaan <mic...@ansible.com <mailto:mic...@ansible.com>>
> napisał:
> >To be honest, nested variable evaluation in hostvars is a can of
> worms
> >to
> >resolve and I don't think it will be a thing.
> Can you point to any specific problems? I'm using a filter that
> just resolves host's vars in the context of hostvars[host], and it
> seems to work ok
>
> >
> >
> >On Wed, Jul 16, 2014 at 5:01 PM, Tomasz Kontusz
> ><tomasz....@gmail.com <mailto:tomasz....@gmail.com>>
> <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> >> To post to this group, send email to
> >ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> >> To view this discussion on the web visit
> >>
> >https://groups.google.com/d/msgid/ansible-project/CAFN0R25WbD7P_SdMm26J0rN9Cmj%2BKOEHvF2yAGuy%2B7eS-V4zQA%40mail.gmail.com
> >>
> ><https://groups.google.com/d/msgid/ansible-project/CAFN0R25WbD7P_SdMm26J0rN9Cmj%2BKOEHvF2yAGuy%2B7eS-V4zQA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> >> .
> >> For more options, visit https://groups.google.com/d/optout.
> >>
> >>
> >> --
> >> 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
> <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> >> To post to this group, send email to
> >ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> >> To view this discussion on the web visit
> >>
> >https://groups.google.com/d/msgid/ansible-project/53C6E845.4050709%40gmail.com
> >>
> ><https://groups.google.com/d/msgid/ansible-project/53C6E845.4050709%40gmail.com?utm_medium=email&utm_source=footer>
> >> .
> >>
> >> For more options, visit https://groups.google.com/d/optout.
> >>
>
> --
> Wysłane za pomocą K-9 Mail.
>
> --
> 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
> <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To post to this group, send email to
> ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> --
> 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
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com
> <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgy%2BRPi4FRNyr%3D5i2-o9WZ7V%3DyeFcC%3DzzOhAoAO8vQRQjA%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgy%2BRPi4FRNyr%3D5i2-o9WZ7V%3DyeFcC%3DzzOhAoAO8vQRQjA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
hostvars.py

Michael DeHaan

unread,
Jul 17, 2014, 9:41:11 PM7/17/14
to ansible...@googlegroups.com
Not too complicated.  

I'd worry abit about the efficiency as this would need to be calculated a fair amount, but it's something we can look into.  

Note there is something of a backlog, but I'd file a ticket with info if not already and we can explore it.


On Thu, Jul 17, 2014 at 4:34 PM, Tomasz Kontusz <tomasz....@gmail.com> wrote:
Dnia czw, 17 lip 2014, 14:29:45 Michael DeHaan pisze:

I could be very very wrong about my assumption.

Can we see the source to your filter?  It might be instructive about
what this might take.
I kind of hope you are, as it would be really handy to have this in Ansible core ;-)

My filter is attached. I'm afraid it ignores variables set with Jinja2 in current template (as it only looks at the context with "basedir" attribute), and I'm not sure about role's and play's vars. So far I've only used it to access variables that could be resolved without even looking at playbook, only with knowledge from inventory and {group,host}_vars.
Example usage: {{ ('other_host' | get_hostvars).other_hosts_variable }}


On Thu, Jul 17, 2014 at 2:06 AM, Tomasz Kontusz
<tomasz....@gmail.com <mailto:tomasz.kontusz@gmail.com>> wrote:



    Michael DeHaan <mic...@ansible.com <mailto:mic...@ansible.com>>

    napisał:
    >To be honest, nested variable evaluation in hostvars is a can of
    worms
    >to
    >resolve and I don't think it will be a thing.
    Can you point to any specific problems? I'm using a filter that
    just resolves host's vars in the context of hostvars[host], and it
    seems to work ok

    >
    >
    >On Wed, Jul 16, 2014 at 5:01 PM, Tomasz Kontusz

    >> To post to this group, send email to

    >> To view this discussion on the web visit
    >>
    >https://groups.google.com/d/msgid/ansible-project/CAFN0R25WbD7P_SdMm26J0rN9Cmj%2BKOEHvF2yAGuy%2B7eS-V4zQA%40mail.gmail.com
    >>
    ><https://groups.google.com/d/msgid/ansible-project/CAFN0R25WbD7P_SdMm26J0rN9Cmj%2BKOEHvF2yAGuy%2B7eS-V4zQA%40mail.gmail.com?utm_medium=email&utm_source=footer>
    >> .
    >> For more options, visit https://groups.google.com/d/optout.
    >>
    >>
    >>  --
    >> 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

    >> To post to this group, send email to

    >> To view this discussion on the web visit
    >>
    >https://groups.google.com/d/msgid/ansible-project/53C6E845.4050709%40gmail.com
    >>
    ><https://groups.google.com/d/msgid/ansible-project/53C6E845.4050709%40gmail.com?utm_medium=email&utm_source=footer>
    >> .
    >>
    >> For more options, visit https://groups.google.com/d/optout.
    >>

    --
    Wysłane za pomocą K-9 Mail.

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

    To post to this group, send email to
--
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
--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/53C83358.7010605%40gmail.com.
Reply all
Reply to author
Forward
0 new messages