2017-05-19 64 views
0

我找到了找到ansible模块Ansible - 否定过滤

- 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" 

的问题是,我不不想要比赛。我想要所有不符合该路径的东西?

我怎么能否定匹配过滤器? (!,不等)?

回答

0

我建议重写你的任务如下:

- 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 }}" 
  1. 它使用rejectattr拒绝(消除)从find_results.files匹配/sys/devices/power/events/.*正则表达式的所有项目。一种你想要的比赛否定。

  2. 它使一个仅环与所需物品。你的任务将与所有找到的文件一个循环,并产生“跳过”每个when声明小姐的消息。

  3. 此外,当服务管理不是systemd时,它也会提供空列表[]with_items。否则,你的任务既可以生成多个跳过项目每个find_results.files项目,或者是因为以前的任务是跳过,具有find_results.files不确定的完全失败。

P.S.如果您有依赖于systemd许多任务,你可能需要将它们分成不同的YAML费尔和条件包括它只能在systemd机器 - 这将使得代码更清洁的没有这些多when: ansible_service_mgr == "systemd"语句。

0

可以尝试

when: ("/sys/devices/power/events/*" not in item.path "/sys/devices/power/events/") and 
     (ansible_service_mgr == "systemd") 

但第二个条件是无用的,因为你得到find_results

1

THX你的帮助,你也可以这样做的时候就已经设定的条件:

not (item.path | match("/sys/devices/power/events/*")) 
+0

所以你需要通过投票的答案,如果有帮助的感谢,并接受的答案,如果它是正确的。 – BMW