如何通过 vmware_guest_info 获取特定 IP?

How to fetch a specific IP via vmware_guest_info?

我使用 vmware_guest_info: 来获取我的 VM 的 DHCP 地址。问题是,在我配置了几个静态 IP 后,guest.ipAddress 从 DHCP 地址(通常是第一个网络适配器)更改为随机配置的静态 IP:

---

- name: Gather a virtual machine info
  vmware_guest_info:
    hostname: '{{ vsphere_host }}'
    username: '{{ vsphere_user }}'
    password: '{{ vsphere_password }}'
    validate_certs: false
    datacenter: "{{ vsphere_datacenter }}"
    name: "{{ hostname }}"
    schema: vsphere
    properties:
      - guest.ipAddress
  retries: 60
  delay: 10
  until: gather_vm_info.instance.guest.ipAddress is not none
  register: gather_vm_info
  delegate_to: localhost

- name: Gather a virtual machine info
  vmware_guest_info:
    hostname: '{{ vsphere_host }}'
    username: '{{ vsphere_user }}'
    password: '{{ vsphere_password }}'
    validate_certs: false
    datacenter: "{{ vsphere_datacenter }}"
    name: "{{ hostname }}"
    schema: vsphere
    properties:
      - guest.ipAddress
  retries: 60
  delay: 10
  until: gather_vm_info.instance.guest.ipAddress != "0.0.0.0"
  register: gather_vm_info
  delegate_to: localhost

- debug: var=gather_vm_info.instance.guest.ipAddress

- debug: var=gather_vm_info

- name: "Update host_var with new IP Address"
  set_fact: 
    ansible_host: "{{ gather_vm_info.instance.guest.ipAddress }}"
    ansible_hostname: "{{ hostname }}"

这是我得到的:

ok: [1-Europe-ECV-Site2] => 
  gather_vm_info.instance.guest.net:
  - _vimtype: vim.vm.GuestInfo.NicInfo
    connected: true
    deviceConfigId: -1
    dnsConfig: null
    ipAddress:
    - fe80::fc70:f8ff:fe15:ae75
    ipConfig:
      _vimtype: vim.net.IpConfigInfo
      autoConfigurationEnabled: null
      dhcp: null
      ipAddress:
      - _vimtype: vim.net.IpConfigInfo.IpAddress
        ipAddress: fe80::fc70:f8ff:fe15:ae75
        lifetime: null
        origin: null
        prefixLength: 64
        state: unknown
    macAddress: fe:70:f8:15:ae:75
    netBIOSConfig: null
    network: null
  - _vimtype: vim.vm.GuestInfo.NicInfo
    connected: true
    deviceConfigId: -1
    dnsConfig: null
    ipAddress:
    - fe80::dc8b:91ff:fefe:66dd
    ipConfig:
      _vimtype: vim.net.IpConfigInfo
      autoConfigurationEnabled: null
      dhcp: null
      ipAddress:
      - _vimtype: vim.net.IpConfigInfo.IpAddress
        ipAddress: fe80::dc8b:91ff:fefe:66dd
        lifetime: null
        origin: null
        prefixLength: 64
        state: unknown
    macAddress: de:8b:91:fe:66:dd
    netBIOSConfig: null
    network: null
  - _vimtype: vim.vm.GuestInfo.NicInfo
    connected: true
    deviceConfigId: -1
    dnsConfig: null
    ipAddress:
    - fe80::f8ea:c4ff:fe90:99e2
    ipConfig:
      _vimtype: vim.net.IpConfigInfo
      autoConfigurationEnabled: null
      dhcp: null
      ipAddress:
      - _vimtype: vim.net.IpConfigInfo.IpAddress
        ipAddress: fe80::f8ea:c4ff:fe90:99e2
        lifetime: null
        origin: null
        prefixLength: 64
        state: unknown
    macAddress: fa:ea:c4:90:99:e2
    netBIOSConfig: null
    network: null
  - _vimtype: vim.vm.GuestInfo.NicInfo
    connected: true
    **deviceConfigId: 4000**
    dnsConfig: null
    ipAddress:
    - 192.168.0.35
    - fe80::250:56ff:fe91:c8c0
    ipConfig:
      _vimtype: vim.net.IpConfigInfo
      autoConfigurationEnabled: null
      dhcp: null
      ipAddress:
      - _vimtype: vim.net.IpConfigInfo.IpAddress
        ipAddress: 192.168.0.35
        lifetime: null
        origin: null
        prefixLength: 16
        state: preferred
      - _vimtype: vim.net.IpConfigInfo.IpAddress
        ipAddress: fe80::250:56ff:fe91:c8c0
        lifetime: null
        origin: null
        prefixLength: 64
        state: unknown

