1 minute read

How to filter and group VMs using dynamic inventory

We would like to pull only certain VMs from our vCenter and put them in different inventory groups. Some examples using filters when importing inventory from vCenter. See the plugin doc for details.

Scenario 1

  • Import VMs in vCenter only with tag “import_to_inventory” under the categories “Test” and “Tower”
  • Put VMs into group name of the corresponding tag category
  • Import only the VMs that are powered on
  • Import VMs with the configured name (VMs are created without duplicate name in vCenter)
---
hostnames: ["config.name"]
groups:
  Tower: "'Tower' in categories"
  Test: "'Test' in categories"
with_tags: yes
filters:
- summary.runtime.powerState == "poweredOn"
- "'import_to_inventory' in tags"

Note:

  • Check host vars of imported host to see what variables are available for filtering
  • If using “properties” to selective import host vars, it must include the one used in filters

Scenario 2

  • Import VMs under the “management” folder for datacenter “LAB”
---
hostnames: ["config.name"]
with_tags: yes
resources:
- datacenter:
  - LAB
  resources:
  - folder:
    - "management"

Scenario 3

  • Import only VMs:
    • in folder Test VMs and under LAB datacenter
    • that are powered on
    • are set as Windows OS on vCenter
  • They should be grouped based on the resource pool and OS. The group name is prefixed accordingly.
---
validate_certs: False
hostnames:
- config.name
filters:
- summary.runtime.powerState == "poweredOn"
- config.guestId.startswith('windows')
properties:
- config.guestId
- config.name
- resourcePool
with_nested_properties: True
keyed_groups:
- key: resourcePool
  separator: ''
  prefix: 'ResourcePool'
- key: config.guestId
  separator: '_'
  prefix: 'OS'
resources:
- datacenter:
  - LAB
  resources:
  - folder:
    - Test VMs

Note: Resource pool ID will be used in the group name, since pool name is not available.

References

Leave a comment