Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Inventory Integration to Hypervisors #2709

Open
tl87 opened this issue Jan 23, 2025 · 1 comment
Open

Feature: Inventory Integration to Hypervisors #2709

tl87 opened this issue Jan 23, 2025 · 1 comment
Labels

Comments

@tl87
Copy link

tl87 commented Jan 23, 2025

Related to

Other

Impact

major improvement to user experience

Missing Feature

AWX and AAP offer integration capabilities with various hypervisors, such as VMware, through an API provided by the hypervisor. This integration enables the creation of a dynamic inventory.

As a result, the inventory is automatically updated based on the hosts in VMware or other hypervisors, eliminating the need for manual updates to the inventory file. This ensures that the inventory remains current without relying on others to manage updates, providing an always-up-to-date reflection of the hosts in VMware or another hypervisor.

Implementation

Utilize the API provided by the various hypervisors to retrieve hostnames, which can then undergo a validation process to ensure proper formatting. Once validated, an inventory can be generated and made available for use in Ansible Semaphore. The inventory can be set to update automatically on an hourly basis, or an option can be provided to manually trigger an inventory update as needed.

Design

Although I don't have programming experience, I believe that utilizing an API from a hypervisor to retrieve hostnames may require authentication. This would likely involve setting up a user or API key to allow access to the hypervisor and pull the necessary hostnames. Once retrieved, Semaphore could then format the data and generate the inventory.

Given that different hypervisors have distinct API request structures and formats, I assume that separate integrations would need to be developed for each. It might be beneficial to begin with the most commonly used hypervisors, such as VMware and Proxmox.

@tl87 tl87 added the feature label Jan 23, 2025
@tl87
Copy link
Author

tl87 commented Jan 23, 2025

I found a work-around and you might get some value for adding it to your documentation for Ansible Semaphore, because I can see more people being interested in adding VMware as a dynamic inventory.

The guide is as follows:

ansible - vmware inventory

this guide will help set up a dynamic inventory file in yaml format, where it uses the Ansible collection module community.vmware, the python packages pyVmomi, requests and vSphere Automation SDK.

  1. Install python packages:
pip install \
	ansible \
	requests \
	pyVmomi \
	git+https://github.com/vmware/vsphere-automation-sdk-python.git
  1. Install Ansible collection:
ansible collection install \
	community.vmware
  1. Create a service account in VMware, with the correct API permissions.

  2. Add below to ansible.cfg, to load plugin:

[inventory]
enable_plugins = vmware_vm_inventory
  1. Create the inventory, let's call it vmware.yaml, and add the credentials from the VMware user and the hostname for the vcenter:
plugin: vmware_vm_inventory
strict: false
hostname: vcenter01.domain.com
username: vmware-api-user
password: !vault |
  $ANSIBLE_VAULT;1.1;AES256
  3932.....
validate_certs: false
with_tags: false
groups:
  VMs: true

You can use a clear-text-password, but I recommend using an Ansible encrypted string.

To create encrypted string, use the command below, and the output can then be added to vmware.yaml in the password field:

ansible-vault encrypt_string 'PUT YOUR PASSWORD HERE'
  1. To test if it works, use the command below:
ansible-inventory -i vmware.yml --list --ask-vault-pass

If it all goes well, you should see a list of hosts, it might seem like it's stuck, but that could be that you have alot of VM's or slow connection. Don't worry, simply wait.

Now you are done.

Bonus tip

You can add the inventory to Ansible Semaphore, just make sure you have created a secret for the encrypted string. This assumes you have setup an Repository in Semaphore, where vmware.yaml is committed to.

This guide is made on Ansible Semaphore version v2.11.2, things might be subject to change in newer versions.

  1. Sign into Ansible Semaphore's webgui.

  2. Navigate Key Store and click on New Key.

Set the following:

  • Key Name: vmware encrypted string password
  • Type: Login with password
  • Login (Optional): {blank}
  • Password: {add the password used to encrypt the string in step 5}

Click on the Create button.

  1. Navigate to Inventory and click on New Inventory.

Set the following:

  • Name: vmware inventory
  • User credentials: vmware encrypted string password
  • Sudo Credentials (Optional): {blank}
  • Type: File
  • Path to Inventory file: {set path to vmware.yaml corresponding to where it's located in the repository}
  • Repository: {set to the repo, where vmware.yaml exists}
  1. Done

You can use --limit to limit what host should be used when using this inventory.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant