Dicts转换问题的Ansible列表

问题描述 投票:0回答:0

我正在尝试改变 VM 的部署,我有两种 IPAM 自动生成的 IP 和手动分配的 IP。

运行它时,我不断收到错误消息

": "Elements value for option 'networks' is of type <class 'str'>
,我们无法转换为字典:请求字典,无法解析 JSON 或键=值”

我正在努力解决如何以正确格式获取 vmware_guest 模块的数据

我的变量


vm_networks:
  - name: vlan_105
    ip: "auto"
    netmask: "auto"
    gateway: "auto"
    connected: true
    start_connected: true
    device_type: vmxnet3
  - name: vlan_106
    ip: "auto"
    netmask: "auto"
    gateway: "auto"
    connected: true
    start_connected: true
    device_type: vmxnet3
  - name: vlan_107
    ip: 192.168.0.1
    netmask: 255.255.255.0
    gateway: 192.168.0.254
    connected: true
    start_connected: true

在检索并添加 IP 地址后,我的剧本为 vm_networks 输出以下内容

   {
    "vm_networks_updated": [
        {
                "connected": true,
                "device_type": "vmxnet3",
                "gateway": "10.17.1.254",
                "ip": "10.17.1.2",
                "name": "vlan_105",
                "netmask": "255.255.255.0",
                "start_connected": true
            },
            {
                "connected": true,
                "device_type": "vmxnet3",
                "gateway": "10.19.4.254",
                "ip": "10.19.4.2",
                "name": "vlan_106",
                "netmask": "255.255.255.0",
                "start_connected": true
            },
            {
                "connected": true,
                "device_type": "vmxnet3",
                "gateway": "192.168.0.254",
                "ip": "192.168.0.1",
                "name": "vlan_107",
                "netmask": "255.255.255.0",
                "start_connected": true
            }
        ]
    }

我的剧本是:

     var: vm_networks_updated

 - set_fact: 
     vm_networks_updated_keys: "{{ vm_networks_updated|     map(attribute='name')|map('community.general.dict_kv', 'name')|list }}"

 - debug:
     var: vm_networks_updated_keys

 - set_fact: 
     vm_networks_updated_dict: "{{ vm_networks_updated| zip(vm_networks_updated_keys) }}"

 - debug:
     var: vm_networks_updated_dict


 - name: Create a virtual machine from a template
   community.vmware.vmware_guest:
     hostname: "{{ vc_hostname }}"
     username: "{{ vc_username }}"
     password: "{{ vc_password }}"
     validate_certs: "{{ vc_validate_certs }}"
     template: "{{ vm_spec.template |default(vm_spec_template) }}"
     name: "{{ vm_spec.name }}"
     state: poweredon
     datacenter: "{{ vm_spec.datacenter }}"
     cluster: "{{ vm_spec.cluster }}"
     folder: "{{ vm_spec.folder }}"
     disk:
       - datastore: "{{ vm_spec.datastore }}"
         size_kb: "{{ vm_spec.disk_size * 1048576 }}"
         type: thin
     hardware:
       memory_mb: "{{ vm_spec.memory * 1048576 }}"
       num_cpus: "{{ vm_spec.cpus }}"
       num_cpu_cores_per_socket: "{{ vm_spec.cores_per_socket }}"
     networks: |
         "{{ vm_networks_updated_dict|to_yaml }}"
     wait_for_ip_address: true
     delegate_to: localhost
     register: deploy```

my output looks like:
 ```"vm_networks_updated_dict": [
     [
         {
             "connected": true,
             "device_type": "vmxnet3",
             "gateway": "auto",
             "ip": "73e7d1719feaaccf00ef",
             "name": "vlan_105",
             "netmask": "auto",
             "start_connected": true
         },
         {
             "name": "vlan_105"
         }
     ],
     [
         {
             "connected": true,
             "device_type": "vmxnet3",
             "gateway": "auto",
             "ip": "e99d179681813536785b",
             "name": "vlan_106",
             "netmask": "auto",
             "start_connected": true
         },
         {
             "name": "vlan_106"
         }
     ],
     [
         {
             "connected": true,
             "device_type": "vmxnet3",
             "gateway": "192.168.0.254",
             "ip": "192.168.0.1",
             "name": "vlan_107",
             "netmask": "255.255.255.0",
             "start_connected": true
         },
         {
             "name": "vlan_107"
         }
     ]
 ]
}

