lininfile fails on line with colon in it

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.

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

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

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

Hm I still get the prob …

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

Please paste output and ansible --version.

Thanks!

Something like this should help
Assuming the following variable declaration

  vars:
     - a_column: "\x3A"

then later you can have:

    line="ALL{{ a_column }} ALL"

Hi Phil,

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

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.

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.

no, not working together.

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

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

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”

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

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

Thought I'd add how I handle this:

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

I personally find it slightly more readable.

thanks Morgan,

the single quote is the solution here.