【发布时间】:2018-10-27 06:16:42
【问题描述】:
我们正在开发 Ansible Environemt。我们希望使用其 UUUID 连接到新部署的 VM。
如何使用 Ansible 获取 VMware 虚拟机的 UUID,以便建立连接。
【问题讨论】:
我们正在开发 Ansible Environemt。我们希望使用其 UUUID 连接到新部署的 VM。
如何使用 Ansible 获取 VMware 虚拟机的 UUID,以便建立连接。
【问题讨论】:
您是否查看了此链接:The UUID Location and Format
它可以通过标准的 SMBIOS 扫描软件访问——例如 SiSoftware Sandra 或 IBM utility smbios2 [...]
【讨论】:
您必须先使用 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 }}"
现在继续在您的脚本中使用 {{ vm_uuid }} 您需要虚拟机的 uuid。
【讨论】:
Ansible 模块 vmware_guest_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
以上代码假设您知道虚拟机所在的数据中心。如果不确定,您也可以运行following code:
- 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']"
【讨论】: