简体   繁体   中英

Ansible : Create dict from file

I am trying to create a dict from a var file from here and here , unsuccessfully.

I import a var file which looks like this (vm id is defined by the key)

---
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
...

In my playbook, I wrote this for debbuging:

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 }}"

I can't find a way to access the values, so my output is as follows:

TASK [DEBUG] ***********************************************************************************************************************
ok: [x.x.x.x] => {
    "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] => (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] => {
    "vms_infos": "[{'id': AnsibleUndefined, 'ip': AnsibleUndefined}]"
}

TASK [ID and  IP] ******************************************************************************************************************
fatal: [x.x.x.x]: FAILED! => {"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"}

In this task...

 - name: Populate dict (test with IP)
      set_fact:
        vms_infos: "{{ vms_infos | default([]) + [ {'id': item.key, 'ip': item.value.ip} ] }}"
      with_items:
        - "{{ k8s_vms }}"

...you're trying to access item.key and item.value , but item isn't going to have those attributes. You're iterating over a dictionary ( k8s_vms ), which means you're going to get a list of keys. So item will have the value 888 the first time, and 999 the second time.

Also, this task is called "populate dict", but you're actually create a list, which is going to confuse anyone reading your playbook.

You probably want to use the dict2items filter:

- name: populate vm_infos list
  set_fact:
    vms_infos: "{{ vms_infos + [{'id': item.key, 'ip': item.value.ip}] }}"
  loop: "{{ k8s_vms|dict2items }}"
  vars:
    vms_infos: []

Here's an example playbook that does that:

- hosts: localhost
  gather_facts: false
  vars:
    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

  tasks:
    - name: populate vm_infos list
      set_fact:
        vms_infos: "{{ vms_infos + [{'id': item.key, 'ip': item.value.ip}] }}"
      loop: "{{ k8s_vms|dict2items }}"
      vars:
        vms_infos: []

    - debug:
        var: vms_infos

Running that produces:


PLAY [localhost] ******************************************************************************

TASK [populate vm_infos list] *****************************************************************
ok: [localhost] => (item={'key': 888, 'value': {'ip': '10.0.30.110', 'mac': 'ca:d1:23:45:4e:01'}})
ok: [localhost] => (item={'key': 999, 'value': {'ip': '10.0.30.111', 'mac': 'ca:d1:23:45:4e:02'}})

TASK [debug] **********************************************************************************
ok: [localhost] => {
    "vms_infos": [
        {
            "id": 888,
            "ip": "10.0.30.110"
        },
        {
            "id": 999,
            "ip": "10.0.30.111"
        }
    ]
}

PLAY RECAP ************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM