Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Virtualization / VMware / Managing VMware Infrastructure with Ansible

December 11, 2023

Managing VMware Infrastructure with Ansible

You can use Ansible to automate some tasks in your VMware infrastructure. You can automatically deploy and configure ESXi hosts, manage network settings, start, stop, or delete VMs, deploy virtual machines from templates, install updates on ESXi hosts, etc. This post explains how you can get started with using Ansible to manage your VMware environment.

Ansible has a large number of modules for managing VMware infrastructure and most of them are based on pyVmomi (a Python SDK that allows you to connect to VMware vSphere API). Install pyVmomi with pip manager:

$ sudo pip install pyvmomi

pip install pyVmomi

We assume you already have a Linux host with Ansible installed.

To allow the use of VMware dynamic inventory plugin, enable the vmware_vm_inventory in ansible.cfg:

[inventory]
enable_plugins = vmware_vm_inventory, host_list, script, auto, yaml, ini, toml
vmware_vars
plugin: vmware_vm_inventory
strict: False
hostname: 192.168.13.70
username: [email protected]
password: pas1swsew43
validate_certs: False
with_tags: True

Then you can run the inventory command and get a list of virtual machines on your vCenter or ESXi server.

$ ansible-inventory --list -i /etc/ansible/vmware/vmware.yml

You can get a hierarchical list of VMs:

$ ansible-inventory --graph -i /etc/ansible/vmware/vmware.yml

ansible-inventory vmware host

Let’s add the list of your ESXi hosts to /etc/ansible/hosts:

[all_esxi_hosts]
esxi1 ansible_host=192.168.31.20
esxi2 ansible_host=192.168.31.21
esxi3 ansible_host=192.168.31.22

Set the variable values for this group of hosts:

[all_esxi_hosts:vars]
ansible_user=root
ansible_python_interpreter=/bin/python
ansible_ssh_pass=P1ssw0rd6

add vmware host names and credentials to /etc/ansible/hosts

In this example, the ESXi host root password is specified in clear text. It is recommended that you configure SSH key-based authentication on ESXi in a production environment.

You can then verify that all ESXi hosts are accessible from the Ansible host over the network:

$ ansible all_esxi_hosts -m ping

You can interactively prompt for the password if you have not specified it in the hosts file:

$ ansible all_esxi_hosts -m ping --ask-pass

ansible ping vmware esxi

With Ansible, you can run any command against all of your ESXi hosts. Ansible ad-hoc command mode is used to execute the command on the remote ESXi. For example, you might want to know the ESXi version number of all your hosts:

$ ansible all_esxi_hosts -m shell -a "vmware -vl"

Run command on multiple ESXi hosts with Ansible

In this way, you can run any shell command on any of the hosts (SSH must be enabled on ESXi).

Let’s create a simple Ansible playbook vm_start.yml to power on a specific virtual machine in VMware vCenter. This playbook uses connection credentials from vmware_vars.yml.

- name: start vm 
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vmware_vars.yml
  tasks:
    - name: power on
      vmware_guest_powerstate:
        hostname: "{{ hostname }}"
  username: "{{ username }}"
        password: "{{ password }}"
        name: munfs01
        validate_certs: "{{ validate_certs }}"
        state: powered-on

Run the playbook:

$ ansible-playbook /etc/ansible/vmware/vm_start.yml

Example of ansible-playbook for VMware

Open the vSphere Client and verify that your VM has started successfully. vcenter client inerface

The next playbook allows you to enable the ntpd service on a specific ESXi host. Install the community.vmware module before using this Playbook:

$ ansible-galaxy collection install community.vmware

Now create a playbook (we’ve specified the connection parameters directly in the Playbook to make this easier to understand.):

---
- hosts: localhost
  vars:
    vcenter_hostname: "192.168.31.20"
    vcenter_user: "[email protected]"
    vcenter_password: "passwprd123"
    esxi_hostname: "192.168.31.50"
    esxi_username: "root"
    esxi_password: "es-122023"
  tasks: 
    - name: Start Service on  esxi host in vcenter
      community.vmware.vmware_host_service_manager:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_password }}"
        esxi_hostname: "{{ esxi_hostname }}"
        service_name: ntpd
        state: present #present #absent
        service_policy: off 
        validate_certs: no
      delegate_to: localhost

Run your playbook:

$ ansible-playbook /etc/ansible/vmware/vm_esxi_start_service.yml

Note that the free VMware Hypervisor settings cannot be changed from Ansible. The ESXi APIs are read-only in this version.

In this post, we have covered the basics of using Ansible to manage VMware ESXi, vCenter, and VMs in a vSphere environment.

In the previous article, we showed you how to use Ansible to manage Windows hosts.
0 comment
2
Facebook Twitter Google + Pinterest
LinuxVMware
previous post
How to Export MS Teams Chat History with PowerShell
next post
Send Telegram Messages from a PowerShell Script

Related Reading

How to Fix ‘An Operating System Wasn’t Found’...

August 24, 2023

How to Install Free VMware Hypervisor (ESXi)

March 17, 2024

Reset Root Password in VMware ESXi

October 6, 2023

Adding ESXi Host to VMware vCenter Server (vCSA)

March 12, 2024

Unmounting an NFS Datastore from VMware ESXi

March 12, 2024

How to Migrate (Import) VMs from VMware ESXi...

July 24, 2024

How to Increase Virtual Machine Disk Size in...

March 17, 2024

How to Create a Virtual Machine on VMWare...

March 12, 2024

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fixing ‘The Network Path Was Not Found’ 0x80070035 Error Code on Windows
  • Recovering Files from BitLocker Encrypted Drive
  • Installing an Open Source KMS Server (Vlmcsd) on Linux
  • How to Access VMFS Datastore from Linux, Windows, or ESXi
  • Install Any OS from ISO Image over Network with iVentoy
  • Using iPerf to Test Network Speed and Bandwidth
  • Moving WSL to Another Drive in Windows
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top