# Negate 'contains' in find TASK

**URL:** <https://forum.ansible.com/t/negate-contains-in-find-task/42530>\
**Category:** Get Help\
**Created:** [May 8, 2025, 5:16am UTC](https://forum.ansible.com/t/negate-contains-in-find-task/42530 "2025-05-08T05:16:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [May 8, 2025, 5:16am UTC](https://forum.ansible.com/t/negate-contains-in-find-task/42530/1 "2025-05-08T05:16:49Z")

</div>

this `find` operation works.

```auto
- 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?

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [May 8, 2025, 9:14am UTC](https://forum.ansible.com/t/negate-contains-in-find-task/42530/2 "2025-05-08T09:14:17Z")

</div>

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”_

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [May 8, 2025, 2:16pm UTC](https://forum.ansible.com/t/negate-contains-in-find-task/42530/3 "2025-05-08T14:16:55Z")

</div>

Looking over the [find](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find_module.html) module docs, does the `exclude` argument do what you’re looking for? Something like this?

```auto
    - 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

```

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [May 8, 2025, 9:27pm UTC](https://forum.ansible.com/t/negate-contains-in-find-task/42530/4 "2025-05-08T21:27:28Z")

</div>

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