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 vault_password_file = /etc/ansible/.vault_pass
├── .vault_pass the KEY (chmod 600, never in Git)
├── hosts [home_vcsa] vcenter.lab.internal
└── group_vars/
├── home_vcsa.yml vcenter_hostname / username / password: “{{ vault_vcenter_password }}”
└── all/
└── vault.yml vault_vcenter_password:
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 key file
cd /etc/ansible
openssl rand -base64 32 > .vault_pass
chmod 600 .vault_passCreate the Vault File to store our vCenter password
ansible-vault create group_vars/all/vault.ymlNo password prompt appears, because it uses .vault_pass automatically.
vault_vcenter_password: "our-svc-automation-password"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

For AWX
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=vcenter.lab.internal
export VMWARE_USER=svc-automation@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