看起来 VMWare 为第一个网络接口分配了 deviceConfigId: 4000,但我如何在我的 Playbook 上访问它?

debug: var=gather_vm_info.instance.guest.net[3].ipAddress[0]-?????

编辑:

有人可以解释一下,为什么 vSphere 将某些 VM 的 IP 地址正确注册为 guest.ipAddress,有时您必须将其注册为 gather_vm_info.instance.hw_eth0.ipaddresses.

这很好,但在此示例中,您可以看到 IP 地址仅注册为 gather_vm_info.instance.hw_eth0.ipaddresses 而您没有注册得到它作为 hw_eth0.ipaddresses???

TASK [debug] ******************************************************************************************************************************************************************************
ok: [1-USA-ECV-Site8] => 
  gather_vm_info:
    changed: false
    failed: false
    instance:
      annotation: Silver Peak Systems ECV
      current_snapshot: null
      customvalues: {}
      guest_consolidation_needed: false
      guest_question: null
      guest_tools_status: guestToolsRunning
      guest_tools_version: '10341'
      hw_cluster: null
      hw_cores_per_socket: 1
      hw_datastores:
      - Datastore2
      hw_esxi_host: 51.89.43.64
      hw_eth0:
        addresstype: assigned
        ipaddresses: null <<-- IP Address is missing!?
        label: Network adapter 1
        macaddress: 00:50:56:91:55:12
        macaddress_dash: 00-50-56-91-55-12
        portgroup_key: null
        portgroup_portkey: null
        summary: Management
      hw_eth1:
        addresstype: assigned
        ipaddresses: null
        label: Network adapter 2
        macaddress: 00:50:56:91:26:a1
        macaddress_dash: 00-50-56-91-26-a1
        portgroup_key: null
        portgroup_portkey: null
        summary: 1-MPLS
      hw_eth2:
        addresstype: assigned
        ipaddresses:
        - 10.8.10.1
        - fe80::250:56ff:fe91:361d
        label: Network adapter 3
        macaddress: 00:50:56:91:36:1d
        macaddress_dash: 00-50-56-91-36-1d
        portgroup_key: null
        portgroup_portkey: null
        summary: 1-Site8
      hw_eth3:
        addresstype: assigned
        ipaddresses: null
        label: Network adapter 4
        macaddress: 00:50:56:91:b7:ce
        macaddress_dash: 00-50-56-91-b7-ce
        portgroup_key: null
        portgroup_portkey: null
        summary: 1-Site8
      hw_eth4:
        addresstype: assigned
        ipaddresses: null
        label: Network adapter 5
        macaddress: 00:50:56:91:68:73
        macaddress_dash: 00-50-56-91-68-73
        portgroup_key: null
        portgroup_portkey: null
        summary: 1-Site8
      hw_eth5:
        addresstype: assigned
        ipaddresses:
        - fe80::250:56ff:fe91:b5c8
        label: Network adapter 6
        macaddress: 00:50:56:91:b5:c8
        macaddress_dash: 00-50-56-91-b5-c8
        portgroup_key: null
        portgroup_portkey: null
        summary: 1-Site8
      hw_eth6:
        addresstype: assigned
        ipaddresses: null
        label: Network adapter 7
        macaddress: 00:50:56:91:83:ab
        macaddress_dash: 00-50-56-91-83-ab
        portgroup_key: null
        portgroup_portkey: null
        summary: 1-Site8
      hw_files:
      - '[Datastore2] 1-USA-ECV-Site8/1-USA-ECV-Site8.vmx'
      - '[Datastore2] 1-USA-ECV-Site8/1-USA-ECV-Site8.nvram'
      - '[Datastore2] 1-USA-ECV-Site8/1-USA-ECV-Site8.vmsd'
      - '[Datastore2] 1-USA-ECV-Site8/vmware.log'
      - '[Datastore2] 1-USA-ECV-Site8/1-USA-ECV-Site8.vmdk'
      - '[Datastore2] 1-USA-ECV-Site8/1-USA-ECV-Site8_1.vmdk'
      hw_folder: /OVH/vm
      hw_guest_full_name: Other 4.x or later Linux (64-bit)
      hw_guest_ha_state: null
      hw_guest_id: other4xLinux64Guest
      hw_interfaces:
      - eth0
      - eth1
      - eth2
      - eth3
      - eth4
      - eth5
      - eth6
      hw_is_template: false
      hw_memtotal_mb: 4096
      hw_name: 1-USA-ECV-Site8
      hw_power_status: poweredOn
      hw_processor_count: 2
      hw_product_uuid: 4211ed87-aa18-53bb-a983-7aff188b115a
      hw_version: vmx-14
      instance_uuid: 5011fd96-b512-e3d1-d6f0-ac58201e75db
      ipv4: 192.168.0.212 <<-- IP Address is only assigned here!?
      ipv6: null
      module_hw: true
      moid: vm-2296
      snapshots: []
      vimref: vim.VirtualMachine:vm-2296
      vnc: {}

