lineinfile - simply replace a string

1,046 views
Skip to first unread message

AnsiNoob

unread,
May 16, 2017, 1:17:29 AM5/16/17
to Ansible Project
Hi

Two things that I miss in Ansible when compared to shell scripting is easy 'grep' and 'sed'. That was easy to do in shell!

My case is very simple. I have a file with lot of lines (sample below) and the line mentioned below comment has to be checked for its presence. 

______________________________
someline
my line
    {
        another line
        # Below line's presence needs to be checked
        another=myother
    }
another line
______________________________

Now, below line does not work as there are lot of spaces before "another=myother".

lineinfile:
  dest: /my/path
  line: "another=myother"
  state: present

I tried regexp: "$another=myother" and few other combinations but nothing works. lineinfile is never able to read for the entry and everytime says "line added" when playbook is run in --check mode.

Dick Visser

unread,
May 16, 2017, 8:30:39 AM5/16/17
to ansible...@googlegroups.com
You seem to be missing the leading white space 


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d3257091-3d23-47ff-b0ad-95a662a8fb12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Dick Visser
Sr. System & Network Engineer
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs

Listing

unread,
May 16, 2017, 8:48:38 AM5/16/17
to Ansible Project
FWIW: you can also use grep or sed in command and then register the result. If you then output result.stdout you will get the result of the grep/sed.

AnsiNoob

unread,
May 16, 2017, 9:59:52 AM5/16/17
to Ansible Project
@Dick

Thanks for your reply. I don't want to use leading white space in the string mentioned in line. That is because there are multiple files and some may have leading white space before the string and some may not.

Ansible should be able to find a snippet which is part of one line in a multi-line file.

AnsiNoob

unread,
May 16, 2017, 10:04:27 AM5/16/17
to Ansible Project
@Listing - I will eventually use grep and sed only buy I wish Ansible had a easy to use module for this. I have been using Ansible for sometime now and I'm not to close this gap.

Dick Visser

unread,
May 16, 2017, 5:38:06 PM5/16/17
to ansible...@googlegroups.com
Then you should use a regex that does just that?

lineinfile:
dest: /tmp/test.txt
backrefs: yes
regexp: '^(\s+)another=myother$'
line: '\1another=myother'
state: present

Dick Visser

unread,
May 17, 2017, 3:20:47 AM5/17/17
to ansible...@googlegroups.com
On 16 May 2017 at 23:37, Dick Visser <dick....@geant.org> wrote:

> Then you should use a regex that does just that?
>
> lineinfile:
> dest: /tmp/test.txt
> backrefs: yes
> regexp: '^(\s+)another=myother$'
> line: '\1another=myother'
> state: present

AnsiNoob, what are you trying to achieve in the first place?
More specifically, what do you mean by: " # Below line's presence
needs to be checked" ?

lineinfile is meant to make sure (not "checking") a specific line is
in a file, optionally aware of the lines before and after.
But looking at your sample file, it looks like a more complex data structure.
Adding just a line at the bottom doesn't seem to make sense in this case?

In summary, can you be more clear and specific, possibly with more examples?

AnsiNoob

unread,
May 17, 2017, 12:21:27 PM5/17/17
to Ansible Project
Thanks for your time Dick.

I've give some thought to this and I agree that lineinfile is meant to replace/add a line if not present and not for 'just checking' an entire line, a word, or few words.

You are right that my file is complex data structure. We have 100 such files and our job is to check for existence of specific string (another=myother is just an example) in all files. I thought I could use lineinfile module in dry run mode (check_mode: yes) so that lineinfile would just check for presence and not replace anything. 

I do have a workaround now but would be interested to know how people use Ansible to perform simple greps to check for presence of specific settings/entries in different configuration files in order to generate reports.

What I'm doing now:

regex: '(another=myother)'
line: 'any sample string'

When above is run in check mode then lineinfile outputs saying it did find the string in regexp and replaced it with string in line section but it actually does not as it was executed in check mode.

The 'changed' output in yellow is what I use to report that the string exists. 

Kai Stian Olstad

unread,
May 17, 2017, 1:56:23 PM5/17/17
to ansible...@googlegroups.com
On 17. mai 2017 18:21, AnsiNoob wrote:
> I do have a workaround now but would be interested to know how people use
> Ansible to perform simple greps to check for presence of specific
> settings/entries in different configuration files in order to generate
> reports.

This is an assertion from my side, most people don't.
Ansible is a configuration management tool and not a reporting tool.
That means you tell it how you would like your server to be configured
and Ansible do the job.

--
Kai Stian Olstad

Brian Coca

unread,
May 17, 2017, 4:02:25 PM5/17/17
to Ansible Project
but if you MUST misuse ansible, the `find` module has a `contains`
option that functions mostly like grep


----------
Brian Coca

John Jolet

unread,
May 18, 2017, 1:07:40 PM5/18/17
to Ansible Project
did you try  ( ?) or some such?
Reply all
Reply to author
Forward
0 new messages