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 - 用于标签功能

参数

参数

注释

cache

布尔值

切换以启用/禁用清单源数据的缓存,需要设置缓存插件才能工作。

选择

  • false ←(默认)

  • true

配置

cache_connection

字符串

缓存连接数据或路径,请阅读缓存插件文档了解详细信息。

配置

cache_plugin

字符串

用于清单源数据的缓存插件。

默认值: "memory"

配置

cache_prefix

字符串

用于缓存插件文件/表的前缀。

默认值: "ansible_inventory_"

配置

cache_timeout

整数

缓存持续时间(秒)。

默认值: 3600

配置

compose

字典

从 jinja2 表达式创建变量。

默认值: {}

filters

列表 / 元素=字符串

此选项允许使用 jinja 模板进行客户端筛选主机。

当引入服务器端筛选时,应优先使用此筛选。

默认值: []

groups

字典

根据 Jinja2 条件将主机添加到组。

默认值: {}

hostname

字符串 / 必需

vCenter 或 ESXi 服务器的名称。

配置

hostnames

列表 / 元素=字符串

按优先级顺序组成 inventory_hostname 的模板列表。

如果结果为空字符串或 None 值,则忽略模板。

您可以使用 properties 中指定的属性作为模板中的变量。

默认值: ["config.name + \"_\" + config.uuid"]

keyed_groups

列表 / 元素=字典

根据变量的值将主机添加到组。

默认值: [{"key": "config.guestId", "separator": ""}, {"key": "summary.runtime.powerState", "separator": ""}]

default_value

字符串

在 ansible-core 2.12 中添加

当主机变量的值为空字符串时的默认值。

此选项与 keyed_groups[].trailing_separator 互斥。

key

字符串

用于生成组的输入字典中的键。

parent_group

字符串

键控组的父组。

前缀

字符串

键控组的名称将以此前缀开头。

默认值: ""

分隔符

字符串

用于构建键控组名称的分隔符。

默认值: "_"

尾部分隔符

布尔值

在 ansible-core 2.12 中添加

将此选项设置为 false 可在值为空字符串时省略主机变量后的 keyed_groups[].separator

此选项与 keyed_groups[].default_value 互斥。

选择

  • false

  • true ← (默认)

起始分隔符

布尔值

在 ansible-core 2.11 中添加

keyed_groups 结合使用。

默认情况下,没有提供前缀或分隔符的键控组的名称将以下划线开头。

这是因为默认前缀为 "",默认分隔符为 "_"

如果未给出前缀,请将此选项设置为 false 以省略起始下划线(或其他分隔符)。

如果组名称是从映射派生的,则仍使用分隔符连接项。

要完全不在组名称中使用分隔符,请将键控组的分隔符设置为空字符串。

选择

  • false

  • true ← (默认)

密码

字符串 / 必需

vSphere 用户的密码。

接受 Vault 加密变量。

接受 Jinja 模板化值

配置

端口

整数

用于连接到 vCenter 或 ESXi Server 的端口号。

默认值: 443

配置

属性

列表 / 元素=字符串

指定与 VM 关联的 VMware 架构属性列表。

这些属性将填充到给定 VM 的 hostvars 中。

列表中的每个值都可以是 VM 对象中特定属性的路径,也可以是 VM 对象集合的路径。

如果 hostnames 设置为默认值,则 config.nameconfig.uuid 是必需的属性。

如果 keyed_groups 设置为默认值,则 config.guestIdsummary.runtime.powerState 是必需的属性。

请确保其他参数中使用的所有属性都包含在此选项中。

除了 VM 属性外,以下是特殊值

使用 customValue 来填充虚拟机的自定义属性。 customValue 仅受 vCenter 支持,不受 ESXi 支持。

使用 all 来填充虚拟机的全部属性。值 all 是耗时的操作,除非绝对需要,否则不要使用。

请参考更多可用作属性的 VMware 客机属性 https://docs.ansible.org.cn/ansible/latest/collections/community/vmware/docsite/vmware_scenarios/vmware_inventory_vm_attributes.html

默认值: ["name", "config.cpuHotAddEnabled", "config.cpuHotRemoveEnabled", "config.instanceUuid", "config.hardware.numCPU", "config.template", "config.name", "config.uuid", "guest.hostName", "guest.ipAddress", "guest.guestId", "guest.guestState", "runtime.maxMemoryUsage", "customValue", "summary.runtime.powerState", "config.guestId"]

proxy_host

字符串

将接收所有 HTTPS 请求并转发它们的代理地址。

格式为主机名或 IP。

配置

proxy_port

整数

将接收所有 HTTPS 请求并转发它们的 HTTP 代理端口。

配置

资源

列表 / 元素=字典

限制搜索范围的资源列表。

每个资源项都由一个 'vim_type_snake_case:资源名称列表 对和可选的嵌套 资源 表示

键名称基于 vim 类型名称的蛇形命名法;例如 host_system 对应于 vim.HostSystem

请参阅 VIM 类型

默认值: []

严格

布尔值

如果 yes,则将无效条目视为致命错误,否则跳过并继续。

由于可以在表达式中使用事实,因此它们可能并非总是可用,我们默认会忽略这些错误。

选择

  • false ←(默认)

  • true

子属性

列表 / 元素=字典

在 community.vmware 4.2.0 中添加

来自普通属性的子属性列表。

这些子属性也将填充到给定 VM 的 hostvars 中。

默认值: []

属性

字符串 / 必需

属性的名称

子元素

列表 / 元素=字符串

子元素列表

use_extra_vars

布尔值

在 ansible-core 2.11 中添加

将额外的变量合并到可用于组合的变量中(优先级最高)。

选择

  • false ←(默认)

  • true

配置

用户名

字符串 / 必需

vSphere 用户的名称。

接受 Vault 加密变量。

接受 Jinja 模板化值

配置

validate_certs

布尔值

允许在 SSL 证书无效时进行连接。

当证书不受信任时设置为 false

选择

  • false

  • true ← (默认)

配置

with_nested_properties

布尔值

此选项将扁平化的属性名称转换为嵌套字典。

选择

  • false

  • true ← (默认)

with_path

布尔值

包含虚拟机路径。

将此选项设置为字符串值以替换 'Datacenters' 中的根名称。

选择

  • false ←(默认)

  • true

with_sanitized_property_name

布尔值

此选项允许对属性名称进行清理,以创建可在 Ansible 中使用的安全属性名称。

此外,还会将属性名称转换为蛇形命名法。

选择

  • false ←(默认)

  • true

with_tags

布尔值

包含标签和关联的虚拟机。

需要在给定的控制器机器上安装“vSphere Automation SDK”库。

请参阅以下 URL 以了解安装步骤

https://code.vmware.com/web/sdk/7.0/vsphere-automation-python

选择

  • false ←(默认)

  • true

示例

# 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"

作者

  • Abhijeet Kasurde (@Akasurde)

提示

每个条目类型的配置条目都具有从低到高的优先级顺序。例如,列表中较低的变量将覆盖列表中较高的变量。