Re: [ansible-project] lininfile fails on line with colon in it

1,615 views
Skip to first unread message

Michael DeHaan

unread,
Mar 22, 2013, 5:00:41 PM3/22/13
to ansible...@googlegroups.com
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.



On Fri, Mar 22, 2013 at 1:09 PM, Peter Klausner
<peter.k...@gmail.com> wrote:
> I try to update /etc/sudoers, but fail with the YAML syntax. So I actually
> RTFM'ed and found the last sudoers example matches my case pretty much - and
> it fails as well. I'm running ansible from git. Here's a demo file with
> the sample and the failure:
>
> ________________________________________________________________________________
> mybox$ cat demo.yml
> - hosts: rh
> user: root
> tasks:
> - name: ok - sample w/o colon
> lineinfile: \"dest=/etc/sudoers state=present regexp='^%wheel' line
> ='%wheel ALL=(ALL) NOPASSWD ALL'\"
> - name: also fine, less quoting
> lineinfile: dest=/etc/sudoers state=present regexp='^%wheel' line
> ='%wheel ALL=(ALL) NOPASSWD ALL'
> - name: the colon kills it
> lineinfile: \"dest=/etc/sudoers state=present regexp='^%wheel' line
> ='%wheel ALL=(ALL) NOPASSWD: ALL'\"
> ________________________________________________________________________________
> mybox$ ansible-playbook -v demo.yml
> ERROR: Syntax Error while loading YAML script, demo.yml
> Note: The error may actually appear before this position: line 9, column 102
>
> - name: the colon kills it
> lineinfile: \"dest=/etc/sudoers state=present regexp='^%wheel' line
> ='%wheel ALL=(ALL) NOPASSWD: ALL'\"
>
> ^
> ________________________________________________________________________________
>
>
> Is there a way to quote the colon so that it is passed on properly?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Peter Klausner

unread,
Apr 12, 2013, 1:13:53 PM4/12/13
to ansible...@googlegroups.com
For the record, this syntax did it:

   lineinfile: "...    line='foo: bar'  ..."

Note that the documentation still samples the wrong syntax:

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

Michael DeHaan

unread,
Apr 12, 2013, 1:43:03 PM4/12/13
to ansible...@googlegroups.com
Right, hence "If there is a colon in a YAML line, it must be quoted."


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nick Doyle

unread,
Mar 18, 2014, 8:43:52 PM3/18/14
to ansible...@googlegroups.com
Hm I still get the prob ..

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

Michael DeHaan

unread,
Mar 18, 2014, 8:46:14 PM3/18/14
to ansible...@googlegroups.com
Please paste output and ansible --version.

Thanks!


Philippe Eveque

unread,
Mar 19, 2014, 8:52:32 AM3/19/14
to ansible...@googlegroups.com
Something like this should help
Assuming the following variable declaration

vars:
- a_column: "\x3A"


then later you can have:

line="ALL{{ a_column }} ALL"


--
Phil
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/28583879-97bb-4260-b772-66f05b853a0f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Mar 19, 2014, 10:00:08 AM3/19/14
to ansible...@googlegroups.com
Hi Phil,

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



Philippe Eveque

unread,
Mar 19, 2014, 10:49:33 AM3/19/14
to ansible...@googlegroups.com
 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, <path>/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.
 


Michael DeHaan

unread,
Mar 19, 2014, 10:54:23 AM3/19/14
to ansible...@googlegroups.com
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.






Philippe Eveque

unread,
Mar 19, 2014, 10:59:15 AM3/19/14
to ansible...@googlegroups.com
no, not working together.
 
just because of a similar issue I had I thought I could help
 
 


Adam Morris

unread,
Mar 19, 2014, 11:51:27 AM3/19/14
to ansible...@googlegroups.com


On Wednesday, March 19, 2014 7:49:33 AM UTC-7, Philippe Eveque wrote:

 
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
 
 
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

 

Philippe Eveque

unread,
Mar 19, 2014, 12:14:31 PM3/19/14
to ansible...@googlegroups.com
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"
--
Phil


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

C. Morgan Hamill

unread,
Mar 19, 2014, 12:20:53 PM3/19/14
to ansible-project
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.
--
Morgan Hamill

Philippe Eveque

unread,
Mar 19, 2014, 12:27:26 PM3/19/14
to ansible...@googlegroups.com
thanks Morgan,
 
the single quote is the solution here.
 
--
Phil


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages