Simon
Simon

Reputation: 333

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

Upvotes: 3

Views: 6489

Answers (1)

nwinkler
nwinkler

Reputation: 54517

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

Upvotes: 3

Related Questions