In today's post we are going to talk about making useful inventories in AWX.  It isn't very hard to setup, but making it look and be usable is another story, that is what I'll be covering today.

Don't worry, this will be super easy.  Barely an inconvenience.

First thing we need to do is create a credential for VMWare.  In AWX, go to Credentials in the left menu.  Create a new credential by clicking the Plus in the top right corner.

Give it a name, something like vCenter.  For the credential type, select VMware vCenter.  This should give you 3 fields to fill out.  Put your vCenter hostname in for  vCenter Host, then the username and password.  Hit save.

Now that we have that done.  Lets go to Inventories in the left menu.  Click the Plus in the top right, then Inventory.  For a name we will give it something like vmware-all.  You have to select an Organization.  I selected Default, select whatever fits your use case.  Hit Save.

Now we need to go to Sources at the top.  Click the Plus in the top right to add a new source.  Give it a name.  I'm just going to name mine vmware.

For the source select VMWare vCenter.  In the credential section, select the credential we created earlier.  Now hit save.  Once it is added you should be able to click "SYNC ALL" at the bottom.  This will go out and grab every VM in your vCenter, it will take some time depending on the size of your environment.

Once it syncs, you can go to hosts and you should see your VMs.  But wait a minute... that is some ugly output.


Now that we have the inventory syncing, lets make it better.  Go back to Sources at the top, then select your source.  The Source Variables box below is where we can do this.  It uses jinja2 filtering, so we can do some neat things here.  Paste the below into the source variables box

---
validate_certs: False
alias_pattern: "{{ config.name }}"
host_filters: "{{ guest.gueststate == 'running' }}"
properties:
  prop01: "config.name"
  prop02: "guest.hostName"
  prop03: "guest.ipAddress"
  prop04: "guest.guestId"
  prop05: "guest.guestState"
  prop06: "guest.guestFamily"
  prop07: "config.annotation"

Now this is a very basic version of what you can do.  It is basically going to only import VMs that are in a running state.  It also will name them whatever they are named in vCenter.

The first line of alias_pattern determines what the name of inventory item will be.  In this case it will be what the name of the VM is in vCenter.

The host_filters line is where you can do some cool stuff. Here we are simply limiting it guests that are in the running state.  Lets see what my inventory looks like now.

Now that is every machine in vCenter that is powered on using the VM name from vCenter.  But lets take this a step further.  Lets separate our Windows and linux boxes.  This will let us specify connection settings such as winrm for windows and ssh parameters for linux.

Lets go create a new inventory.  Lets name this one vmware-windows.  Create the source again the same as before, using VMWare vCenter as the type.

---
validate_certs: False
alias_pattern: "{{ config.name }}"
host_filters: "{{ guest.gueststate == 'running' }},{{ config.guestid == 'windows9Server64Guest' }}"
groupby_patterns: "{{ guest.guestid }}"
properties:
  prop01: "config.name"
  prop02: "guest.hostName"
  prop03: "guest.ipAddress"
  prop04: "guest.guestId"
  prop05: "guest.guestState"
  prop06: "guest.guestFamily"
  prop07: "config.annotation"

Run a sync, and now go look at your hosts.  Mine now only shows the 3 windows boxes I have.

Now comes the cool part.  Go to the details tab on the inventory.  In the variables section you can put your winrm connection info.  Put the below into that box, and save

---
ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore
ansible_winrm_transport: kerberos
ansible_winrm_kerberos_delegation: true

Now if you use your vmware-windows inventory it will know to use winrm instead of SSH to connect.

Now you can do the same thing for linux.  The only difference is instead of

 config.guestid
 you may want to use
 guest.guestfamily == 'linuxGuest'

The reason is the guestid may be specific to your linux distro.

If you want to know what you can filter on,go to the HOSTs tab and click on any host.  All the variables listed you can filter on.

You can do the same thing for Amazon EC2. You will be able to filter on things like tags or ip address etc.

If you have any issues or want to request a topic feel free to contact me here:

Contact
If you’d like to get in touch with me with questions or comments about anything I talk about feel free to reach out to me. You can email me or send me a DM on twitter. blog [@] glitchv0.com (remove [], trying to avoid spambots) Twitter: https://twitter.com/glitchv0