I find some files with the find ansible module
- find:
paths: "/"
patterns: ["*.service"]
file_type: file
hidden: True
recurse: yes
register: find_results
when: ansible_service_mgr == "systemd"
Now I want to check modify the Permissions on some of the files:
- file:
path: "{{ item.path }}"
owner: root
group: root
mode: 0644
with_items:
- "{{ find_results.files }}"
when:
- item.path | match("/sys/devices/power/events/*")
- ansible_service_mgr == "systemd"
The Problem is that I don't want a match. I want everything that don't match that path?
How can I negate the match filter? (!, not, etc.)?
I'd recommend to rewrite your task as follows:
It uses
rejectattr
to reject (eliminate) all items fromfind_results.files
that match/sys/devices/power/events/.*
regexp. A kind of match negation you wanted.It makes a loop only with required items. Your task will make a loop with all found files and generate "skipped" message for each
when
statement miss.Also it feeds empty list
[]
towith_items
when service managed is notsystemd
. Otherwise your task may either generate multiple skipped items for eachfind_results.files
item, or fail altogether because previous task was skipped and hasfind_results.files
undefined.P.S. If you have many tasks that depend on
systemd
, you may want to separate them into different yaml fils and conditionally include it only onsystemd
machines – this will make code much cleaner without those multiplewhen: ansible_service_mgr == "systemd"
statements.