# Escaping $ inside a variable

**URL:** <https://forum.ansible.com/t/escaping-inside-a-variable/12001>\
**Category:** Ansible Project\
**Created:** [October 19, 2012, 9:08pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001 "2012-10-19T21:08:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janis\_Gengeris](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Janis\_Gengeris](https://forum.ansible.com/u/Janis_Gengeris)\
**Post date:** [October 19, 2012, 9:08pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/1 "2012-10-19T21:08:17Z")

</div>

I have variable defined in playbook like this:

vars:

- net\_status\_match\_02: “\[1\]+address ${service\_net\_ip}$”

How to properly escape the ‘$’ sign? The backslash is not working for me.

Thanks,  
–janis

* * *

1. [:blank:]

---

<div class="post-metadata">

**Author:** ![CWS](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CWS](https://forum.ansible.com/u/CWS)\
**Post date:** [October 19, 2012, 10:06pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/2 "2012-10-19T22:06:07Z")

</div>

You're probably wanting a double backslash. Or triple, I can never remember which.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [October 19, 2012, 10:23pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/3 "2012-10-19T22:23:54Z")

</div>

Jānis Ģeņģeris wrote:

> I have variable defined in playbook like this:
> 
> vars:  
> &nbsp;&nbsp;&nbsp;- net\_status\_match\_02: "^[[:blank:]]+address ${service\_net\_ip}\$"
> 
> How to properly escape the '$' sign? The backslash is not working for me.

That depends entirely on what you want to do with it. Ansible's variable  
replacer will not touch any $ that is not followed by a valid variable name.

Daniel

---

<div class="post-metadata">

**Author:** ![Janis\_Gengeris](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Janis\_Gengeris](https://forum.ansible.com/u/Janis_Gengeris)\
**Post date:** [October 20, 2012, 9:17am UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/4 "2012-10-20T09:17:42Z")

</div>

Somehow this is not working for me.

Using this:

- net\_status\_match\_02: “\[1\]+address ${service\_net\_ip}$”

I get this error:

Traceback (most recent call last):  
File “/home/j9/software/ansible/bin/ansible-playbook”, line 164, in   
sys.exit(main(sys.argv[1:]))  
File “/home/j9/software/ansible/bin/ansible-playbook”, line 135, in main  
pb.run()  
File “/home/j9/software/ansible/lib/ansible/playbook/ **init**.py”, line 170, in run  
play = Play(self, play\_ds, play\_basedir)  
File “/home/j9/software/ansible/lib/ansible/playbook/play.py”, line 83, in **init**  
self.\_tasks = self.\_load\_tasks(self.\_ds, ‘tasks’)  
File “/home/j9/software/ansible/lib/ansible/playbook/play.py”, line 123, in \_load\_tasks  
results.append(Task(self,x,module\_vars=task\_vars))  
File “/home/j9/software/ansible/lib/ansible/playbook/task.py”, line 142, in **init**  
self.action = utils.template(None, self.action, self.module\_vars)  
File “/home/j9/software/ansible/lib/ansible/utils.py”, line 390, in template  
text = varReplace(unicode(text), vars, expand\_lists=expand\_lists)  
File “/home/j9/software/ansible/lib/ansible/utils.py”, line 309, in varReplace  
replacement = varReplace(replacement, vars, depth=depth+1, expand\_lists=expand\_lists)  
File “/home/j9/software/ansible/lib/ansible/utils.py”, line 296, in varReplace  
m = \_varFind(raw)  
File “/home/j9/software/ansible/lib/ansible/utils.py”, line 249, in \_varFind  
if text[var\_start] == ‘{’:  
IndexError: string index out of range

When using backslash escape:

- net\_status\_match\_02: “\[2\]+address ${service\_net\_ip}$”

This error:

ERROR: Syntax Error while loading YAML script, /home/j9/cfg/ansible/playbooks/network\_config.yml  
Note: The error may actually appear before this position: line 31, column 68

- net\_status\_match\_02: “\[3\]+address ${service\_net\_ip}$”  
^

* * *

1. [:blank:] 

2. [:blank:] 

3. [:blank:]

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 20, 2012, 11:26am UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/5 "2012-10-20T11:26:16Z")

</div>

Hi Jānis,

Please make sure there's a bug filed in github for this. It looks  
like we need to improve the string replacement code. If you can paste  
as much of your playbook as possible into the ticket that would be  
great.

(You will probably have to indent each line in github by four spaces  
to get it to render correctly in the browser.)

[http://github.com/ansible/ansible](http://github.com/ansible/ansible), click on "issues".

your problem \*MAY\* go away if you change that to $service\_net\_ip, but  
I can't be positive without trying it. In any event, paste the full  
playbook, redacting what you need to, and we can take a look.

--Michael

---

<div class="post-metadata">

**Author:** ![Kesten\_Broughton2](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Kesten\_Broughton2](https://forum.ansible.com/u/Kesten_Broughton2)\
**Post date:** [December 10, 2013, 9:48pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/6 "2013-12-10T21:48:12Z")

</div>

Is there a solution to this?

I am trying to add to PATH and can’t keep ansible from evaluating a $  
I’ve tried ‘$’ and several other things.

I’m trying to achieve the following:

MYPATH is an env variable on a remote host.

I want to add a string defined in vars/main.yml to a line in .bash\_profile

export PATH=$PATH:$MYPATH

In full detail here

- name: Add git scripts to .bash\_profile  
lineinfile: dest=“{{home\_prefix}}/{{ansible\_ssh\_user}}/.bash\_profile”  
regexp=“{{item.regexp}}”  
line=“{{item.line}}”  
with\_items:
- {  
regexp: “roles/git/files”,  
line: ‘{{path\_to\_git\_extras}}’  
}

with path\_to\_git\_extras defined in vars/main.yml

path\_to\_git\_extras: ‘PATH=\$PATH:\$ANSIBLE\_21CT\_HOME/roles/git/files’

# I thought jinja filters might work here but i get variable “string” undefined

path\_to\_git\_extras: ‘{{string(“export PATH=$PATH:$ANSIBLE\_21CT\_HOME/roles/git/files”)}}’

The latter is what i really want. A function or something that says “Don’t evaluate anything inside here EVER”

I didn’t see an issue on git. Still want one filed?

kesten

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [December 10, 2013, 10:03pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/7 "2013-12-10T22:03:51Z")

</div>

does '\>' quoting help?

&nbsp;&nbsp;&nbsp;&nbsp;path\_to\_git\_extras: \>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PATH=$PATH:$ANSIBLE\_21CT\_HOME/roles/git/files

K

---

<div class="post-metadata">

**Author:** ![kesten\_broughton1](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kesten\_broughton1](https://forum.ansible.com/u/kesten_broughton1)\
**Post date:** [December 11, 2013, 6:13pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/8 "2013-12-11T18:13:43Z")

</div>

unfortunately no.

with  
path\_to\_git\_extras: \>  
PATH=$PATH:$ANSIBLE\_21CT\_HOME/roles/git/files

when the file gets templated to the remote, it contains the value of ANSIBLE\_21CT\_HOME evaluated on the local machine. The remote has the correct ANSIBLE\_21CT\_HOME path but it never gets a chance.

I tried something new with jinja2 templating which i was sure would work but it seems that ansible $variable substitution happens in a peculiar way.

I tried this in my file template, but still get the local $ANSIBLE\_21CT\_HOME evaluated.

TEST 1  
{% filter string %}  
export PATH=“$PATH:$ANSIBLE\_21CT\_HOME/roles/git/files”  
{% endfilter %}

TEST2  
Testing with upper, i see the export line uppercased except for the $ANSIBLE\_21CT\_HOME portion.

{% filter upper %}  
export PATH=“$PATH:$ANSIBLE\_21CT\_HOME/roles/git/files”  
{% endfilter %}

Result  
EXPORT PATH=“$PATH:/Users/kbroughton/21ct-ansible/ROLES/GIT/FILES”

So it seems that in TEST2 that the jinja2 filter gets applied BEFORE the ansible variable substitution,  
but it appears that in TEST1 the jinja2 filter comes after, or is not being applied somehow.

Peculiar.

kesten

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [December 11, 2013, 9:28pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/9 "2013-12-11T21:28:44Z")

</div>

How about the following:

&nbsp;&nbsp;&nbsp;&nbsp;path\_to\_git\_extras: "{{ 'EXPORT PATH=$' + 'PATH:' + '$' +  
'ANSIBLE\_21CT\_HOME/roles/git/file' }}"

Or something similar in a template.

The problem will probably just go away once the old style '$' variable  
syntax is retired.

K

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925

---

<div class="post-metadata">

**Author:** ![kesten\_broughton1](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kesten\_broughton1](https://forum.ansible.com/u/kesten_broughton1)\
**Post date:** [December 11, 2013, 10:28pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/10 "2013-12-11T22:28:50Z")

</div>

Amazingly no! ansible catenates the strings, then evaluates it with the local variable.

I wonder if there is an option that can be set that will allow the new behavior - {{ only with no $ vars - that can be used for now?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 11, 2013, 11:01pm UTC](https://forum.ansible.com/t/escaping-inside-a-variable/12001/11 "2013-12-11T23:01:31Z")

</div>

Pretty sure this isn’t going through the local shell before getting to the git module.

You can debug with setting ANSIBLE\_KEEP\_REMOTE\_FILES=1 in the environment and running with -vvv and looking at that module it outputs on the remote system.

If you see that behavior, please file a ticket if you are seeing this on Ansible 1.4.X or higher.
