Antivíeis vmware_guest_facts usam fatos para multl vm's

1

Estou escrevendo um script no qual implanto e configuro VMs em um ambiente vSphere.

Após a implantação, quero coletar os IPs das VMs para o registro DNS. Os fatos podem ser reunidos para 2 ou mais VMs ao mesmo tempo. Mas como faço para usar os dados coletados para gerar um nome de VM com endereço IP?

Para uma única VM isso funciona para obter o IP, mas quando usado com 2 VMs a variável é indefinida:

- debug:
    var: vm_guest_facts.instance.ipv4

Talvez minha abordagem esteja errada, mas não tenho certeza de como fazer isso.

- name: Gather facts from recently deployed VM's

  vmware_guest_facts:
    validate_certs: False
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_user }}"
    password: "{{ vcenter_pass }}"
    datacenter: "{{ datacenter }}"
    name: "{{ item.key }}"
  register: vm_guest_facts
  with_dict: "{{ vmdetails }}"

- debug:
    var: vm_guest_facts

Resultados (o nome do host e a pasta são Terraform, mas este exemplo usa apenas Ansible):

TASK [Gather facts from standalone ESXi server having datacenter as 'ha- 
datacenter'] 

ok: [terraform.rum.local] => (item={'value': {u'mem': 512, u'network': u'T1- 
PRD', u'datastore': u'nfs-b', u'cpu': 1, u'vmfolder': u'terraform-deploy'}, 
'key': u'testvm4'})
ok: [terraform.rum.local] => (item={'value': {u'mem': 756, u'network': u'T2- 
TEST', u'datastore': u'nfs-a', u'cpu': 2, u'vmfolder': u'terraform-deploy'}, 
'key': u'testvm3'})

TASK [debug]
*************************************
ok: [terraform.rum.local] => {
"vm_guest_facts": {
    "changed": false,
    "msg": "All items completed",
    "results": [
        {
            "_ansible_ignore_errors": null,
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": false,
            "failed": false,
            "instance": {
                "annotation": "",
                "current_snapshot": null,
                "customvalues": {},
                "guest_consolidation_needed": false,
                "guest_question": null,
                "guest_tools_status": "guestToolsRunning",
                "guest_tools_version": "10304",
                "hw_cores_per_socket": 1,
                "hw_datastores": [
                    "nfs-b",
                    "nfs-a"
                ],
                "hw_esxi_host": "esx-a.rum.local",
                "hw_eth0": {
                    "addresstype": "assigned",
                    "ipaddresses": [
                        "192.168.1.12",
                        "fe80::250:56ff:feb8:d51c"
                    ],
                    "label": "Network adapter 1",
                    "macaddress": "00:50:56:b8:d5:1c",
                    "macaddress_dash": "00-50-56-b8-d5-1c",
                    "summary": "DVSwitch: 50 38 43 04 bb 97 81 76-81 51 a6 cd a4 39 2b 61"
                },
                "hw_files": [
                    "[nfs-b] testvm4/testvm4.vmx",
                    "[nfs-b] testvm4/testvm4.nvram",
                    "[nfs-b] testvm4/testvm4.vmsd",
                    "[nfs-b] testvm4/testvm4.vmxf",
                    "[nfs-b] testvm4/testvm4.vmdk"
                ],
                "hw_folder": "/datacenter1/vm/terraform-deploy",
                "hw_guest_full_name": "CentOS 7 (64-bit)",
                "hw_guest_ha_state": null,
                "hw_guest_id": "centos7_64Guest",
                "hw_interfaces": [
                    "eth0"
                ],
                "hw_is_template": false,
                "hw_memtotal_mb": 512,
                "hw_name": "testvm4",
                "hw_power_status": "poweredOn",
                "hw_processor_count": 1,
                "hw_product_uuid": "42387ae9-cac5-1faa-1e84-0859533dd2b0",
                "instance_uuid": "5038a877-e2db-1bfd-6439-78f6522a9049",
                "ipv4": "192.168.1.12",
                "ipv6": "fe80::250:56ff:feb8:d51c",
                "module_hw": true,
                "snapshots": []
            },
            "invocation": {
                "module_args": {
                    "datacenter": "datacenter1",
                    "folder": "/vm",
                    "hostname": "vcenter.rum.local",
                    "name": "testvm4",
                    "name_match": "first",
                    "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                    "port": 443,
                    "username": "[email protected]",
                    "uuid": null,
                    "validate_certs": false
                }
            },
            "item": {
                "key": "testvm4",
                "value": {
                    "cpu": 1,
                    "datastore": "nfs-b",
                    "mem": 512,
                    "network": "T1-PRD",
                    "vmfolder": "terraform-deploy"
                }
            }
        },
        {
            "_ansible_ignore_errors": null,
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": false,
            "failed": false,
            "instance": {
                "annotation": "",
                "current_snapshot": null,
                "customvalues": {},
                "guest_consolidation_needed": false,
                "guest_question": null,
                "guest_tools_status": "guestToolsRunning",
                "guest_tools_version": "10304",
                "hw_cores_per_socket": 1,
                "hw_datastores": [
                    "nfs-a"
                ],
                "hw_esxi_host": "esx-a.rum.local",
                "hw_eth0": {
                    "addresstype": "assigned",
                    "ipaddresses": [
                        "192.168.1.16",
                        "fe80::250:56ff:feb8:5e2c"
                    ],
                    "label": "Network adapter 1",
                    "macaddress": "00:50:56:b8:5e:2c",
                    "macaddress_dash": "00-50-56-b8-5e-2c",
                    "summary": "DVSwitch: 50 38 43 04 bb 97 81 76-81 51 a6 cd a4 39 2b 61"
                },
                "hw_files": [
                    "[nfs-a] testvm3/testvm3.vmx",
                    "[nfs-a] testvm3/testvm3.nvram",
                    "[nfs-a] testvm3/testvm3.vmsd",
                    "[nfs-a] testvm3/testvm3.vmxf",
                    "[nfs-a] testvm3/testvm3.vmdk"
                ],
                "hw_folder": "/datacenter1/vm/terraform-deploy",
                "hw_guest_full_name": "CentOS 7 (64-bit)",
                "hw_guest_ha_state": null,
                "hw_guest_id": "centos7_64Guest",
                "hw_interfaces": [
                    "eth0"
                ],
                "hw_is_template": false,
                "hw_memtotal_mb": 756,
                "hw_name": "testvm3",
                "hw_power_status": "poweredOn",
                "hw_processor_count": 2,
                "hw_product_uuid": "4238b6c3-a81a-cb51-a816-b83627bfcab0",
                "instance_uuid": "5038a0b1-f75a-f8cb-a872-344afdb1bc6f",
                "ipv4": "192.168.1.16",
                "ipv6": "fe80::250:56ff:feb8:5e2c",
                "module_hw": true,
                "snapshots": []
            },
            "invocation": {
                "module_args": {
                    "datacenter": "datacenter1",
                    "folder": "/vm",
                    "hostname": "vcenter.rum.local",
                    "name": "testvm3",
                    "name_match": "first",
                    "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                    "port": 443,
                    "username": "[email protected]",
                    "uuid": null,
                    "validate_certs": false
                }
            },
            "item": {
                "key": "testvm3",
                "value": {
                    "cpu": 2,
                    "datastore": "nfs-a",
                    "mem": 756,
                    "network": "T2-TEST",
                    "vmfolder": "terraform-deploy"
                }
            }
        }
    ]
  }
}
    
por Ewt 25.05.2018 / 21:35

1 resposta

0

Você pode querer usar json_query desta forma. Aqui, estou reunindo fatos de vmware_guest_facts e registrando a saída em no_vm_result . Então, usando esse no_vm_result com json_query para iterar todos os resultados e pegando ipv4 , que pode ser usado para fazer qualquer coisa. Isso é uma coisa bem direta.

---
- name: StackOverflow answer
  gather_facts: no
  vars_files:
    - vcsim_vars.yml
  vars:
    vm_list:
      - DC0_H0_VM0
      - DC0_H0_VM1
  hosts: localhost
  tasks:
    - name: Gather VMware guest facts
      vmware_guest_facts:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        datacenter: DC0
        validate_certs: no
        name: "{{ item }}"
      register: no_vm_result
      with_items: "{{ vm_list }}"

    - name: Get IPv4 address of VMs to add to DNS configuration
      debug:
        msg: "{{ no_vm_result.results | json_query(s_query) }}"
      vars:
        s_query: "[?instance.hw_name == '{{ item }}'].instance.ipv4"
      with_items: "{{ vm_list }}"
    
por 10.10.2018 / 13:41