lineinfile - simply replace a string

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.

You seem to be missing the leading white space

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.

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

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

did you try ( ?) or some such?

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?

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.

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.

but if you MUST misuse ansible, the `find` module has a `contains`
option that functions mostly like grep