0

I have multiple config.properties files in nested folder which contains my common configuration.

/myproject/env/dev/config.properties:

LOG_PATH=/app/log
LOG_LEVEL=INFO
CLUSTER=ABC
NAMESPACE=dev

/myproject/env/test/config.properties:

CLUSTER=ABC
NAMESPACE=test

I want to list the files for a set of keys like (LOG_PATH) not available in config.properties.

I am able to do positive lookup (contains: LOG_PATH) in contains parameter but when I do negative lookup it shows me both file path as matched but I expect it should show just one path /test/config.properties.

Is something wrong in my regex? I verified it using https://pythex.org and it works there. I also used DOTALL regex ^((?!LOG_PATH)[\\s][\\S])*$

findFiles-Playbook.yml:

- hosts: localhost
  gather_facts: false

  tasks:
   - name: List the file path which doesn't contains the content
     find:
        paths: /mrproject/env/
        recurse: yes
        follow: True
        patterns: config.properties
        use_regex: True
        contains: LOG_PATH
        #contains: ^((?!LOG_PATH).)*$ this Negative lookup doesn't works
     register: matched_files

   - name: print matches
     debug:
       var: matched_files
techraf
  • 64,883
  • 27
  • 193
  • 198
jazz
  • 19
  • 4

1 Answers1

1

Everything works as expected:

  • with contains: LOG_PATH you search for files which contain LOG_PATH (only /myproject/env/dev/config.properties contains that line)

  • with ^((?!LOG_PATH).)*$ you search for files which contain lines other than starting with LOG_PATH (both example files contain such lines)

    • it will exclude file(s) with LOG_PATH line(s) only; no other lines

There is no way to specify "does not contain" in a single Ansible find-module call.

You can either:

  • call the module twice: once to find all files matching pattern, the second time with contains: LOG_PATH and process the results (map(attribute='path') then difference filter)

  • execute an external command returning the files (see this question for inspiration); there is no change caused by this task anyway, so you can safely add changed: false for the task to report ok.

techraf
  • 64,883
  • 27
  • 193
  • 198