Replace contents of same named file located in multiple non-unique directories.

I’d like to replace some text in a file using the Replace: command. The file is located in many different sub-directories. I know how to replace text when I know the directory, but the file I want to replace could be in multiple locations unique to each server. I know each folder where the file exists ends with _. How can I make this happen?

ex: file name = file.conf
folder structure = /opt/<different_folder_name>_[0-9]/file.conf

I think the cleanest way to do it would be to save the folder name as a host var. If you can’t do that, here’s a really gross way!

Use find. Something like:

find /opt -name ‘file.conf’

If that file name is unique, you should only get one result.

  • name: Find file.conf Path
    command: find /opt -name ‘file./conf’
    register: file_conf_path

  • name: Do something to file.conf
    file:
    dest: “{{ file_conf_path.stdout }}”

This is not an ideal solution, and not technically idempotent, but it would solve your problem.

in 2.0 you have the find module