community.vmware.vmware_vm_inventory – VMware Guest inventory source

Note

This plugin is part of the community.vmware collection (version 1.9.0).

To install it use: ansible-galaxy collection install community.vmware.

To use it in a playbook, specify: community.vmware.vmware_vm_inventory.

Synopsis

  • Get virtual machines as inventory hosts from VMware environment.

  • Uses any file which ends with vmware.yml, vmware.yaml, vmware_vm_inventory.yml, or vmware_vm_inventory.yaml as a YAML configuration file.

Requirements

The below requirements are needed on the local controller node that executes this inventory.

  • Python >= 2.7

  • PyVmomi

  • requests >= 2.3

  • vSphere Automation SDK - For tag feature

Parameters

Parameter Choices/Defaults Configuration Comments
cache
boolean
    Choices:
  • no ←
  • yes
ini entries:

[inventory]
cache = no

env:ANSIBLE_INVENTORY_CACHE
Toggle to enable/disable the caching of the inventory's source data, requires a cache plugin setup to work.
cache_connection
string
ini entries:

[defaults]
fact_caching_connection = None

[inventory]
cache_connection = None

env:ANSIBLE_CACHE_PLUGIN_CONNECTION
env:ANSIBLE_INVENTORY_CACHE_CONNECTION
Cache connection data or path, read cache plugin documentation for specifics.
cache_plugin
string
Default:
"memory"
ini entries:

[defaults]
fact_caching = memory

[inventory]
cache_plugin = memory

env:ANSIBLE_CACHE_PLUGIN
env:ANSIBLE_INVENTORY_CACHE_PLUGIN
Cache plugin to use for the inventory's source data.
cache_prefix
string
Default:
"ansible_inventory_"
ini entries:

[default]
fact_caching_prefix = ansible_inventory_

[inventory]
cache_prefix = ansible_inventory_

env:ANSIBLE_CACHE_PLUGIN_PREFIX
env:ANSIBLE_INVENTORY_CACHE_PLUGIN_PREFIX
Prefix to use for cache plugin files/tables
cache_timeout
integer
Default:
3600
ini entries:

[defaults]
fact_caching_timeout = 3600

[inventory]
cache_timeout = 3600

env:ANSIBLE_CACHE_PLUGIN_TIMEOUT
env:ANSIBLE_INVENTORY_CACHE_TIMEOUT
Cache duration in seconds
compose
dictionary
Default:
{}
Create vars from jinja2 expressions.
filters
list / elements=string
Default:
[]
This option allows client-side filtering hosts with jinja templating.
When server-side filtering is introduced, it should be preferred over this.
groups
dictionary
Default:
{}
Add hosts to group based on Jinja2 conditionals.
hostname
string / required
env:VMWARE_HOST
env:VMWARE_SERVER
Name of vCenter or ESXi server.
hostnames
list / elements=string
Default:
["config.name + \"_\" + config.uuid"]
A list of templates in order of precedence to compose inventory_hostname.
Ignores template if resulted in an empty string or None value.
You can use property specified in properties as variables in the template.
keyed_groups
list / elements=string
Default:
[{"key": "config.guestId", "separator": ""}, {"key": "summary.runtime.powerState", "separator": ""}]
Add hosts to group based on the values of a variable.
password
string / required
env:VMWARE_PASSWORD
Password of vSphere user.
port
integer
Default:
443
env:VMWARE_PORT
Port number used to connect to vCenter or ESXi Server.
properties
list / elements=string
Default:
["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"]
Specify the list of VMware schema properties associated with the VM.
These properties will be populated in hostvars of the given VM.
Each value in the list can be a path to a specific property in VM object or a path to a collection of VM objects.
config.name, config.uuid are required properties if hostnames is set to default.
config.guestId, summary.runtime.powerState are required if keyed_groups is set to default.
Please make sure that all the properties that are used in other parameters are included in this options.
In addition to VM properties, the following are special values
Use customValue to populate virtual machine's custom attributes. customValue is only supported by vCenter and not by ESXi.
Use all to populate all the properties of the virtual machine. The value all is time consuming operation, do not use unless required absolutely.
resources
list / elements=string
added in 2.10 of community.vmware
Default:
[]
A list of resources to limit search scope.
Each resource item is represented by exactly one 'vim_type_snake_case:list of resource names pair and optional nested resources
Key name is based on snake case of a vim type name; e.g host_system correspond to vim.HostSystem
strict
boolean
    Choices:
  • no ←
  • yes
If yes make invalid entries a fatal error, otherwise skip and continue.
Since it is possible to use facts in the expressions they might not always be available and we ignore those errors by default.
username
string / required
env:VMWARE_USER
env:VMWARE_USERNAME
Name of vSphere user.
validate_certs
boolean
    Choices:
  • no
  • yes ←
env:VMWARE_VALIDATE_CERTS
Allows connection when SSL certificates are not valid.
Set to false when certificates are not trusted.
with_nested_properties
boolean
    Choices:
  • no ←
  • yes
This option transform flatten properties name to nested dictionary.
with_path
boolean
    Choices:
  • no ←
  • yes
Include virtual machines path.
Set this option to a string value to replace root name from 'Datacenters'.
with_sanitized_property_name
boolean
    Choices:
  • no ←
  • yes
This option allows property name sanitization to create safe property names for use in Ansible.
Also, transforms property name to snake case.
with_tags
boolean
    Choices:
  • no ←
  • yes
Include tags and associated virtual machines.
Requires 'vSphere Automation SDK' library to be installed on the given controller machine.
Please refer following URLs for installation steps

Examples

# 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

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

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

Authors

  • Abhijeet Kasurde (@Akasurde)