Home > other >  Ansible: create a dictionary from a register.results var
Ansible: create a dictionary from a register.results var

Time:10-15

i have the result variable ( see at the bottom ) from a task (modul community.general.proxmox_kvm) and i want create a new dictionary variable from the items that changed in the result variable.

The structure should look like this

 vm_dict:
   80001:
     host_name: ansible-clone1
     host_ip_address: 10.10.0.1
   80002:
     host_name: ansible-clone2
     host_ip_address: 10.10.0.2

What is a good way to accomplish this ? I tryed it with set_fact and combine filter but i get stucked and was not able to put multible values. Also the changed status not worked as expected.

- name: Populate dict
  set_fact:
   _dict: "{{ _dict|default({}) | combine( {item.item.key: item.item.value.host_name} ) }}"
  loop: "{{ provisioning_vm.results }}"
  when: provisioning_vm.changed == true

I will be happy for some suggestions.

Here is the result variable ( type list )

TASK [provisioning_proxmox : print var provisioning_vm] *************************************************************
ok: [localhost] => {
    "provisioning_vm": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "acpi": null,
                        "agent": null,
                        "api_host": "10.0.10.1:8006",
                        "api_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "api_token_id": null,
                        "api_token_secret": null,
                        "api_user": "ansible_admin@pve",
                        "args": null,
                        "autostart": null,
                        "balloon": null,
                        "bios": null,
                        "boot": null,
                        "bootdisk": null,
                        "cicustom": null,
                        "cipassword": null,
                        "citype": null,
                        "ciuser": null,
                        "clone": "arbitrary_template",
                        "cores": null,
                        "cpu": null,
                        "cpulimit": null,
                        "cpuunits": null,
                        "delete": null,
                        "description": null,
                        "digest": null,
                        "efidisk0": null,
                        "force": null,
                        "format": "raw",
                        "freeze": null,
                        "full": true,
                        "hostpci": null,
                        "hotplug": null,
                        "hugepages": null,
                        "ide": null,
                        "ipconfig": null,
                        "keyboard": null,
                        "kvm": null,
                        "localtime": null,
                        "lock": null,
                        "machine": null,
                        "memory": null,
                        "migrate_downtime": null,
                        "migrate_speed": null,
                        "name": "ansible-clone1.dev.test.com",
                        "nameservers": null,
                        "net": null,
                        "newid": 80001,
                        "node": "proxmox01",
                        "numa": null,
                        "numa_enabled": null,
                        "onboot": null,
                        "ostype": null,
                        "parallel": null,
                        "pool": null,
                        "protection": null,
                        "proxmox_default_behavior": "no_defaults",
                        "reboot": null,
                        "revert": null,
                        "sata": null,
                        "scsi": null,
                        "scsihw": null,
                        "searchdomains": null,
                        "serial": null,
                        "shares": null,
                        "skiplock": null,
                        "smbios": null,
                        "snapname": null,
                        "sockets": null,
                        "sshkeys": null,
                        "startdate": null,
                        "startup": null,
                        "state": "present",
                        "storage": "CEPH-DEV",
                        "tablet": null,
                        "tags": null,
                        "target": null,
                        "tdf": null,
                        "template": null,
                        "timeout": 400,
                        "update": false,
                        "validate_certs": false,
                        "vcpus": null,
                        "vga": null,
                        "virtio": null,
                        "vmid": 987654,
                        "watchdog": null
                    }
                },
                "item": {
                    "key": 80001,
                    "value": {
                        "host_ip_address": "10.10.0.1",
                        "host_name": "ansible-clone1.dev.test.com"
                    }
                },
                "msg": "VM with name <ansible-clone1.dev.test.com> already exists",
                "vmid": 80001
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "acpi": null,
                        "agent": null,
                        "api_host": "10.0.10.1:8006",
                        "api_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "api_token_id": null,
                        "api_token_secret": null,
                        "api_user": "ansible_admin@pve",
                        "args": null,
                        "autostart": null,
                        "balloon": null,
                        "bios": null,
                        "boot": null,
                        "bootdisk": null,
                        "cicustom": null,
                        "cipassword": null,
                        "citype": null,
                        "ciuser": null,
                        "clone": "arbitrary_template",
                        "cores": null,
                        "cpu": null,
                        "cpulimit": null,
                        "cpuunits": null,
                        "delete": null,
                        "description": null,
                        "digest": null,
                        "efidisk0": null,
                        "force": null,
                        "format": "raw",
                        "freeze": null,
                        "full": true,
                        "hostpci": null,
                        "hotplug": null,
                        "hugepages": null,
                        "ide": null,
                        "ipconfig": null,
                        "keyboard": null,
                        "kvm": null,
                        "localtime": null,
                        "lock": null,
                        "machine": null,
                        "memory": null,
                        "migrate_downtime": null,
                        "migrate_speed": null,
                        "name": "ansible-clone2.dev.test.com",
                        "nameservers": null,
                        "net": null,
                        "newid": 80002,
                        "node": "proxmox01",
                        "numa": null,
                        "numa_enabled": null,
                        "onboot": null,
                        "ostype": null,
                        "parallel": null,
                        "pool": null,
                        "protection": null,
                        "proxmox_default_behavior": "no_defaults",
                        "reboot": null,
                        "revert": null,
                        "sata": null,
                        "scsi": null,
                        "scsihw": null,
                        "searchdomains": null,
                        "serial": null,
                        "shares": null,
                        "skiplock": null,
                        "smbios": null,
                        "snapname": null,
                        "sockets": null,
                        "sshkeys": null,
                        "startdate": null,
                        "startup": null,
                        "state": "present",
                        "storage": "CEPH-DEV",
                        "tablet": null,
                        "tags": null,
                        "target": null,
                        "tdf": null,
                        "template": null,
                        "timeout": 400,
                        "update": false,
                        "validate_certs": false,
                        "vcpus": null,
                        "vga": null,
                        "virtio": null,
                        "vmid": 987654,
                        "watchdog": null
                    }
                },
                "item": {
                    "key": 80002,
                    "value": {
                        "host_ip_address": "10.10.0.2",
                        "host_name": "ansible-clone2.dev.test.com"
                    }
                },
                "msg": "VM with name <ansible-clone2.dev.test.com> already exists",
                "vmid": 80002
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "acpi": null,
                        "agent": null,
                        "api_host": "10.0.10.1:8006",
                        "api_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "api_token_id": null,
                        "api_token_secret": null,
                        "api_user": "ansible_admin@pve",
                        "args": null,
                        "autostart": null,
                        "balloon": null,
                        "bios": null,
                        "boot": null,
                        "bootdisk": null,
                        "cicustom": null,
                        "cipassword": null,
                        "citype": null,
                        "ciuser": null,
                        "clone": "arbitrary_template",
                        "cores": null,
                        "cpu": null,
                        "cpulimit": null,
                        "cpuunits": null,
                        "delete": null,
                        "description": null,
                        "digest": null,
                        "efidisk0": null,
                        "force": null,
                        "format": "raw",
                        "freeze": null,
                        "full": true,
                        "hostpci": null,
                        "hotplug": null,
                        "hugepages": null,
                        "ide": null,
                        "ipconfig": null,
                        "keyboard": null,
                        "kvm": null,
                        "localtime": null,
                        "lock": null,
                        "machine": null,
                        "memory": null,
                        "migrate_downtime": null,
                        "migrate_speed": null,
                        "name": "ansible-clone4.dev.test.com",
                        "nameservers": null,
                        "net": null,
                        "newid": 80004,
                        "node": "proxmox01",
                        "numa": null,
                        "numa_enabled": null,
                        "onboot": null,
                        "ostype": null,
                        "parallel": null,
                        "pool": null,
                        "protection": null,
                        "proxmox_default_behavior": "no_defaults",
                        "reboot": null,
                        "revert": null,
                        "sata": null,
                        "scsi": null,
                        "scsihw": null,
                        "searchdomains": null,
                        "serial": null,
                        "shares": null,
                        "skiplock": null,
                        "smbios": null,
                        "snapname": null,
                        "sockets": null,
                        "sshkeys": null,
                        "startdate": null,
                        "startup": null,
                        "state": "present",
                        "storage": "CEPH-DEV",
                        "tablet": null,
                        "tags": null,
                        "target": null,
                        "tdf": null,
                        "template": null,
                        "timeout": 400,
                        "update": false,
                        "validate_certs": false,
                        "vcpus": null,
                        "vga": null,
                        "virtio": null,
                        "vmid": 987654,
                        "watchdog": null
                    }
                },
                "item": {
                    "key": 80004,
                    "value": {
                        "host_ip_address": "10.10.10.4",
                        "host_name": "ansible-clone4.dev.test.com"
                    }
                },
                "msg": "VM with name <ansible-clone4.dev.test.com> already exists",
                "vmid": 80004
            }
        ],
        "skipped": false
    }
}

CodePudding user response:

For example,

  vm_dict: "{{ dict(provisioning_vm.results|
                    json_query('[].[item.key, item.value]')) }}"

gives

  vm_dict:
    80001:
      host_ip_address: 10.10.0.1
      host_name: ansible-clone1.dev.test.com
    80002:
      host_ip_address: 10.10.0.2
      host_name: ansible-clone2.dev.test.com
    80004:
      host_ip_address: 10.10.10.4
      host_name: ansible-clone4.dev.test.com

Optionally, the task below rejects changed items. Change the filter to selectattr if you want to select them

  vm_dict: "{{ dict(provisioning_vm.results|
                    rejectattr('changed')|
                    json_query('[].[item.key, item.value]')) }}"
  • Related