Part 12 – Automating VMware Infrastructure with Ansible CLI

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

Install VMware Collection

ansible-galaxy collection install community.vmware

Install required Python libraries system-wide

sudo pip3 install pyVmomi
sudo pip3 install requests

Add vCenter to Ansible host Inventory

Create the Vault File to store our vCenter password

cd /etc/ansible
ansible-vault create vault.yml

Enter 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.yml

Paste the variables

---
vcenter_hostname: "vcsa8-home.ash.local"
vcenter_username: "administrator@vsphere.local"
vcenter_password: "{{ vault_vcenter_password }}"
vcenter_validate_certs: false

Run 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_env

Add to ~/.bashrc

echo 'source /etc/ansible/.vmware_env' >> ~/.bashrc 
echo "read -sp 'VMware Password: ' VMWARE_PASSWORD && export VMWARE_PASSWORD" >> ~/.bashrc
source ~/.bashrc

Verify

echo $VMWARE_HOST
(Visited 22 times, 1 visits today)

By Ash Thomas

Ash Thomas is a seasoned IT professional with extensive experience as a technical expert, complemented by a keen interest in blockchain technology.