# special characters for lineinfile

**URL:** <https://forum.ansible.com/t/special-characters-for-lineinfile/13395>\
**Category:** Ansible Project\
**Created:** [August 30, 2013, 6:38pm UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395 "2013-08-30T18:38:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Schneider](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Jeremy\_Schneider](https://forum.ansible.com/u/Jeremy_Schneider)\
**Post date:** [August 30, 2013, 6:38pm UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/1 "2013-08-30T18:38:05Z")

</div>

Apologies if this is obvious, but what would the syntax be to get special characters in a lineinfile directive? In particular I’m looking to do an escape character (\e).

-Jeremy

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [August 30, 2013, 10:27pm UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/2 "2013-08-30T22:27:16Z")

</div>

Serendipity…

I want to include a colon in lineinfile: and I get an error…

Bizarrely only on the second one…

- name: search files then dns for hosts  
lineinfile: dest=“/etc/nsswitch.conf” state=present regexp=“^hosts:”  
line=“hosts: files dns”

I’ve tried escaping that second one, using single quotes instead of double ones,… I’m stuck. Can anyone help?

Adam

---

<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:** [August 31, 2013, 1:32am UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/3 "2013-08-31T01:32:34Z")

</div>

You've got to quote the whole sucker because of the presence of the  
':' anywhere in the YAML value string. I'd use the '\>' YAML quoting  
mechanism to quote the whole value as follows:

- name: search files then dns for hosts  
&nbsp;&nbsp;lineinfile: \>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest="/etc/nsswitch.conf"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state=present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp="^hosts:"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line="hosts: files dns"

Hope this helps.

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:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [August 31, 2013, 1:42am UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/4 "2013-08-31T01:42:48Z")

</div>

I’ve found out how to make this work for me, but I suspect that this might be a bug…

My test file…

This works…

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [August 31, 2013, 1:52am UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/5 "2013-08-31T01:52:20Z")

</div>

Thank you,

That works too. I’m not sure why the : in the line parameter causes problems but not in the regexp parameter.

Here’s the actual output from a failed run…

ERROR: Syntax Error while loading YAML script, roles/common/tasks/error.yml  
Note: The error may actually appear before this position: line 6, column 45

- name: test  
lineinfile: regexp=“^hosts:” line=“hosts: files dns” state=present dest=  
“/etc/nsswitch.conf”  
^

I still think that this is a coding issue, particularly as the sudoers example given in the docs fails in the same way… Perhaps the sudoers example just needs to be rewritten.

---

<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:** [August 31, 2013, 2:15am UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/6 "2013-08-31T02:15:00Z")

</div>

Yep, it’s listed on the YAML gotchas page.

If a colon appears in a YAML line, you must quote the entire line.

If you find a doc example you believe needs to be corrected please let us know which/where by filing a ticket on [github.com/ansible/ansible](http://github.com/ansible/ansible), or sending a pull request to correct it.

Documentation on the modules page is code generated using the source in the “library/” directory of the checkout.

Other documentation examples live in “docsite/latest/\*.rst”

---

<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:** [August 31, 2013, 2:59am UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/7 "2013-08-31T02:59:01Z")

</div>

If you look carefully at your example, the 'line' parameter has a  
'colon followed by a space' and the 'host' parameter has a 'colon  
followed by a double quote'. The 'colon followed by a space' is  
special sequence in the YAML spec. This confuses people a lot,  
because its a relatively rare two character sequence they have to  
watch out for (the sudoers file is another common place to see this  
problem). I'm not sure this was made clear in the Gotchas section of  
the YAML syntax docs. The advice to always quote the whole value if it  
contains a ':' is a good, but it is a bit of harmless over-kill: it  
can cause confusion because its not always required.

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:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [August 31, 2013, 3:12am UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/8 "2013-08-31T03:12:32Z")

</div>

Thanks, I’ll fix that example and put in a pull request. The faulty example is pretty confusing particularly when combined with the working regexp.  
Thanks again,  
Adam

---

<div class="post-metadata">

**Author:** ![Andy1](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Andy1](https://forum.ansible.com/u/Andy1)\
**Post date:** [August 31, 2013, 4:04pm UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/9 "2013-08-31T16:04:43Z")

</div>

Personally I like this syntax way better:

```auto
   - name: line in file test
     lineinfile:
       dest: /tmp/nsswitch.conf # look a comment
       state: present
       regexp: '^hosts:'
       line: 'hosts: files dns' # another comment

```

Side effect:

- Gives nicer diffs

- Increases line count

- Allows for comments

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [September 2, 2013, 3:46am UTC](https://forum.ansible.com/t/special-characters-for-lineinfile/13395/10 "2013-09-02T03:46:00Z")

</div>

Indeed, that does look a lot neater. I’ve used that format in the pull request I submitted with an improved example for lineinfile.

Adam
