What's the safest way to do grep-like searches in files?

14 views
Skip to first unread message

Nico Sabbi

unread,
Feb 13, 2018, 11:45:57 AM2/13/18
to Ansible Project
HI,
I need to do a grep-like search in a file without using shell commands and in a check_mode compatible way.
I just need to check  the presence of a given string a in a file without doing overwrites of any kind.
I implemented something with lineinfile but I'm very annoyed by the need to  specify the line parameter to overwrite the file
because the r.e. may be wrong.

Is there any cleaner way to do what I need?

- name: grub
  become: true
  lineinfile:
    path: /etc/myfile
    backrefs: yes
    regexp: "^CMDLINE (.*)"
    line: 'CMDLINE \1'
    state: present
  register: myxx


Thanks.

Arthur Reyes

unread,
Feb 13, 2018, 11:48:53 AM2/13/18
to Ansible Project
You should run your playbook in check mode until you're certain that your regular expression is precise.

Matt Martz

unread,
Feb 13, 2018, 11:50:44 AM2/13/18
to ansible...@googlegroups.com
I would honestly use slurp to fetch the file and maybe a set_fact to set a var indicating it's existence.  That is the pattern I have always used.  Something like:

- slurp:
    path: /etc/myfile
  register: myfile

- set_fact:
    contains_cmdline: "{{ myfile.contents|b64decode|search("^CMDLINE") }}"

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0ca75bd8-67b9-43f3-87cb-0dbdeacd0088%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Nico Sabbi

unread,
Feb 13, 2018, 11:52:42 AM2/13/18
to Ansible Project

I like it. Thanks
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.

Nico Sabbi

unread,
Feb 13, 2018, 11:53:52 AM2/13/18
to Ansible Project

That may turn out to be too late depending on the complexity of the R.E.
It's not an error-proof approach.

Brian Coca

unread,
Feb 13, 2018, 11:59:38 AM2/13/18
to Ansible Project
The find module has a 'contains' option.




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

Arthur Reyes

unread,
Feb 13, 2018, 12:03:53 PM2/13/18
to Ansible Project
Not knowing the state of your environment or what you intend to do with the registered fact, I can only offer you this advice:

If a line in a file needs to exist and an action should occur if that file is changed, line in file is the most precise way to do this operation.

If you are using searches or other methods to find values in a file in order to notify actions, you haven't added additional precision to your plays. The risk of using regular expression is no different if you use line in file or if you use grep. There's some great regular expression builders on the web. Try using that to improve the precision of lineinfile, instead of querying system states using grep or other utilities.
Reply all
Reply to author
Forward
0 new messages