[英]Ansible Task: with_dict statement got triggered, even if the when-clause should prevent this
我正在嘗試自動創建smart_ [diskdevice]鏈接到
/ usr / share / munin / plugins / smart_
在通過ansible安裝munin節點期間。
這里的代碼部分起作用,除非目標機器上沒有要鏈接的磁盤設備。 然后我的致命失敗
{"msg": "with_dict expects a dict"}
我已經審查了Ansible文檔,並嘗試在網上搜索問題。 據我了解,如果“ when”語句失敗,則不應執行整個“ file”指令。
---
- name: Install Munin Node
any_errors_fatal: true
block:
...
# drives config
- file:
src: /usr/share/munin/plugins/smart_
dest: /etc/munin/plugins/smart_{{ item.key }}
state: link
with_dict: "{{ ansible_devices }}"
when: "item.value.host.startswith('SATA')"
notify:
- restart munin-node
在具有SATA驅動器的目標上,該代碼有效。 找到“ sda”之類的驅動器並創建鏈接。 環路和其他軟設備被忽略(按預期方式)僅在完全沒有SATA驅動器的Raspberry上,我才遇到致命故障。
您正在使用with_dict
選項設置loop
。 這將每次迭代的item
變量的值設置為帶有兩個鍵的字典:
key
: dict
當前鍵的名稱。 value
: dict
現有鍵的值。 然后,您將運行when
選項,該選項將在每次迭代when
檢查item
變量。 因此,請檢查這是否是您想要的行為。
關於你的錯誤,就會被拋出,因為某種原因, ansible_devices
不是dict
的錯誤說。 並且Ansible在解決when
條件之前會檢查with_dict
類型的有效性。
檢查以下示例:
---
- name: Diff test
hosts: local
connection: local
gather_facts: no
vars:
dict:
value: True
name: "dict"
tasks:
- debug: var=item
when: dict.value == False
with_dict: '{{ dict }}'
- debug: var=item
when: dict.value == True
with_dict: '{{ dict }}'
- debug: var=item
when: dict.value == False
with_dict: "Not a dict"
前兩個task
會成功,因為他們有一個有效的dict
上with_dict
的選項和正確的狀態when
的選項。 最后一個將失敗,因為with_dict
值的類型錯誤,即使when
條件可以正確解決,也應保證跳過task
。
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.