I was trying to loop over dictionaries and I referred the following thread but it kept on failing:
How to loop over this dictionary in Ansible?
Following is my play book:
- hosts: server_hosts
tasks:
- name: Include dictionary data
include_vars:
file: vars/input_vars.yaml
- name: Show info field from data.yml
debug:
msg: "Id: {{ input_data[item]['version'] }} - info: {{ input_data[item]['name'] }}"
with_items: "{{ input_data.keys() }}"
Following is my dictionary vars/input_vars.yaml file:
input_data:
item_1:
name: "test1"
version: "18.3"
item_2:
name: "test2"
version: "18.3"
item_3:
name: "test3"
version: "18.3"
When I executed the playbook, it fails with following error:
fatal: [192.168.16.120]: FAILED! => {
"ansible_facts": {},
"ansible_included_var_files": [],
"changed": false,
"message": "Syntax Error while loading YAML.\n mapping values are not allowed here\n\nThe error appears to have been in '/git_projects/base/vars/input_vars.yaml': line 2, column 12, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n----\n input_data:\n ^ here\n"
}