# lininfile fails on line with colon in it

**URL:** <https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [March 22, 2013, 9:00pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557 "2013-03-22T21:00:41Z")\
**Posts on this page:** 14\
**Page:** 1

<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:** [March 22, 2013, 9:00pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/1 "2013-03-22T21:00:41Z")

</div>

If there is a colon in a YAML line, it must be quoted.

Because you have escaped your starting quote with \", that doesn't  
count as a quote, and does not serve to quote the colon.

It appears the answer is to not escape your leading \", and instead  
just say lineinfile:" ... and to keep it simple, use single quotes  
inside the double, and you own't have to escape anything else.

---

<div class="post-metadata">

**Author:** ![Peter\_Klausner](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@Peter\_Klausner](https://forum.ansible.com/u/Peter_Klausner)\
**Post date:** [April 12, 2013, 5:13pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/2 "2013-04-12T17:13:53Z")

</div>

For the record, this syntax did it:

lineinfile: “… line=‘foo: bar’ …”

Note that the documentation still samples the wrong syntax:  
[http://ansible.cc/docs/modules.html#lineinfile](http://ansible.cc/docs/modules.html#lineinfile)

And note that recent sudo supports the /etc/sudoers.d directory,  
which obsoletes mucking with lineinfile in this case.

---

<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:** [April 12, 2013, 5:43pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/3 "2013-04-12T17:43:03Z")

</div>

Right, hence “If there is a colon in a YAML line, it must be quoted.”

---

<div class="post-metadata">

**Author:** ![Nick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/n/9fc348/32.png) [@Nick\_Doyle](https://forum.ansible.com/u/Nick_Doyle)\
**Post date:** [March 19, 2014, 12:43am UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/4 "2014-03-19T00:43:52Z")

</div>

Hm I still get the prob …

- name: allow gateway  
sudo: yes  
lineinfile:  
line=“ALL: 127.0.0.1”  
dest=/etc/hosts.allow

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 19, 2014, 12:46am UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/5 "2014-03-19T00:46:14Z")

</div>

Please paste output and ansible --version.

Thanks!

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [March 19, 2014, 12:52pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/6 "2014-03-19T12:52:32Z")

</div>

Something like this should help  
Assuming the following variable declaration

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- a\_column: "\x3A"

then later you can have:

&nbsp;&nbsp;&nbsp;&nbsp;line="ALL{{ a\_column }} ALL"

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 19, 2014, 2:00pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/7 "2014-03-19T14:00:08Z")

</div>

Hi Phil,

There are no such sequences in the above post. Still waiting on version info and output.

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [March 19, 2014, 2:49pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/8 "2014-03-19T14:49:33Z")

</div>

Hi Michael,

sure the version info and output are much needed !

I had a very similar uses case to what I understood from Nick.  
I needed to insert/insure a line such as below being declared in /etc/hosts.deny file:  
ALL: ALL

Using the lineinfile module, the only way I found for a line to contain a column character, is to use something like I described.

That is having a variable being defined like:

a\_column: “\x3A”

and in the playbook a task like:

lineinfile:  
dest=/etc/hosts.deny  
state=present  
regexp=‘^ALL:\s+ALL’  
line=“ALL{{ a\_column }} ALL”  
insertafter=EOF

Having the following task will not work:

lineinfile:  
dest=/etc/hosts.deny  
state=present  
regexp=‘^ALL:\s+ALL’  
line=“ALL: ALL”

and lead to the following output:

ERROR: Syntax Error while loading YAML script, /host\_access\_restricted/tasks/main.yml  
Note: The error may actually appear before this position: line 33, column 14  
regexp=‘^ALL:\s+ALL’  
line=“ALL: ALL”

^  
ansible-playbook version in my case is 1.5.3

Not sure if the above error really is an error or should be considered as a bug.  
Note: this is old code on my side and IIRC I applied this workaround since ansible \>= 1.2

HTH

Phil.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 19, 2014, 2:54pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/9 "2014-03-19T14:54:23Z")

</div>

I was hoping for a reply from Nick re the above but it may be you work together?

In the above it appeared he had a “space” but it appears in your example you are talking about a non-space character.

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [March 19, 2014, 2:59pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/10 "2014-03-19T14:59:15Z")

</div>

no, not working together.

just because of a similar issue I had I thought I could help

---

<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:** [March 19, 2014, 3:51pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/11 "2014-03-19T15:51:27Z")

</div>

Actually it’s easier than that…

I have a module where I need a colon in a lineinfile entry…

- name: search files then dns for hosts  
lineinfile: ‘backup=yes dest={{network\_switch}} state=present  
regexp=“^hosts:” line=“hosts: files dns”’

Not just one colon in that line but two…

I found that the double quoting was necessary, but there you go.

Adam

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [March 19, 2014, 4:14pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/12 "2014-03-19T16:14:31Z")

</div>

Thanks for this

Based on your post , and in my case where the statement is spread over multiples lines  
I just found the following to work (tend to always forgot the yaml syntax for multi-line )  
which to me is even simpler (this is just me) as no even need to double quote

lineinfile: \>  
dest=/etc/hosts.deny  
state=present  
regexp=“^ALL:\s+ALL”  
line=“ALL: ALL”

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [March 19, 2014, 4:20pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/13 "2014-03-19T16:20:53Z")

</div>

Excerpts from Philippe Eveque's message of 2014-03-19 12:14:31 -0400:

> &nbsp;&nbsp;lineinfile: \>  
> &nbsp;&nbsp;&nbsp;&nbsp;dest=/etc/hosts.deny  
> &nbsp;&nbsp;&nbsp;&nbsp;state=present  
> &nbsp;&nbsp;&nbsp;&nbsp;regexp="^ALL:\s+ALL"  
> &nbsp;&nbsp;&nbsp;&nbsp;line="ALL: ALL"

Thought I'd add how I handle this:

&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/host.deny  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^ALL:\s+ALL'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: 'ALL: ALL'

I personally find it slightly more readable.

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [March 19, 2014, 4:27pm UTC](https://forum.ansible.com/t/lininfile-fails-on-line-with-colon-in-it/12557/14 "2014-03-19T16:27:26Z")

</div>

thanks Morgan,

the single quote is the solution here.
