# Convert string to yaml path

**URL:** <https://forum.ansible.com/t/convert-string-to-yaml-path/32933>\
**Category:** Ansible Project\
**Created:** [May 14, 2020, 2:35pm UTC](https://forum.ansible.com/t/convert-string-to-yaml-path/32933 "2020-05-14T14:35:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dumast\_Axel](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@Dumast\_Axel](https://forum.ansible.com/u/Dumast_Axel)\
**Post date:** [May 14, 2020, 2:35pm UTC](https://forum.ansible.com/t/convert-string-to-yaml-path/32933/1 "2020-05-14T14:35:14Z")

</div>

Hello,

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

I have 2 yaml files :

Master.yml:  
 ![](https://us1.discourse-cdn.com/ansible/original/2X/1/197fd98022fd9dc050bca7f6112eef94f54386da.png)

values.yml:

 ![](https://us1.discourse-cdn.com/ansible/original/2X/c/c753dde332c6ef3f08342c0df79ca11405704309.png)  
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 :

 ![](https://us1.discourse-cdn.com/ansible/original/2X/5/52548d9fc4eec797f54bf2aebf588a77d61e0369.png)

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,

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 14, 2020, 2:50pm UTC](https://forum.ansible.com/t/convert-string-to-yaml-path/32933/2 "2020-05-14T14:50:42Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Dumast\_Axel](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@Dumast\_Axel](https://forum.ansible.com/u/Dumast_Axel)\
**Post date:** [May 14, 2020, 3:15pm UTC](https://forum.ansible.com/t/convert-string-to-yaml-path/32933/3 "2020-05-14T15:15:56Z")

</div>

Hello,

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

Thank you very much, you saved me !! 🙂

Have a nice day
