Convert string to yaml path

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,

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

Hello,

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

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

Have a nice day