I try to master the work with dictionaries at ansible. When I include dict to playbook as at example all works like charm. But when I try to include vars block to my host_vars/myserv.yml file like this:
host_vars/myserv.yml <- There is wrong syntax don't use this example!
vars:
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:
name: Bob Bananarama
telephone: 987-654-3210
playbook.yml
- name: "myserv"
hosts: "myserv"
gather_facts: yes
remote_user: root
tasks:
- name: "Ping hosts"
ping:
roles:
- {role: 'test-role'}
roles/test-role/task/main.yml
- name: Print phone records
debug:
msg: "User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})"
loop: "{{ lookup('dict', users) }}"
I get an error:
FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'dict'. Error was a <class 'ansible.errors.AnsibleError'>, original message: with_dict expects a dict"}
So what I missed? I need to use some lookup option?
UPD: the trouble was pretty simple - just remove "vars:" line from host_vars file and all work's fine :).
Regards
UPD: the trouble was pretty simple - just remove "vars:" line from host_vars file and all work's fine :).