Nested variables and regex_replace()

2,265 views
Skip to first unread message

Josh Smift

unread,
Dec 2, 2014, 4:04:19 PM12/2/14
to ansible...@googlegroups.com
I have some variables like 'service', which can be "abc" or "xyz", and
'site', which can be "bos" or "sfo", on different hosts. I have hosts
whose FQDNs are like service-site-thing-N.site.service.care.com, which
have those those variables set accordingly, in a group like 'things'.

My use case is that I want to create CNAME records, using the route53
module, for names like thing-N.site.service.care.com, so that if you're on
a host with site.service.care.com in its resolv.conf search path, you can
refer to "thing-N" and have that resolve to service-site-thing-N.

For now, to take the route53 aspect out of the equation (I just mentioned
that for context), I'm just debugging, and it works if I hardcode the
services and sites, but not if I try to use the variables.

Here's a playbook:

- hosts: things

vars: { service="abc", site="bos" }

tasks:
- debug: msg={{ ansible_hostname | regex_replace("^abc-bos-", '') }}
- debug: msg={{ ansible_hostname | regex_replace("^{{service}}-{{site}}-", '') }}

Here's what that does:

TASK: [debug msg={{ ansible_hostname | regex_replace("^abc-bos-", '') }}] *****
ok: [abc-bos-thing-01] => {
"msg": "thing-01"
}
ok: [abc-bos-thing-02] => {
"msg": "thing-02"
}

TASK: [debug msg={{ ansible_hostname | regex_replace("^{{service}}-{{site}}-", '') }}] ***
ok: [abc-bos-thing-01] => {
"msg": "abc-bos-thing-01"
}
ok: [abc-bos-thing-02] => {
"msg": "abc-bos-thing-02"
}

I was hoping the second one would be the same as the first. I tried a
couple of variations on the syntax of the regex_replace in the second
case, but no luck.

I have the same problem if I try to use the

{{ ansible_hostname | regex_replace("^{{service}}-{{site}}-", '') }}

construction when I call the route53 module -- the regexp isn't actually
replaced.

Is there another/better way to do this?

-Josh (j...@care.com)



This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

Matt Martz

unread,
Dec 2, 2014, 4:18:37 PM12/2/14
to ansible...@googlegroups.com
The problem is that cannot nest jinja brackets inside of brackets.  Your following example:

  {{ ansible_hostname | regex_replace("^{{service}}-{{site}}-", '') }}

Should probably be:

  {{ ansible_hostname | regex_replace("^" + service + "-" + site + "-", '') }}

Inside of {{ }} variables are already expanded.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/21630.10549.573264.896397%40gargle.gargle.HOWL.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Josh Smift

unread,
Dec 2, 2014, 4:33:34 PM12/2/14
to ansible...@googlegroups.com
MM> The problem is that cannot nest jinja brackets inside of brackets.

Ah! Right, because brackets mean "Jinja", not just "variable". Still new
here. :^)

MM> Your following example:
MM>
MM> {{ ansible_hostname | regex_replace("^{{service}}-{{site}}-", '') }}
MM>
MM> Should probably be:
MM>
MM> {{ ansible_hostname | regex_replace("^" + service + "-" + site + "-", '')
MM> }}
MM>
MM> Inside of {{ }} variables are already expanded.

That works! Well, it turns out I was defining my variables wrong in this
example, but when I changed

vars: { service="abc", site="bos" }

to the less wrong

vars:
service: "abc"
site: "bos"

it worked perfectly, both in the debug example and in the actual route53
module.

Yay Ansible! Yay ansible-project! Thanks Matt!
Reply all
Reply to author
Forward
0 new messages