# Nested variable

**URL:** <https://forum.ansible.com/t/nested-variable/27607>\
**Category:** Ansible Project\
**Created:** [May 23, 2018, 8:04pm UTC](https://forum.ansible.com/t/nested-variable/27607 "2018-05-23T20:04:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 23, 2018, 8:04pm UTC](https://forum.ansible.com/t/nested-variable/27607/1 "2018-05-23T20:04:15Z")

</div>

Hello guys,

I’ve got this set\_fact:

` hosted_zone_id: "{{ dns_results | json_query('HostedZones[?Name==`[mydomain.com](http://mydomain.com).`].[Id]') | regex_replace ('/hostedzone/', '') }}" `

But i want to get the id from any of my domains, so i want to use a variable:

`hosted_zone_name: "mydomain.com"`

and do something like:

` hosted_zone_id: "{{ dns_results | json_query('HostedZones[?Name==`{{ hosted\_zone\_name }}.`].[Id]') | regex_replace ('/hostedzone/', '') }}" `

But it looks like nested variables like that doesn’t work…

Any suggestions?

Thanks!

David

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 23, 2018, 8:40pm UTC](https://forum.ansible.com/t/nested-variable/27607/2 "2018-05-23T20:40:36Z")

</div>

Solved!

`

- set\_fact:  
hosted\_zone\_id: “{{ dns\_results | json\_query(‘HostedZones[?Name==`(lookup('vars', hosted_zone_name)).`].[Id]’) | regex\_replace (‘/hostedzone/’, ‘’) }}”  
`

For future reference.

Thanks

David

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 23, 2018, 10:23pm UTC](https://forum.ansible.com/t/nested-variable/27607/3 "2018-05-23T22:23:27Z")

</div>

Spoke too soon… That doesn’t work either…

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 24, 2018, 1:11am UTC](https://forum.ansible.com/t/nested-variable/27607/4 "2018-05-24T01:11:31Z")

</div>

Try this

`

- set\_fact:  
hosted\_zone\_id: “{{ dns\_results | json\_query(‘HostedZones[?Name==’ ~ lookup(‘vars’, hosted\_zone\_name) ~ ‘].[Id]’) | regex\_replace(‘/hostedzone/’, ‘’) }}”

`

You need to escape outside of the quoted arg of json\_query and then run the lookup. Note I haven’t tested this

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 24, 2018, 4:46pm UTC](https://forum.ansible.com/t/nested-variable/27607/5 "2018-05-24T16:46:46Z")

</div>

Hey Jordan,

Thanks for replying!  
There were “`” missing, worked beautifully after that!

`

- set\_fact:  
hosted\_zone\_id: “{{ dns\_results | json\_query(‘HostedZones[?Name==`' ~ hosted_zone_name ~ '.`].[Id]’) | regex\_replace(‘/hostedzone/’, ‘’) }}”  
`

“~” escapes it, so there’s no need to do a lookup, works nice

Thanks!

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 24, 2018, 8:35pm UTC](https://forum.ansible.com/t/nested-variable/27607/6 "2018-05-24T20:35:51Z")

</div>

ahh yes I should have seen you just wanted a nested var, just an FYI ~ is similar to + but coerces the variable to a string so you don’t have to do that yourself. Usually + works but ~ is safer.

Thanks

Jordan
