How should I properly configure inventories that query VMWare vSphere for node information?

1,568 views
Skip to first unread message

David Reagan

unread,
Oct 12, 2017, 5:09:29 PM10/12/17
to Ansible Project

Also posted here: https://github.com/ansible/awx/issues/386

SUMMARY

What I want to do is create an inventory of all vm's on my vSphere that start with "web-".


I've read http://docs.ansible.com/ansible-tower/latest/html/userguide/inventories.html#vmware-vcenter and spent several hours experimenting with both smart and regular inventories on my test instance of AWX.


I have been able to create a regular inventory and add a vmware source to it. That source then pulls in every single vm we have. I have not been able to make the vmware source filter out all but the "web-" vm's.


I've been able to create a smart inventory that looks that vmware regular inventory and filters out all but the hosts I want.


Is that how I am supposed to do it?


Information about my setup:

Docker version 17.09.0-ce, build afdb6d4
AWX from github, devel branch. Last commit: d02221702fd680cd38068ec49597a7b627228c04
Installed using the docker instructions.


Related questions:


Justin Redman

unread,
Jan 30, 2020, 3:31:18 PM1/30/20
to Ansible Project
Did you ever figure this out or get a good answer? I’ve ran into the exact same issue, found your post on github also, and I’ve found nothing useful.




On Thursday, October 12, 2017 at 4:09:29 PM UTC-5, David Reagan wrote:
> Also posted here: https://github.com/ansible/awx/issues/386
>
> SUMMARY
>
>
> What I want to do is create an inventory of all vm's on my vSphere that start with "web-".
>
>
>
> I've read http://docs.ansible.com/ansible-tower/latest/html/userguide/inventories.html#vmware-vcenter and spent several hours experimenting with both smart and regular inventories on my test instance of AWX.
>
>
>
> I have been able to create a regular inventory and add a vmware
> source to it. That source then pulls in every single vm we have. I have
> not been able to make the vmware source filter out all but the "web-"
> vm's.
>
>
>
> I've been able to create a smart inventory that looks that vmware regular inventory and filters out all but the hosts I want.
>
>
>
> Is that how I am supposed to do it?
>
>
>
> Information about my setup:
> Docker version 17.09.0-ce, build afdb6d4
> AWX from github, devel branch. Last commit: d02221702fd680cd38068ec49597a7b627228c04
> Installed using the docker instructions.
>
>
>
>
> Related questions:
> What syntax can I use in the "INSTANCE FILTERS" box when configuring a vmware source?
> Can I use "*" as a wildcard?What keys can I filter on? (I did look at https://github.com/ansible/ansible/blob/devel/contrib/inventory/vmware_inventory.ini and https://docs.vmware.com/en/VMware-vSphere/6.0/com.vmware.vsphere.vcenterhost.doc/GUID-2B6A1637-384D-4597-B453-B575F0ECD8A7.html but I wasn't able to figure out how to apply that information...)
> In https://github.com/ansible/ansible/blob/13e89b17cc8ac3676837608270a88864ff4282cb/contrib/inventory/vmware_inventory.ini#L101
> there's something about reducing the data looked up to speed up
> inventory refreshes. How do I translate that into the variables textarea
> when configuring my vmware source?Same question as the last bullet for "skip_keys".

william...@gmail.com

unread,
Aug 14, 2020, 2:14:20 PM8/14/20
to Ansible Project

I see this is quite old/stale, however there is no answer to it that I can see... I am trying to use the VMware quick filters and getting no luck with that, however I was able to do something similar to what you are asking for...

I needed all VM names that started with 'TA - Linux'. I put the following into the  inventory/source instance filter field:

{{ "TA - Linux" in config.name }}

and it worked correctly.   Hopefully if anyone else is searching this might help them...  Now if I could figure out how the quick filters work - I need to try and get all VMs that are of some sort of windows variety in one inventory.  I have tried:

{{ "windows" in config.guestfullname }}


but that returns ALL vms...  finding any examples of this is difficult - if I had one example that worked I am sure I could work it backwards and figure the whole quick filters thing out..l

I got that config.guestfullname from https://github.com/ansible/awx/issues/386.   but in there at the end with the server variables, I don't see config.name!  I imagine this is something stupid I am not following but if anyone sees this, I am still looking!

Paul Manno

unread,
Sep 3, 2020, 11:12:25 AM9/3/20
to Ansible Project
Hi all,

So I too am struggling with this, but I do have some things working to some degree.

Environment Details:
  AWX Version:  AWX 14.1.0
  VCenter Server Version: 6.7.0
  ESXi Host Versions: VMware ESXi, 6.0.0, 10719132

Pretty much the only field in the Source to use is the "Source Variables" field.  In there I have the following:

******************
---
alias_pattern: "{{ config.name }}"
host_filters: "{{ '<somevalue>' in config.name }},{{ runtime.powerstate == 'poweredOn' }}"
groupby_patterns: "{{ AnsibleGroup }},{{ AnsibleGroup1 }},{{ AnsibleGroup2 }},{{ AnsibleGroup3 }},{{ AnsibleGroup4 }},{{ AnsibleGroup5 }}"

******************

So that <somevalue> is a portion of the hostname that I'm using.  I figure you could use other config. values there (i.e. config.guestFullName or config.guestId for OS type).

The groupby_patterns I'm using use VCenter "Custom Attributes".  I tried using Tags, but I just can't get it to work.. don't know if it just hasn't been properly implemented yet or maybe it's my mix of VCenter 6.7 with ESX 6.0... not sure but after hours of frustration I gave up and am now setting both custom attributes and tags at build time and hope I can flip to using tags once that gets fully sorted out.

My current struggle is with group_vars.  Specifically... how do you set group_vars on a dynamic inventory?  Are there some VCenter fields I can use that AWX can grab to populate that info with?  What about the "All" group?  How do I specify those vars?  I believe, at least for the moment, that all I can do is go into the AWX gui (or maybe use the awxkit cli) to set vars on each inventory I have defined (I have a few, since I run multiple environments on each VCenter server and I have defined multiple inventories that have a discrete source for each environment).

Hope some of that info proves helpful.

Best,
Paul
Reply all
Reply to author
Forward
0 new messages