TASK [deploy_vm : Create a virtual machine from a template]       ****************************************************************************************************   ****************************************************************************
task path: /root/ansible/roles/deploy_vm/tasks/vmguest.yml:18
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c 'echo ~root && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo    /root/.ansible/tmp/ansible-tmp-1682354686.7525673-550065-107719873366957 `" && echo ansible-tmp-   1682354686.7525673-550065-107719873366957="` echo /root/.ansible/tmp/ansible-tmp-1682354686.7525673-   550065-107719873366957 `" ) && sleep 0'
Using module file    /root/.ansible/collections/ansible_collections/community/vmware/plugins/modules/vmware_guest.py
<localhost> PUT /root/.ansible/tmp/ansible-local-54992960i4o82q/tmpzitqivth TO    /root/.ansible/tmp/ansible-tmp-1682354686.7525673-550065-107719873366957/AnsiballZ_vmware_guest.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1682354686.7525673-550065-   107719873366957/ /root/.ansible/tmp/ansible-tmp-1682354686.7525673-550065-107719873366957/AnsiballZ_vmware_guest.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/libexec/platform-python /root/.ansible/tmp/ansible-tmp-   1682354686.7525673-550065-107719873366957/AnsiballZ_vmware_guest.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1682354686.7525673-550065-   107719873366957/ > /dev/null 2>&1 && sleep 0'
fatal: [bitwarden-001]: FAILED! => {
 "changed": false,
 "invocation": {
     "module_args": {
         "advanced_settings": [],
         "annotation": null,
         "cdrom": [],
         "cluster": "Bifrost",
         "convert": null,
         "customization": {
             "autologon": null,
             "autologoncount": null,
             "dns_servers": null,
             "dns_suffix": null,
             "domain": null,
             "domainadmin": null,
             "domainadminpassword": null,
             "existing_vm": null,
             "fullname": null,
             "hostname": null,
             "hwclockUTC": null,
             "joindomain": null,
             "joinworkgroup": null,
             "orgname": null,
             "password": null,
             "productid": null,
             "runonce": null,
             "script_text": null,
             "timezone": null
         },
         "customization_spec": null,
         "customvalues": [],
         "datacenter": "Asgard",
         "datastore": null,
         "delegate_to": "localhost",
         "delete_from_inventory": false,
         "disk": [
             {
                 "autoselect_datastore": null,
                 "controller_number": null,
                 "controller_type": null,
                 "datastore": "StarLord_Datastore01",
                 "disk_mode": null,
                 "filename": null,
                 "size": null,
                 "size_gb": null,
                 "size_kb": 10485760,
                 "size_mb": null,
                 "size_tb": null,
                 "type": "thin",
                 "unit_number": null
             }
         ],
         "esxi_hostname": null,
         "folder": "/testvms",
         "force": false,
         "guest_id": null,
         "hardware": {
             "boot_firmware": null,
             "cpu_limit": null,
             "cpu_reservation": null,
             "cpu_shares": null,
             "cpu_shares_level": null,
             "hotadd_cpu": null,
             "hotadd_memory": null,
             "hotremove_cpu": null,
             "iommu": null,
             "max_connections": null,
             "mem_limit": null,
             "mem_reservation": null,
             "mem_shares": null,
             "mem_shares_level": null,
             "memory_mb": 2097152,
             "memory_reservation_lock": null,
             "nested_virt": null,
             "num_cpu_cores_per_socket": 2,
             "num_cpus": 1,
             "scsi": null,
             "secure_boot": null,
             "version": null,
             "virt_based_security": null,
             "vpmc_enabled": null
         },
         "hostname": "",
         "is_template": false,
         "linked_clone": false,
         "name": "bitwarden-001",
         "name_match": "first",
         "networks": [],
         "nvdimm": {
             "label": null,
             "size_mb": 1024,
             "state": null
         },
         "password": "",
         "port": 443,
         "proxy_host": null,
         "proxy_port": null,
         "register": "deploy",
         "resource_pool": null,
         "snapshot_src": null,
         "state": "poweredon",
         "state_change_timeout": 0,
         "template": "oracle_linux_8.7_v0.1",
         "use_instance_uuid": false,
         "username": "",
         "uuid": null,
         "validate_certs": false,
         "vapp_properties": [],
         "wait_for_customization": false,
         "wait_for_customization_timeout": 3600,
         "wait_for_ip_address": true,
         "wait_for_ip_address_timeout": 300
     }
 },
 "msg": "Elements value for option 'networks' is of type <class 'str'> and we were unable to convert to dict: dictionary requested, could not parse JSON or key=value"```
list ansible jinja2 vmware
© www.soinside.com 2019 - 2024. All rights reserved.