Skip to content

Instantly share code, notes, and snippets.

@ngschmidt
Created December 25, 2023 18:19
Show Gist options
  • Save ngschmidt/0c7687cb62ba6f7bb98feb67ff936906 to your computer and use it in GitHub Desktop.
Save ngschmidt/0c7687cb62ba6f7bb98feb67ff936906 to your computer and use it in GitHub Desktop.
Ansible Collect vSphere Inventory (for Automated Deployments)
---
- hosts: localhost
gather_facts: true
# Before executing ensure that the prerequisites are installed
# `ansible-galaxy collection install vmware.vmware_rest`
# `python3 -m pip install aiohttp`
tasks:
- name: "Get The Clusters"
vmware.vmware_rest.vcenter_cluster_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
register: vmware_clusters
- name: "Get First Cluster's relevant info (like resource pool IDs)"
vmware.vmware_rest.vcenter_cluster_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
cluster: '{{ vmware_clusters.value[0].cluster }}'
register: vmware_clusterinfo
- name: "Get the vSphere Data Centers"
vmware.vmware_rest.vcenter_datacenter_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
register: vmware_datacenters
- name: "Get the vSphere Folders"
vmware.vmware_rest.vcenter_folder_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
register: vmware_folders
- name: "Get the vSphere Content Libraries"
vmware.vmware_rest.content_locallibrary_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
register: vmware_content_libraries
- name: "Get the vSphere Content Library Objects"
vmware.vmware_rest.content_library_item_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
library_id: '{{ item.id }}'
register: vmware_content_libraries_contents
with_items: '{{ vmware_content_libraries.value }}'
- name: "Get the vSphere Hosts"
vmware.vmware_rest.vcenter_host_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
clusters: '{{ vmware_clusters.value[0].cluster }}'
register: vmware_hosts
- name: "Get the vSphere Datastores"
vmware.vmware_rest.vcenter_datastore_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
datacenters: '{{ vmware_datacenters.value[0].datacenter }}'
register: vmware_datastores
- name: "Get the vSphere Networks"
vmware.vmware_rest.vcenter_network_info:
vcenter_hostname: '{{ lookup("env", "VHOSTNAME") }}'
vcenter_username: '{{ lookup("env", "VAPIUSER") }}'
vcenter_password: '{{ lookup("env", "VAPIPASS") }}'
vcenter_validate_certs: false
datacenters: '{{ vmware_datacenters.value[0].datacenter }}'
register: vmware_networks
# Finally, let's use a Jinja template to format and filter the output in a way
# that's easy to read by a person
- name: Dump Variables
template:
src: scoutput.j2
dest: test_output.yml
name: "On-Demand: Get vSphere Inventory"
on:
workflow_dispatch:
permissions:
contents: read
jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- name: Create Variable YAML File
run: |
cat <<EOF > roles/vmware_rest/parameters.yaml
---
EOF
- name: Get VM Data
run: |
cat parameters.yaml
export VAPIUSER="${{ secrets.VAPIUSER }}"
export VAPIPASS="${{ secrets.VAPIPASS }}"
export VHOSTNAME="vcenter-name-here"
ansible-playbook ansible_playbook.yml
cat test_output.yml
clusters:
{% for i in vmware_clusters.value %}
- id: '{{ i.cluster }}'
name: '{{ i.name }}'
{% endfor %}
datacenters:
{% for i in vmware_datacenters.value %}
- id: '{{ i.datacenter }}'
name: '{{ i.name }}'
{% endfor %}
folders:
{% for i in vmware_folders.value %}
- id: '{{ i.folder}}'
name: '{{ i.name }}'
type: '{{ i.type }}'
{% endfor %}
content_libraries:
{% for i in vmware_content_libraries_contents.results %}
{% for ii in i.value %}
- id: {{ ii.id }}
name: {{ ii.name }}
type: {{ ii.type }}
library_id: {{ ii.library_id }}
{% endfor %}
{% endfor %}
hosts:
{% for i in vmware_hosts.value %}
- id: '{{ i.host }}'
name: '{{ i.name }}'
{% endfor %}
datastores:
{% for i in vmware_datastores.value %}
- id: '{{ i.datastore }}'
name: '{{ i.name }}'
type: '{{ i.type }}'
free_space: '{{ i.free_space }}'
capacity: '{{ i.capacity }}'
{% endfor %}
networks:
{% for i in vmware_networks.value %}
- id: '{{ i.network }}'
name: '{{ i.name }}'
type: '{{ i.type }}'
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment