Nested variable

14 views
Skip to first unread message

David Villasmil

unread,
May 23, 2018, 4:04:16 PM5/23/18
to Ansible Project
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


David Villasmil

unread,
May 23, 2018, 4:40:36 PM5/23/18
to Ansible Project
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

David Villasmil

unread,
May 23, 2018, 6:23:27 PM5/23/18
to Ansible Project
Spoke too soon... That doesn't work either...


On Wednesday, May 23, 2018 at 10:04:16 PM UTC+2, David Villasmil wrote:

Jordan Borean

unread,
May 23, 2018, 9:11:32 PM5/23/18
to Ansible Project
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

David Villasmil

unread,
May 24, 2018, 12:46:46 PM5/24/18
to Ansible Project
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!

Jordan Borean

unread,
May 24, 2018, 4:35:52 PM5/24/18
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages