community.vmware.vmware_vm_inventory 清单 – VMware 虚拟机清单源
注意
此清单插件是 community.vmware 集合(版本 5.2.0)的一部分。
如果您使用的是 ansible
包,您可能已经安装了此集合。它不包含在 ansible-core
中。要检查是否已安装,请运行 ansible-galaxy collection list
。
要安装它,请使用:ansible-galaxy collection install community.vmware
。您需要进一步的要求才能使用此清单插件,有关详细信息,请参阅要求。
要在 playbook 中使用它,请指定:community.vmware.vmware_vm_inventory
。
概要
从 VMware 环境获取虚拟机作为清单主机。
使用任何以 vmware.yml、vmware.yaml、vmware_vm_inventory.yml 或 vmware_vm_inventory.yaml 结尾的文件作为 YAML 配置文件。
要求
本地控制器节点上需要以下要求才能执行此清单。
requests >= 2.3
vSphere Automation SDK - 用于标签功能
参数
参数 |
注释 |
---|---|
切换以启用/禁用清单源数据的缓存,需要设置缓存插件才能工作。 选择
配置
|
|
缓存连接数据或路径,请阅读缓存插件文档了解详细信息。 配置
|
|
用于清单源数据的缓存插件。 默认值: 配置
|
|
用于缓存插件文件/表的前缀。 默认值: 配置
|
|
缓存持续时间(秒)。 默认值: 配置
|
|
从 jinja2 表达式创建变量。 默认值: |
|
此选项允许使用 jinja 模板进行客户端筛选主机。 当引入服务器端筛选时,应优先使用此筛选。 默认值: |
|
根据 Jinja2 条件将主机添加到组。 默认值: |
|
按优先级顺序组成 inventory_hostname 的模板列表。 如果结果为空字符串或 None 值,则忽略模板。 您可以使用 默认值: |
|
根据变量的值将主机添加到组。 默认值: |
|
当主机变量的值为空字符串时的默认值。 此选项与 |
|
用于生成组的输入字典中的键。 |
|
键控组的父组。 |
|
键控组的名称将以此前缀开头。 默认值: |
|
用于构建键控组名称的分隔符。 默认值: |
|
将此选项设置为 此选项与 选择
|
|
与 默认情况下,没有提供前缀或分隔符的键控组的名称将以下划线开头。 这是因为默认前缀为 如果未给出前缀,请将此选项设置为 如果组名称是从映射派生的,则仍使用分隔符连接项。 要完全不在组名称中使用分隔符,请将键控组的分隔符设置为空字符串。 选择
|
|
指定与 VM 关联的 VMware 架构属性列表。 这些属性将填充到给定 VM 的 hostvars 中。 列表中的每个值都可以是 VM 对象中特定属性的路径,也可以是 VM 对象集合的路径。 如果 如果 请确保其他参数中使用的所有属性都包含在此选项中。 除了 VM 属性外,以下是特殊值 使用 使用 请参考更多可用作属性的 VMware 客机属性 https://docs.ansible.org.cn/ansible/latest/collections/community/vmware/docsite/vmware_scenarios/vmware_inventory_vm_attributes.html 默认值: |
|
限制搜索范围的资源列表。 每个资源项都由一个 键名称基于 vim 类型名称的蛇形命名法;例如 请参阅 VIM 类型 默认值: |
|
如果 由于可以在表达式中使用事实,因此它们可能并非总是可用,我们默认会忽略这些错误。 选择
|
|
来自普通属性的子属性列表。 这些子属性也将填充到给定 VM 的 hostvars 中。 默认值: |
|
属性的名称 |
|
子元素列表 |
|
将额外的变量合并到可用于组合的变量中(优先级最高)。 选择
配置
|
|
此选项将扁平化的属性名称转换为嵌套字典。 选择
|
|
包含虚拟机路径。 将此选项设置为字符串值以替换 'Datacenters' 中的根名称。 选择
|
|
此选项允许对属性名称进行清理,以创建可在 Ansible 中使用的安全属性名称。 此外,还会将属性名称转换为蛇形命名法。 选择
|
|
包含标签和关联的虚拟机。 需要在给定的控制器机器上安装“vSphere Automation SDK”库。 请参阅以下 URL 以了解安装步骤 https://code.vmware.com/web/sdk/7.0/vsphere-automation-python 选择
|
示例
# Sample configuration file for VMware Guest dynamic inventory
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
with_tags: true
# Sample configuration file for VMware Guest dynamic inventory using Jinja to template the username and password.
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: '{{ (lookup("file","~/.config/vmware.yaml") | from_yaml).username }}'
password: '{{ (lookup("file","~/.config/vmware.yaml") | from_yaml).password }}'
validate_certs: false
with_tags: true
# Gather minimum set of properties for VMware guest
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
properties:
- 'name'
- 'guest.ipAddress'
- 'config.name'
- 'config.uuid'
# Gather subproperties such as the parent (mostly cluster) of an ESXi
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
properties:
- 'name'
- 'guest.ipAddress'
- 'config.name'
- 'config.uuid'
subproperties:
- property: 'summary.runtime.host'
subelements:
- 'name'
- 'parent.name'
# Create Groups based upon VMware Tools status
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
properties:
- 'name'
- 'config.name'
- 'guest.toolsStatus'
- 'guest.toolsRunningStatus'
hostnames:
- config.name
keyed_groups:
- key: guest.toolsStatus
separator: ''
- key: guest.toolsRunningStatus
separator: ''
# Filter VMs based upon condition
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
properties:
- 'runtime.powerState'
- 'config.name'
filters:
- runtime.powerState == "poweredOn"
hostnames:
- config.name
# Filter VM's based on OR conditions
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
properties:
- 'name'
- 'config.name'
- 'guest.ipAddress'
- 'guest.toolsStatus'
- 'guest.toolsRunningStatus'
- 'config.guestFullName'
- 'config.guestId'
hostnames:
- 'config.name'
filters:
- config.guestId == "rhel7_64Guest" or config.name == "rhel_20_04_empty"
# Filter VM's based on regex conditions
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
properties:
- 'config.name'
- 'config.guestId'
- 'guest.ipAddress'
- 'summary.runtime.powerState'
filters:
- guest.ipAddress is defined and (guest.ipAddress is match('192.168.*') or guest.ipAddress is match('192.169.*'))
# Using compose and groups
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
properties:
- 'name'
- 'config.name'
- 'guest.ipAddress'
compose:
# This will populate the IP address of virtual machine if available
# and will be used while communicating to the given virtual machine
ansible_host: 'guest.ipAddress'
composed_var: 'config.name'
# This will populate a host variable with a string value
ansible_user: "'admin'"
ansible_connection: "'ssh'"
groups:
VMs: true
hostnames:
- config.name
# Use Datacenter, Cluster and Folder value to list VMs
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.200.241
username: [email protected]
password: Esxi@123$%
validate_certs: false
with_tags: true
resources:
- datacenter:
- Asia-Datacenter1
- Asia-Datacenter2
resources:
- compute_resource:
- Asia-Cluster1
resources:
- host_system:
- Asia-ESXI4
- folder:
- dev
- prod
# Use Category and it's relation with Tag
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.201.128
username: [email protected]
password: Esxi@123$%
validate_certs: false
hostnames:
- 'config.name'
properties:
- 'config.name'
- 'config.guestId'
- 'guest.ipAddress'
- 'summary.runtime.powerState'
with_tags: true
keyed_groups:
- key: tag_category.OS
prefix: "vmware_tag_os_category_"
separator: ""
with_nested_properties: true
filters:
- "tag_category.OS is defined and 'Linux' in tag_category.OS"
# customizing hostnames based on VM's FQDN. The second hostnames template acts as a fallback mechanism.
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
hostnames:
- 'config.name+"."+guest.ipStack.0.dnsConfig.domainName'
- 'config.name'
properties:
- 'config.name'
- 'config.guestId'
- 'guest.hostName'
- 'guest.ipAddress'
- 'guest.guestFamily'
- 'guest.ipStack'
# Select a specific IP address for use by ansible when multiple NICs are present on the VM
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.223.31
username: [email protected]
password: Esxi@123$%
validate_certs: false
compose:
# Set the IP address used by ansible to one that starts by 10.42. or 10.43.
ansible_host: >-
guest.net
| selectattr('ipAddress')
| map(attribute='ipAddress')
| flatten
| select('match', '^10.42.*|^10.43.*')
| list
| first
properties:
- guest.net
# Group hosts using Jinja2 conditionals
plugin: community.vmware.vmware_vm_inventory
strict: false
hostname: 10.65.13.37
username: [email protected]
password: Esxi@123$%
validate_certs: false
hostnames:
- config.name
properties:
- 'name'
- 'config.name'
- 'config.datastoreUrl'
groups:
slow_storage: "'Nas01' in config.datastoreUrl[0].name"
fast_storage: "'SSD' in config.datastoreUrl[0].name"