# Nested variables and regex\_replace()

**URL:** <https://forum.ansible.com/t/nested-variables-and-regex-replace/16942>\
**Category:** Ansible Project\
**Created:** [December 2, 2014, 9:03pm UTC](https://forum.ansible.com/t/nested-variables-and-regex-replace/16942 "2014-12-02T21:03:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [December 2, 2014, 9:03pm UTC](https://forum.ansible.com/t/nested-variables-and-regex-replace/16942/1 "2014-12-02T21:03:49Z")

</div>

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:

&nbsp;&nbsp;- hosts: things

&nbsp;&nbsp;&nbsp;&nbsp;vars: { service="abc", site="bos" }

&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg={{ ansible\_hostname | regex\_replace("^abc-bos-", '') }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg={{ ansible\_hostname | regex\_replace("^{{service}}-{{site}}-", '') }}

Here's what that does:

&nbsp;&nbsp;TASK: [debug msg={{ ansible\_hostname | regex\_replace("^abc-bos-", '') }}] \*\*\*\*\*  
&nbsp;&nbsp;ok: [abc-bos-thing-01] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "thing-01"  
&nbsp;&nbsp;}  
&nbsp;&nbsp;ok: [abc-bos-thing-02] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "thing-02"  
&nbsp;&nbsp;}

&nbsp;&nbsp;TASK: [debug msg={{ ansible\_hostname | regex\_replace("^{{service}}-{{site}}-", '') }}] \*\*\*  
&nbsp;&nbsp;ok: [abc-bos-thing-01] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "abc-bos-thing-01"  
&nbsp;&nbsp;}  
&nbsp;&nbsp;ok: [abc-bos-thing-02] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "abc-bos-thing-02"  
&nbsp;&nbsp;}

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

&nbsp;&nbsp;{{ 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?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@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.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 2, 2014, 9:18pm UTC](https://forum.ansible.com/t/nested-variables-and-regex-replace/16942/2 "2014-12-02T21:18:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [December 2, 2014, 9:33pm UTC](https://forum.ansible.com/t/nested-variables-and-regex-replace/16942/3 "2014-12-02T21:33:16Z")

</div>

> The problem is that cannot nest jinja brackets inside of brackets.

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

> Your following example:
> 
> &nbsp;&nbsp;{{ ansible\_hostname | regex\_replace("^{{service}}-{{site}}-", '') }}
> 
> Should probably be:
> 
> &nbsp;&nbsp;{{ ansible\_hostname | regex\_replace("^" + service + "-" + site + "-", '')  
> }}
> 
> 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

&nbsp;&nbsp;vars: { service="abc", site="bos" }

to the less wrong

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;service: "abc"  
&nbsp;&nbsp;&nbsp;&nbsp;site: "bos"

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

Yay Ansible! Yay ansible-project! Thanks Matt!

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@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.
