Apologies if this is obvious, but what would the syntax be to get special characters in a lineinfile directive? In particular I’m looking to do an escape character (\e).
-Jeremy
Apologies if this is obvious, but what would the syntax be to get special characters in a lineinfile directive? In particular I’m looking to do an escape character (\e).
-Jeremy
Serendipity…
I want to include a colon in lineinfile: and I get an error…
Bizarrely only on the second one…
I’ve tried escaping that second one, using single quotes instead of double ones,… I’m stuck. Can anyone help?
Adam
You've got to quote the whole sucker because of the presence of the
':' anywhere in the YAML value string. I'd use the '>' YAML quoting
mechanism to quote the whole value as follows:
- name: search files then dns for hosts
lineinfile: >
dest="/etc/nsswitch.conf"
state=present
regexp="^hosts:"
line="hosts: files dns"
Hope this helps.
K
Kahlil (Kal) Hodgson GPG: C9A02289
Head of Technology (m) +61 (0) 4 2573 0382
DealMax Pty Ltd (w) +61 (0) 3 9008 5281
Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia
"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925
I’ve found out how to make this work for me, but I suspect that this might be a bug…
My test file…
This works…
Thank you,
That works too. I’m not sure why the : in the line parameter causes problems but not in the regexp parameter.
Here’s the actual output from a failed run…
ERROR: Syntax Error while loading YAML script, roles/common/tasks/error.yml
Note: The error may actually appear before this position: line 6, column 45
I still think that this is a coding issue, particularly as the sudoers example given in the docs fails in the same way… Perhaps the sudoers example just needs to be rewritten.
Yep, it’s listed on the YAML gotchas page.
If a colon appears in a YAML line, you must quote the entire line.
If you find a doc example you believe needs to be corrected please let us know which/where by filing a ticket on github.com/ansible/ansible, or sending a pull request to correct it.
Documentation on the modules page is code generated using the source in the “library/” directory of the checkout.
Other documentation examples live in “docsite/latest/*.rst”
If you look carefully at your example, the 'line' parameter has a
'colon followed by a space' and the 'host' parameter has a 'colon
followed by a double quote'. The 'colon followed by a space' is
special sequence in the YAML spec. This confuses people a lot,
because its a relatively rare two character sequence they have to
watch out for (the sudoers file is another common place to see this
problem). I'm not sure this was made clear in the Gotchas section of
the YAML syntax docs. The advice to always quote the whole value if it
contains a ':' is a good, but it is a bit of harmless over-kill: it
can cause confusion because its not always required.
K
Kahlil (Kal) Hodgson GPG: C9A02289
Head of Technology (m) +61 (0) 4 2573 0382
DealMax Pty Ltd (w) +61 (0) 3 9008 5281
Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia
"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925
Thanks, I’ll fix that example and put in a pull request. The faulty example is pretty confusing particularly when combined with the working regexp.
Thanks again,
Adam
Personally I like this syntax way better:
- name: line in file test
lineinfile:
dest: /tmp/nsswitch.conf # look a comment
state: present
regexp: '^hosts:'
line: 'hosts: files dns' # another comment
Side effect:
Indeed, that does look a lot neater. I’ve used that format in the pull request I submitted with an improved example for lineinfile.
Adam