New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colon-space in lineinfile argument produces syntax error #1341

Closed
brontitall opened this Issue Oct 16, 2012 · 18 comments

Comments

Projects
None yet
@brontitall
Contributor

brontitall commented Oct 16, 2012

Given this play:

    - hosts: ${host}
      tasks:
      - name: Create test alias
        action: lineinfile dest=/etc/mail/aliases regexp="^test:" line='test: root' state=present

I get this error:

ERROR: Syntax Error while loading YAML script, space_bug_case.yml
Note: The error may actually appear before this position: line 5, column 73

  - name: Create test alias
    action: lineinfile dest=/etc/mail/aliases regexp="^test:" line='test: root' state=present
                                                                        ^

It seems that it's specifically the ": " sequence that causes the problem, since (thanks to cocoy on IRC) the following all work:

action: lineinfile dest=/etc/mail/aliases regexp="^test:" line='test:\t root' state=present
action: lineinfile dest=/etc/mail/aliases regexp="^test:" line='test:root' state=present
action: lineinfile dest=/etc/mail/aliases regexp="^test:" line='test root' state=present

(although the last gives a different error, since it no longer matches the regexp.)

I'm running devel on MacOS with Python and libyaml from homebrew:

$ brew list --versions python libyaml
python 2.7.3
libyaml 0.1.4
$ ansible --version
ansible 0.8 (devel f160502b7b) last updated 2012/10/16 16:15:01 (GMT +1100)
@mpdehaan

This comment has been minimized.

Contributor

mpdehaan commented Oct 16, 2012

You need to outer quote stuff like this when you have an extra colon

action: "lineinfile blarg blarg blarg blarg "

@mpdehaan mpdehaan closed this Oct 16, 2012

@t2d

This comment has been minimized.

t2d commented Jan 8, 2013

with your trick i recieved fatal: [host] => module lineinfile: not found in /usr/share/ansible/:library:/home/user/playbooks/library
I had to do lineinfile: 'args args="asdas"'

@ghost

This comment has been minimized.

ghost commented May 22, 2013

works for me:

- name: updating of status script
  lineinfile: "dest=/opt/script.sh regexp='DIR=' line='        DIR=/var/run'"
@mpdehaan

This comment has been minimized.

Contributor

mpdehaan commented Jul 3, 2013

Sure if you start a line with a quote you end the line with a quote, and you're good to go!

@drewp

This comment has been minimized.

drewp commented Sep 30, 2013

Still seems broken in 1.3.2. This fails:

- lineinfile:
    regexp="^foo:"
    line="foo: \"| /usr/bin/curl http://localhost:9108/newmail -H 'Content-type: message/rfc822' --data-binary @-\""

-> Syntax Error while loading YAML script on the first colon in 'line'

This works, and I only have to avoid the colon-space combos:

- set_fact: colon=":"
- lineinfile:
    regexp="^foo:"
    line="foo{{colon}} \"| /usr/bin/curl http://localhost:9108/newmail -H 'Content-type{{colon}} message/rfc822' --data-binary @-\""
@drewp

This comment has been minimized.

drewp commented Sep 30, 2013

Don't need a new fact variable; {{':'}} is enough to avoid the parser problem

@DaveQB

This comment has been minimized.

DaveQB commented Mar 7, 2014

Ver 1.4.1

@drewp Brilliant solution. I have been going crazy with single and double quotes, nothing will parse. Interesting, I don't have any issues is using the same line on the command line in a -a " " Only in the playbook does it have this issue. Even an YAML parser said I was doing it right.

Thanks again.
PS Hash symbol (#) looks harder to use.

@brucellino

This comment has been minimized.

brucellino commented Oct 13, 2014

Just wasted 3 hours trying to figure this out, thanks for the tip !

@victorcoder

This comment has been minimized.

Contributor

victorcoder commented Nov 27, 2014

Annoying stuff

@udondan

This comment has been minimized.

udondan commented Jan 23, 2015

@drewp Awesome nice trick, thanks! {{':'}}

@rpocklin

This comment has been minimized.

rpocklin commented Feb 23, 2015

Just found this after tearing my hair out. @drewp thanks 🍺

@practicalweb

This comment has been minimized.

practicalweb commented Feb 27, 2015

ansible 1.9 (devel 2b03740) last updated 2015/02/17 22:11:14 (GMT +100)

same bug here

thanks @drewp

This works - but I couldn't find any other method of escaping that did

# https://github.com/brutasse/graphite-web/commit/28a426811ac71625fe30f532b9a6221acc55c7bc
- name: fix deprecated CACHE config  
  lineinfile: dest=/usr/lib/python2.6/site-packages/graphite/app_settings.py regexp='^CACHE_BACKEND.*'  line="CACHES = { 'default'{{':'}} {   'BACKEND'{{':'}} 'django.core.cache.backends.dummy.DummyCache', }, }"
@practicalweb

This comment has been minimized.

practicalweb commented Feb 27, 2015

I take it back - full line quoting works fine

  lineinfile: "dest=/usr/lib/python2.6/site-packages/graphite/app_settings.py regexp='^CACHE_BACKEND.*'  line=\"CACHES = { 'default': {   'BACKEND': 'django.core.cache.backends.dummy.DummyCache', }, }\""
@stephanadler

This comment has been minimized.

stephanadler commented Jun 25, 2015

Wow. I am on 1.9.2 an also just ran against this problem. I doubted myself for hours before finding this.

@Hirandil

This comment has been minimized.

Hirandil commented Aug 20, 2015

Thanks a lot @drewp, your saved me hours of work !

@msmagnanijr

This comment has been minimized.

msmagnanijr commented Oct 19, 2015

Thanks!

@J-Zet

This comment has been minimized.

J-Zet commented Mar 8, 2016

...just to name a (in my opinion) more readable solution, through using the '>'-mechanism.
see here

  - name: "setting cluster name to: {{ es_cluster_name }}"
    lineinfile: >
      dest=/etc/elasticsearch/elasticsearch.yml
      regexp="cluster.name:"
      line="cluster.name: {{ es_cluster_name }}"
    become: yes
@wouteroostervld

This comment has been minimized.

Contributor

wouteroostervld commented Apr 29, 2016

Why not just build a real parser?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment