python – grep -v analog for ansible

Question:

It is necessary to receive all ever entered users on Windows. The Public folder should not be in the list.

This construction doesn't work:

- name: TEST find 4 win
  block:
    - win_find:
        paths: C:\Users\
        file_type: directory
        excludes: Public
      register: win_find_out
    - debug:
        msg: "{{ win_find_out.files }}"

There is also the patterns parameter, but !Public , ?!Public or not (Public) don't work – the exhaust disappears altogether.

ansible 2.7.5

Answer:

You can use a regular expression like this:

^(?!.*Public)

See a demo of the regular expression online .

Details

  • ^ – start of line
  • (?!.*Public) – an exclusion forward preview block that will not return a match if there is one right after the current position
    • .* – 0 or more characters other than the newline character
    • Public – string Public

If the line can contain line breaks, add (?s) to the beginning:

(?s)^(?!.*Public)
Scroll to Top