Convert string to yaml path

28 views
Skip to first unread message

Dumast Axel

unread,
May 14, 2020, 10:35:14 AM5/14/20
to Ansible Project
Hello,

I search since 2 days but I don't find any solution:

I have 2 yaml files :

Master.yml:

values.yml:
I try to catch hostname1 and hostname2's list using command like msg"{{ firewall.port.{{ansible_hostname}} }}".

The problem is that {{ansible_hostname}} is a string value and "firewall.port" is a yaml_path value. It's not the same type of variable.
So I can't display hostname1 and hostname2 list values using "firewall.port.{{ansible_hostname}}

I tried to have a simple string like this :

But when I run ansible, path_items return a string (firewall.port.hostname1) but not the yaml list.

My question is: How can I do to display hostame1 and hostname2 values using {{ansible_hostname}} ?

Thanks in advance,

Stefan Hornburg (Racke)

unread,
May 14, 2020, 10:51:10 AM5/14/20
to ansible...@googlegroups.com
On 5/14/20 4:35 PM, Dumast Axel wrote:
> Hello,
>
> I search since 2 days but I don't find any solution:
>
> I have 2 yaml files :
>
> Master.yml:
>
> values.yml:
> I try to catch hostname1 and hostname2's list using command like *msg"{{ firewall.port.{{ansible_hostname}} }}"*.
>
> The problem is that {{ansible_hostname}} is a string value and "firewall.port" is a yaml_path value. It's not the same
> type of variable.
> So I can't display hostname1 and hostname2 list values using "firewall.port.{{ansible_hostname}}

You can't wrap Jinja inside Jinja, but you can try to use either vars or Python method:

{{ firewall.port[vars[ansible_hostname]] }}

{{ firewall.port.get(ansible_hostname) }}

Regards
Racke

>
> I tried to have a simple string like this :
>
> But when I run ansible, path_items return a string (firewall.port.hostname1) but not the yaml list.
>
> My question is: How can I do to display hostame1 and hostname2 values using {{ansible_hostname}} ?
>
> Thanks in advance,
>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/eb21155e-0869-47b8-8e7b-410a26b67d85%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/eb21155e-0869-47b8-8e7b-410a26b67d85%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Dumast Axel

unread,
May 14, 2020, 11:15:56 AM5/14/20
to Ansible Project
Hello,

It worked !
I searched for 2 days but the solution was simple...

Thank you very much, you saved me !! :)

Have a nice day

Le jeudi 14 mai 2020 16:51:10 UTC+2, Stefan Hornburg (Racke) a écrit :
You can't wrap Jinja inside Jinja, but you can try to use either vars or Python method:

{{ firewall.port[vars[ansible_hostname]] }}

{{ firewall.port.get(ansible_hostname) }}

Regards
         Racke

>
> I tried to have a simple string like this :
>
> But when I run ansible, path_items return a string (firewall.port.hostname1) but not the yaml list.
>
> My question is: How can I do to display hostame1 and hostname2 values using {{ansible_hostname}} ?
>
> Thanks in advance,
>
> --
> 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
Reply all
Reply to author
Forward
0 new messages