Nested variable

Hello guys,

I’ve got this set_fact:

hosted_zone_id: "{{ dns_results | json_query('HostedZones[?Name==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

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

Spoke too soon… That doesn’t work either…

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

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!

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