使用Ansible获取VMware虚拟机的UUID

时间:2018-05-17 05:02:26

标签: ansible vmware

我们正致力于Ansible Environemt。我们希望使用其UUUID连接到新部署的VM。

如何使用Ansible获取VMware虚拟机的UUID,以便我可以建立连接。

3 个答案:

答案 0 :(得分:0)

您是否检查过以下链接:The UUID Location and Format

  

例如,它可以通过标准SMBIOS扫描软件访问   SiSoftware SandraIBM utility smbios2 [...]

答案 1 :(得分:0)

必须首先使用vmware_guest_facts模块,然后检索UUID。但是,有两个标识为uuid,因此我将它们都列出。我假设您想要的uuid是instance_uuid。

tasks:
- name: get list of facts
  vmware_guest_facts:
    hostname: '{{ vc_name }}'
    username: '{{ vc_user }}'
    password: '{{ vc_pwd }}'
    datacenter: "{{ dc_name }}"
    name: "{{ vm_name }}"
    folder: "{{ dc_folder }}"
    validate_certs: False
  register: vm_facts

- set_fact:
    vm_uuid: "{{ vm_facts.instance.instance_uuid }}"

  - debug:
      msg: "product uuid hw : {{ vm_facts.instance.hw_product_uuid }}\n instance: {{ vm_facts.instance.instance_uuid }}"

现在继续执行脚本,并在需要虚拟机uuid的地方使用{{vm_uuid}}。

答案 2 :(得分:0)

已弃用Ansible模块“ Vmware_gust_facts”。这将无法在Ansible 2.9中运行,而您需要使用vmware_guest_info。


- name: Getting VMWARE UUID
  hosts: localhost
  gather_facts: false
  connection: local
  tasks:
  - name: Get Virtual Machine info
    vmware_guest_info:
      validate_certs: no
      hostname: "{{ vcenter_hostname }}"
      username: "{{ Password }}"
      password:  "{{ pass }}"
      validate_certs: no
      datacenter: "{{ datacenter_name }}"
      name: "{{ VM_Name }}"
      schema: "vsphere"
      properties:
    delegate_to: localhost
    register: vminfo
   - debug:
       var: vminfo.instance.config.uuid

上面的代码假定您知道VM所在的数据中心:如果不确定,您还可以运行以下代码:REF:https://docs.ansible.com/ansible/latest/modules/vmware_vm_info_module.html#vmware-vm-info-module

- name: Get UUID from given VM Name
  block:
    - name: Get virtual machine info
      vmware_vm_info:
        hostname: '{{ vcenter_hostname }}'
        username: '{{ vcenter_username }}'
        password: '{{ vcenter_password }}'
        folder: "/datacenter/vm/folder"
      delegate_to: localhost
      register: vm_info

    - debug:
        msg: "{{ item.uuid }}"
      with_items:
        - "{{ vm_info.virtual_machines | json_query(query) }}"
      vars:
        query: "[?guest_name=='DC0_H0_VM0']"