Filter 负负滤波器
我用find ansible模块找到了一些文件Filter 负负滤波器,filter,ansible,negate,Filter,Ansible,Negate,我用find ansible模块找到了一些文件 - find: paths: "/" patterns: ["*.service"] file_type: file hidden: True recurse: yes register: find_results when: ansible_service_mgr == "systemd" 现在我想检查修改某些文件的权限: - file: path: "{{ item.path }}"
- find:
paths: "/"
patterns: ["*.service"]
file_type: file
hidden: True
recurse: yes
register: find_results
when: ansible_service_mgr == "systemd"
现在我想检查修改某些文件的权限:
- 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"
问题是我不想要一对。我想要所有与那条路不匹配的东西
如何取消匹配筛选器?(!、不是等)?我建议您将任务改写如下:
- file:
path: "{{ item.path }}"
owner: root
group: root
mode: 0644
with_items: "{{ [] if ansible_service_mgr != 'systemd' else find_results.files | rejectattr('path','match','/sys/devices/power/events/.*') | list }}"
rejectattr
拒绝(消除)所有符合/sys/devices/power/events/*
regexp的find_results.files
中的项目。一种你想要的匹配否定生成“跳过”消息
systemd
时,它还将空列表[]
与\u项一起馈送到。否则,您的任务可能会为每个find_results.files
项生成多个跳过的项,或者由于上一个任务已跳过且具有find_results.files
未定义,因此全部失败
注意:如果您有许多任务依赖于
systemd
,您可能希望将它们分为不同的yaml文件,并有条件地仅在systemd
机器上包含这些文件–这将使代码在没有多个的情况下更加干净:ansible\u service\u mgr==“systemd”
语句。我建议您将任务重写如下:
- file:
path: "{{ item.path }}"
owner: root
group: root
mode: 0644
with_items: "{{ [] if ansible_service_mgr != 'systemd' else find_results.files | rejectattr('path','match','/sys/devices/power/events/.*') | list }}"
rejectattr
拒绝(消除)所有符合/sys/devices/power/events/*
regexp的find_results.files
中的项目。一种你想要的匹配否定生成“跳过”消息
systemd
时,它还将空列表[]
与\u项一起馈送到。否则,您的任务可能会为每个find_results.files
项生成多个跳过的项,或者由于上一个任务已跳过且具有find_results.files
未定义,因此全部失败
systemd
,您可能希望将它们分为不同的yaml文件,并有条件地仅在systemd
机器上包含它们–这将使代码更加干净,而无需使用多个语句:ansible\u service\u mgr==“systemd”
语句。您可以尝试吗
when: ("/sys/devices/power/events/*" not in item.path "/sys/devices/power/events/") and
(ansible_service_mgr == "systemd")
但是第二个条件是无用的,因为您在获取find\u结果时已经设置了该条件
when: ("/sys/devices/power/events/*" not in item.path "/sys/devices/power/events/") and
(ansible_service_mgr == "systemd")
但是第二个条件是无用的,因为在获取find\u结果时,您已经设置了该条件。对于您的帮助,您还可以执行以下操作:
not (item.path | match("/sys/devices/power/events/*"))
Thx为您提供帮助,您还可以执行以下操作:
not (item.path | match("/sys/devices/power/events/*"))
因此,如果答案有帮助,你需要投票表示感谢,如果答案正确,你需要投票表示感谢。如果答案有帮助,你需要投票表示感谢,如果答案正确,你需要投票表示接受。