# How do I properly escape a colon in a lineinfile string

**URL:** <https://forum.ansible.com/t/how-do-i-properly-escape-a-colon-in-a-lineinfile-string/18863>\
**Category:** Ansible Project\
**Created:** [July 27, 2015, 2:40pm UTC](https://forum.ansible.com/t/how-do-i-properly-escape-a-colon-in-a-lineinfile-string/18863 "2015-07-27T14:40:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Thacker](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dave\_Thacker](https://forum.ansible.com/u/Dave_Thacker)\
**Post date:** [July 27, 2015, 2:40pm UTC](https://forum.ansible.com/t/how-do-i-properly-escape-a-colon-in-a-lineinfile-string/18863/1 "2015-07-27T14:40:11Z")

</div>

I want to add an alias to the end of my /etc/aliases file.

I’ve tried

action: lineinfile dest=“/etc/aliases” backup=yes state=present insertafter=EOF line=“root: [unixadmin@example.com](mailto:unixadmin@example.com)”

action: lineinfile dest=“/etc/aliases” backup=yes state=present insertafter=EOF line=‘root: [unixadmin@example.com](mailto:unixadmin@example.com)’

I’m tripping on the colon after root.

Thanks in advance

DT

---

<div class="post-metadata">

**Author:** ![Dave\_Thacker](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dave\_Thacker](https://forum.ansible.com/u/Dave_Thacker)\
**Post date:** [July 27, 2015, 6:38pm UTC](https://forum.ansible.com/t/how-do-i-properly-escape-a-colon-in-a-lineinfile-string/18863/2 "2015-07-27T18:38:25Z")

</div>

Another attempt and full error message

- name: add email alias to /etc/alias  
action: lineinfile dest=“/etc/aliases” backup=yes state=present insertafter=EOF line=“root’:’ [unixadmin@example.com](mailto:unixadmin@example.com)”

results in

# Person who should get root’s mail

#root: marc  
root’:’ [unixadmin@example.com](mailto:unixadmin@example.com)

which is nicely quoted but not correct.

DT

---

<div class="post-metadata">

**Author:** ![Dave\_Thacker](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Dave\_Thacker](https://forum.ansible.com/u/Dave_Thacker)\
**Post date:** [July 27, 2015, 6:41pm UTC](https://forum.ansible.com/t/how-do-i-properly-escape-a-colon-in-a-lineinfile-string/18863/3 "2015-07-27T18:41:28Z")

</div>

Sorry,  
Error message is as follows.

ERROR: Syntax Error while loading YAML script, prdscmappl01  
Note: The error may actually appear before this position: line 28, column 95

- name: add email alias to /etc/alias  
action: lineinfile dest=“/etc/aliases” backup=yes state=present insertafter=EOF line=“root: [unixadmin@example.com](mailto:unixadmin@example.com)”  
^  
This one looks easy to fix. There seems to be an extra unquoted colon in the line  
and this is confusing the parser. It was only expecting to find one free  
colon. The solution is just add some quotes around the colon, or quote the  
entire line after the first colon.

For instance, if the original line was:

copy: src=file.txt dest=/path/filename:with\_colon.txt

It can be written as:

copy: src=file.txt dest=‘/path/filename:with\_colon.txt’

Or:

copy: ‘src=file.txt dest=/path/filename:with\_colon.txt’

---

<div class="post-metadata">

**Author:** ![Larry\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/d26b3c/32.png) [@Larry\_Smith1](https://forum.ansible.com/u/Larry_Smith1)\
**Post date:** [July 27, 2015, 8:28pm UTC](https://forum.ansible.com/t/how-do-i-properly-escape-a-colon-in-a-lineinfile-string/18863/4 "2015-07-27T20:28:50Z")

</div>

Colons need to be added like so…  
insertafter=EOF line=“root: [unix...@example.com](mailto:unix...@example.com)”

would be as below  
insertafter=EOF line=“root{{ ‘:’ }} [unix...@example.com](mailto:unix...@example.com)”
