Ansible:从文件创建字典

时间:2020-04-28 17:51:13

标签: ansible

我尝试从herehere的var文件创建字典,但未成功。

我导入一个看起来像这样的var文件(vm id由密钥定义)

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

在我的剧本中,我写这篇文章是为了调试:

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

我找不到访问值的方法,所以我的输出如下:

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

1 个答案:

答案 0 :(得分:0)

在此任务中...

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

...您正尝试访问item.keyitem.value,但是item不会具有这些属性。您正在遍历字典(k8s_vms),这意味着您将获得键列表。因此,item的值将是第一次888,第二次是999

此外,此任务称为“填充字典”,但实际上是在创建一个列表,该列表会使阅读您的剧本的任何人感到困惑。

您可能要使用dict2items过滤器:

- 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: []

这是一个示例剧本,可以做到这一点:

- 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

运行会产生:


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