第一个接口的IP地址可以通过vmware_guest_info模块结果的hw_eth0属性得到。
这是示例剧本。

---
- name: Example Playbook
  hosts: localhost
  gather_facts: false
  vars:
    hostname: test_vm2
  tasks:
    - name: Gather a virtual machine info
      vmware_guest_info:
        hostname: '{{ vsphere_host }}'
        username: '{{ vsphere_user }}'
        password: '{{ vsphere_password }}'
        validate_certs: false
        datacenter: "{{ vsphere_datacenter }}"
        name: "{{ hostname }}"
      register: gather_vm_info

    - name: Display the first interface set IP Address of a virtual machine
      debug:
        msg: >-
          {{ gather_vm_info.instance.hw_eth0.ipaddresses
            | map('ipaddr', 'ipv4')
            | select('!=', false)
            | list
            | first
          }}

hw_eth0是虚拟机的接口信息。
如果要获取第二个接口IP地址,将hw_eth0改为hw_eth1。
上面的剧本使用 ipaddr 过滤器只获取 ipv4 地址。
所以需要安装netaddr库。

(venv)$ pip install netaddr

顺便说一句,下面的剧本也可以获取第一个接口IP地址。

---
- name: Example Playbook
  hosts: localhost
  gather_facts: false
    hostname: test_vm2
  tasks:
    - name: Gather a virtual machine info
      vmware_guest_info:
        hostname: '{{ vsphere_host }}'
        username: '{{ vsphere_user }}'
        password: '{{ vsphere_password }}'
        validate_certs: false
        datacenter: "{{ vsphere_datacenter }}"
        name: "{{ hostname }}"
        schema: vsphere
        properties:
          - guest.net
      register: gather_vm_info

    - name: Set device_config_ids variable
      set_fact:
        device_config_ids: >-
         {{ device_config_ids | default([])
           + [item.deviceConfigId]
          }}
      loop: "{{ gather_vm_info.instance.guest.net }}"

    - name: Display the first interface set IP Address of a virtual machine
      debug:
        msg: "{{ item.1.ipAddress.0 }}"
      with_nested:
        - "{{ device_config_ids | sort | first }}"
        - "{{ gather_vm_info.instance.guest.net }}"
      when:
        - item.0 | int == item.1.deviceConfigId