我们正在研究Ansible Environemt。我们希望使用其UUUID连接到新部署的VM。
如何使用Ansible获取VMware虚拟机的UUID,以便建立连接。
发布于 2018-05-17 13:04:00
发布于 2019-05-10 06:08:35
您必须首先使用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的地方使用{{ vm_uuid }}。
发布于 2020-04-13 18:02:11
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上面的代码假设您知道VM所在的数据中心。如果不确定这一点,您也可以运行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']"https://stackoverflow.com/questions/50383646
复制相似问题