title: Ansible : Create dict from file
body: <p>I am trying to create a dict from a var file from <a href="https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results">here</a> and <a href="https://stackoverflow.com/questions/50314254/ansible-creating-dictionary-from-a-list">here</a>, unsuccessfully. </p>
  
  <p>I import a var file which looks like this (vm id is defined by the key)</p>
  
  <pre><code>---
  k8s_vms:
    888:
      ip: 10.0.30.110
      mac: ca:d1:23:45:4e:01
    999:  
      ip: 10.0.30.111
      mac: ca:d1:23:45:4e:02
  ...
  </code></pre>
  
  <p>In my playbook, I wrote this for debbuging :</p>
  
  <pre><code>pre_tasks:
      - include_vars: 'vm_vars.yml'
  
  [...]
  
   - name: Populate dict (test with IP)
        set_fact:
          vms_infos: "{{ vms_infos | default([]) + [ {'id': item.key, 'ip': item.value.ip} ] }}"
        with_items:
          - "{{ k8s_vms }}"
  
   - name: Debug
     debug: var=vms_infos
  
   - name: ID and  IP
     debug:
       msg: "VM={{ item.id }}, IP={{ item.ip }}"
     with_items: "{{ vms_infos }}"
  </code></pre>
  
  <p>I can't find a way to access the values, so my output is as follows :</p>
  
  <pre><code>TASK [DEBUG] ***********************************************************************************************************************
  ok: [x.x.x.x] =&gt; {
      "k8s_vms": {
          "888": {
              "ip": "10.0.30.110",
              "mac": "ca:d1:23:45:4e:01"
          },
          "999": {
              "ip": "10.0.30.111",
              "mac": "ca:d1:23:45:4e:02"
          }
      }
  }
  
  TASK [Populate dict] ***************************************************************************************************************
  ok: [x.x.x.x] =&gt; (item={888: {'ip': '10.0.30.110', 'mac': 'ca:d1:23:45:4e:01'}, 999: {'ip': '10.0.30.111', 'mac': 'ca:ca:d1:23:45:4e:02'}})
  
  TASK [DEBUG] ***********************************************************************************************************************
  ok: [x.x.x.x] =&gt; {
      "vms_infos": "[{'id': AnsibleUndefined, 'ip': AnsibleUndefined}]"
  }
  
  TASK [ID and  IP] ******************************************************************************************************************
  fatal: [x.x.x.x]: FAILED! =&gt; {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'id'\n\nThe error appears to be in 'xxx/deploy.yml': line 31, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: ID and  IP\n      ^ here\n"}
  </code></pre>