I'am trying to automate the creation of the smart_[diskdevice] links to
/usr/share/munin/plugins/smart_
during the installation of the munin node via ansible.
The code here works partially, except there is no diskdevice to link on the target machine. Then I got a fatal failure with
{"msg": "with_dict expects a dict"}
I've review the ansible documentation and tried to search the problem in the web. For my understanding, the whole "file" directive should not be executed if the "when"-statement fails.
---
- 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
On targets with a SATA-Drive, the code works. Drives like "sda" are found and the links are created. Loop- and other soft-Devices are ignored (as intended) Only on a Raspberry with no SATA-Drive at all i got the fatal failure.
You are using the
with_dictoption to set theloop. This sets the value of theitemvariable for each iteration as a dictionary with two keys:key: The name of the current key in thedict.value: The value of the existing key in thedict.You are then running the
whenoption that checks theitemvariable on each iteration. So check if that is the behavior you want.Regarding your error, it is being thrown because for some reason,
ansible_devicesis not adictas the error says. And Ansible checks for the validity of thewith_dicttype before resolving thewhencondition.Check the following example:
The first two
taskwill succeed because they have a validdicton thewith_dictoption and a correct condition on thewhenoption. The last one will fail because thewith_dictvalue has the wrong type, even though thewhencondition resolves correctly and should guarantee to skip thetask.I hope it helps.