Ansible automates VMware vSphere/vCenter tasks provisioning VMs, managing snapshots, configuring networking using declarative YAML playbooks instead of manual clicks so we will explore all that in our Ansible VMware series.
pyVmomi is the official Python SDK VMware’s vSphere API made by VMware .
We will define everything under this structure
/etc/ansible/
├── ansible.cfg ← ansible config file
├── hosts ← inventory with vcenter groups
└── group_vars/
├── home-vcsa.yml ← plain vars (hostname, username, settings)
├── vault.yml ← vault_password_file
ansible-galaxy collection install community.vmwareInstall required Python libraries system-wide
sudo pip3 install pyVmomi
sudo pip3 install requestsAdd vCenter to Ansible host Inventory

Create the Vault File to store our vCenter password
cd /etc/ansible
ansible-vault create vault.ymlEnter a password when prompted, then in the editor type:
vault_vcenter_password: "YourActualPasswordHere"Define the playbook the traditional way (group_vars + vault)
vi /etc/ansible/playbooks/test_vcenter.yml---
- name: Test vCenter connectivity
hosts: home-vcsa
gather_facts: false
connection: local
vars_files:
- /etc/ansible/vault.yml
tasks:
- name: Gather vCenter info
community.vmware.vmware_about_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ vcenter_validate_certs }}"
register: vc_info
- name: Show vCenter version
debug:
msg: "Connected to {{ vc_info.about_info.product_full_name }}"
Set up Group Variables in group_vars
vi /etc/ansible/group_vars/vcenter.ymlPaste the variables
---
vcenter_hostname: "vcsa8-home.ash.local"
vcenter_username: "administrator@vsphere.local"
vcenter_password: "{{ vault_vcenter_password }}"
vcenter_validate_certs: falseRun the playbook

Another way of doing it is one playbooks targets any vCenter and this is the production pattern for managing multiple vCenters uses one playbook with multiple AWX credentials:
cat test_vcenter_awx.yml
---
- name: Test vCenter connectivity
hosts: localhost
gather_facts: false
connection: local
become: false
tasks:
- name: Gather vCenter info
community.vmware.vmware_about_info:
hostname: "{{ lookup('env', 'VMWARE_HOST') }}"
username: "{{ lookup('env', 'VMWARE_USER') }}"
password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
validate_certs: false
register: vc_info
- name: Show vCenter version
debug:
msg: "Connected to {{ vc_info.about_info.product_full_name }}"
Environment Variables (for AWX-style playbooks on CLI)
#Create VMware env file
cat > /etc/ansible/.vmware_env << 'EOF' export VMWARE_HOST=vcsa8-home.ash.local export VMWARE_USER=administrator@vsphere.local EOF
chmod 600 /etc/ansible/.vmware_envAdd to ~/.bashrc
echo 'source /etc/ansible/.vmware_env' >> ~/.bashrc
echo "read -sp 'VMware Password: ' VMWARE_PASSWORD && export VMWARE_PASSWORD" >> ~/.bashrc
source ~/.bashrcVerify
echo $VMWARE_HOST
