Negate 'contains' in find TASK

this find operation works.

- name: find stuff
  hosts: "{{ targets | default('localhost') }}"
  vars:
    - search_path: /opt/db/postgres/tools/test_4_ansible
    - search_term: "backup_archivelogs*sh"
    - content: NODENAME

  tasks:
    - name: find script in {{ search_path }}
      find:
        path: "{{ search_path }}"
        patterns: "{{ search_term }}"    # "backup_archivelogs.*.sh"
        contains: "{{ content }}"  # NODENAME
      register: findings
      become: true
      become_user: postgres

    - name: show files found
      debug:
        var: item
      loop: "{{ findings.files | map(attribute='path') | list }}"

now I am wondering how to find files matching patterns but do NOT contain, NODENAME (the string I was looking for in the contains clause/argument/option) ?

I played a bit with ! but could not make it work.

any suggestions?

I picked up the suggestion ^((?!NODENAME).)*$ (which, I guess requires use_regex: true), but that neither did the job of excluding files that contain NODENAME.

For now I am afraid it takes another Semester of regex, python, … to get this going.


conclusion for now (for me): “finding files based on a search path and filename is ok, but going for contains or regex … is entering a world of pain”

1 Like

Looking over the find module docs, does the exclude argument do what you’re looking for? Something like this?

    - name: find script in {{ search_path }}
      find:
        path: "{{ search_path }}"
        patterns: "{{ search_term }}"    # "backup_archivelogs.*.sh"
        excludes: "{{ content }}"  # NODENAME
      register: findings
      become: true
      become_user: postgres

excludes argument is used for excluding file names, not file content.