Loading vars into a dictionary

Hi,

I’d like to load variables from a file into a dictionary, so I can later iterate over that dictionary.

I’ve managed to load vars into their own dictionaries based on their name:

  • name: load services definitions for the product
    include_vars:
    file: “…/model/service/{{ item }}.yaml”
    name: “service_data_{{ item }}”
    with_items: “{{ product_data.services }}”

I’ve tried various syntax with the ‘name’ parameter, but I always end up with it being treated as a string.

This ends up looking like this:

“service_data_access_interface”: {
“resources”: [
“interface”
],
“service_name”: “access_interface”,
“service_vars”: [
{
“mandatory”: true,
“name”: “description”,
“type”: “string”
}
]
},
“service_data_l2vpn”: {
“resources”: [
“routing_instance”
],
“service_name”: “l2vpn”,
“service_vars”: [
{
“mandatory”: true,
“name”: “description”,
“type”: “string”
}
]
},

Which makes it very difficult to iterate over. Id like to iterate of this to get to the inner level of ‘service_vars’, names like ‘l2vpn’ or ‘access_interface’ come from another dictionary (that’s loaded earlier)

Any idea how to either import into into a dictionary (so i could use subelements) or iterate over the ‘service_vars’ (knowing part of the string used to create the key)?

kind regards
Pshem

After a bit of trying I came up with this solution - perhaps not the cleanest but does the job:

  • name: load services definitions for the product
    include_vars:
    file: “…/model/service/{{ item }}.yaml”
    name: “service_data_{{ item }}”
    with_items: “{{ product_data.services }}”

  • name: repack services definitions
    set_fact: “services={{ services | default() + [ hostvars[inventory_hostname][‘service_data_’ + item] ] }}”
    with_items: “{{ product_data.services }}”

This builds an array of dictionaries that can be iterated over.

kind regards
Pshem