Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for files containing a particular text with Ansible?

I'd like to know if there's an Ansible module other than command that will give me a list of files (recursive search) containing a pattern?

On Unix I'd do

find . -type f -exec grep -l pattern {} \;

The result would be a list of files I'd iterate to change a value with another value

like image 690
Simon Avatar asked Dec 21 '25 08:12

Simon


1 Answers

You can use the find module to do this. The contains parameter accepts a regex to search for file content:

- name: Find files
  find:
    paths: /var/log
    contains: pattern
  register: found_files

The result of the find modules contains the attributes files, with a list of the matched files, and matched, with the number of matched files. You can store the result by using the register attribute on the find command (found_files above).

like image 74
nwinkler Avatar answered Dec 24 '25 08:12

nwinkler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!