Smart Inventory with Vcenter - problem getting vm_name and ip

Hi Everyone,

I have been trying AWX in a development environment and Tower on Production.
We have a VMWARE for environment for servers virtualization, with 150 servers for now.

When we tried connect a smart inventory option with our vcenter we get the list of vms that we have but with the following problems:

  • most of the vms comes to the smart inventory as <vm_name>, that’s not helpful when we want to run other task to that host.
  • When we have a vm that has 3 interfaces (let’s say: mgmnt int, app int, public int), the smart inventory give me a random default_ipv4_address of those 3.

I would need a way to change how the discover works for the 1st problem and another to filter ips for the 2nd problem.

I’m thinking that for now the only way is to change the py where the smart inventory option triggers vmware to get the info. The only thing is I don’t know where is this py file or if it is something else.

Regards.
Exequiel

I’m writing a blog post on how to solve this type of issue. The root of the problem is that smart inventory can only do equality filtering. To overcome this limitation you can write a playbook that inspects the facts using ansible, which is capable of more complex conditionals. You can then set a per-host fact flag to indicate that a host matches the more complex conditionals. Finally, the smart inventory filter can match against that simple per-host fact flag.

Here is my blog post draft https://docs.google.com/document/d/17Y2Swkd8zG9W2fYYCfEwvz-BvTyh5ZKisBpC3Y-ewgM/edit?usp=drivesdk

Google doc linked about is tough to share. See PDF attached.

(attachments)

Blog - Advanced Smart Inventory.pdf (117 KB)

Hi Chris,

Thanks for your response on this. I read your post and the playbook you wrote is good in theory and could help, but in my case not to much.
With your playbook I will be getting all host that are in the subnet ( Am I right? Do I understood this correctly?).

My case is that I have a Vcenter with 130 vms, each vm has 3 interface (mgmt, app, public), and let’s say that this case we have:
192.168.0.0/24 for mgmt
192.168.1.0/24 for app
8.8.8.0/24 for public (take this an example from googles ips :wink: )

But also I have some bare-metal servers (let’s say 50) with the same interface configuration.

So in that case the dynamic inventory will get all servers no matter where they are vm or physical with the playbook. And with the smart inventory I want to get the ones from vmware for now.

I would need an approach to connect with Vcenter, get the list of vms but “play”/modify the way I insert those hosts into Tower, not only for the correct default interface (mgmt for Tower to connect to them), but also getting the “correct” hostname/vmname (or the vm_name from vsphere). With that in place I could run playbooks that involves connecting by ssh into the vm OS and run stuff, and also using vmware modules on the vms (creating and deleting snapshots, etc).

Sorry but I’m quite new with Tower (not too much with Ansible), may be there is a way to run what I need and I didn’t get it. I tried a few labs in last RedHat Summit and where good, but when I tried to implement in the real scenario with the day-to-day problems, I got stuck.

Hopefully we can work together and get this done. Also present this with our friends in RedHat locally.

Exequiel,

You have to use the subnet example and apply it to your situation. Another example could be if you wanted to create a Smart Inventory with hosts that have > 4GB of ram. Again, because of the smart inventory equality only filtering you can not construct a query for this in AWX. However, with the help of an Ansible playbook fact gathering + set_fact cachable: true + when: clause, you can set a fact that is a flag for the smart inventory filter.

In every case, we are not trying to filter the Dynamic Inventory. We want all the hosts from your cloud to appear in Tower. Smart Inventory is the thing that will filter/reduce the set of hosts.

Let’s solve your first problem; creating a smart inventory that only contains vmware hosts. From your previous messages, you mention that you Dynamic Inventory consists of both vmware virtual machines and bare metal instances and that you want to have smart inventory of ONLY vmware virtual machines. To do this you just need to find something that is unique to one or the other, in the set of facts. If it’s a value that can be filtered on via equality then you can just use a Smart Inventory filter query. If it’s a sort of range query, i.e. vmwarename-5648, vmwarename-5649, … then you need to leverage an Ansible playbook like my example, where you set a fact that is a fixed value that you can perform an equality filter on for a Smart Inventory.

So, can you find a captured fact that is unique to either the bare metal or vmware virtual machines so that they can be differentiated?

What may help and give you more control of what your vcenter inventory is going to return is to do what I’ve done (sans smart inventory hackage). I’ve downloaded the latest vmware_inventory.py from github and uploaded it as a script source. I’ve modified the name of the INI file in the .py first to point to my exact configuration file that I want for that source (example: /etc/ansible/my_vcenter_inventory2.ini). This way you can use the filtering and fun of python to craft up host and group filtering. I also take off the UUID part of the hostname as we ensure unique hostnames. I keep my vcenter inventory separate from my physical boxes but crafted smart inventories can be made to pick and choose (I haven’t tried this yet myself). Note: we are heavily dependent on file structure in vsphere for most environment placements (prod/nonprod etc).

I can share with you my ini file as an example. Just let me know.

Hey Exequiel,

I found the simplest approach was to adjust the source variables. That would take care of your option #1, and once they are imported, you can also see other filtering options to adjust against by looking at a host. Take note of the alias variable config, and see the attached images for reference.

(attachments)

Hi Everyone,

Sorry I was out for a few days. Just wanted to let you know that I was mistaken when talking about smart inventories. I’m using normal inventory with the credentials of vmware and that’s when I get the info I sent.

I will try Garrett and J approach to change the script or get source var. I was wondering if there is something out of the box coming soon.
I was able to get the work done, by doing it manually and I made a workaround for the snapshots with a localhost script with vmware apis to get the info.

Thanks everyone.

Regards.