Nested variables and regex_replace()

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 (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.

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.

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:

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

Should probably be:

  {{ 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

  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!

                                      -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.