# Escaping problem with regular expression and variables

**URL:** <https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713>\
**Category:** Ansible Project\
**Created:** [October 18, 2013, 11:11am UTC](https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713 "2013-10-18T11:11:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![christian\_studer](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@christian\_studer](https://forum.ansible.com/u/christian_studer)\
**Post date:** [October 18, 2013, 11:11am UTC](https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713/1 "2013-10-18T11:11:43Z")

</div>

Hello,

I’m taking my first steps with Ansible (1.4 dev) and love it so far. However I hit a snag escaping special characters with a particular line. I’m trying to insert a line into a YAML file:

- name: Insert clustername {{ clustername }}  
lineinfile:  
dest=/etc/elasticsearch/elasticsearch.yml  
regexp=‘^cluster.name: {{ clustername }}’  
line=‘cluster.name: “{{ clustername }}”’  
state=present

Both the regexp and the line expression give an error, either because of the variable brackets or because of the colon. (clustername is a simple string, no special characters).

I’ve tried a combination of single and double dashes, escaping just the regular expression or the whole argument, but can’t get it to work.

Any ideas?

Thank you,  
christian

---

<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:** [October 18, 2013, 4:29pm UTC](https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713/2 "2013-10-18T16:29:35Z")

</div>

“Both the regexp and the line expression give an error”

Which error?

When reporting a problem on this list, always share the error.

---

<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:** [October 20, 2013, 8:47pm UTC](https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713/3 "2013-10-20T20:47:22Z")

</div>

Try YAML escaping the arguments with a ‘\>’. That is,

lineinfile: \>

dest=/etc/elasticsearch/elasticsearch.yml  
regexp=‘\[1\]([http://cluster.name](http://cluster.name)): {{ clustername }}’  
line=‘[cluster.name](http://cluster.name): “{{ clustername }}”’  
state=present

* * *

1. cluster.name

---

<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:** [October 22, 2013, 1:14am UTC](https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713/4 "2013-10-22T01:14:49Z")

</div>

To be clear, that’s not escaping, that’s a line continuation.

---

<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:** [October 22, 2013, 2:40am UTC](https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713/5 "2013-10-22T02:40:14Z")

</div>

Not quite - it does escape as well. That’s why the ': ’ sequence is not triggered.

K

---

<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:** [October 22, 2013, 2:53am UTC](https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713/6 "2013-10-22T02:53:01Z")

</div>

More so that it knows you aren’t going to specifying another hash value.

See section 2.3 here:

[http://www.yaml.org/spec/1.2/spec.html](http://www.yaml.org/spec/1.2/spec.html)

---

<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:** [October 22, 2013, 4:06am UTC](https://forum.ansible.com/t/escaping-problem-with-regular-expression-and-variables/13713/7 "2013-10-22T04:06:03Z")

</div>

True. I think said escaping when I meant quoting. Would be better to say it forces the following block to be interpreted as a scalar.

I’ve taken to always using the ‘\>’ quoting mechanism even when not needed, because it avoids the whole confusing ‘colon’ + ‘space’ issue.

Enough said.

K
