2017-05-19 28 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