Ansible VMWare playbook examples

Posted by

Using Ansible can be a powerful way to maintain or deploy VMWare environments. Very easy to use and great for those who doesn’t like PowerShell scripting with PowerCli. Another advantage using the module is the ability to add resources in a workflow.

How to install the VMWare Ansible module?

The module is wrapped in the Python module: pyvmomi. Like other Python modules you can install these with pip.

NOTE: If you want to make use of the newest VMWare products like: NXS, VMWare cloud, AWS Console etc. You need to install the latest version of the vSphere Automation Python SDK. Installing this will also include pyvmomi. So I recommend to install using the oneliner below.

Read HERE how to install this module on AWX running on Kubernetes.

How to get started?

The connection is initially made from your Ansible server to the Vcenter appliance on port 443. So all playbooks will be running on the Ansible server. Because of this we need to specify: hosts: localhost, and connection local.

Never use plain text credentials in your playbooks. Use Ansible VAULT if you are running a simple Ansible deployment. Use the Credential store in AWX / Tower if you running Ansible on Kubernetes for example. The best solution is to use or create a credential plugin for your central credential store!

Use  “validate_certs: False” if your certificate management is not in place (like in 99% of all environments).
Some examples below.

Stop VM using Ansible

Using “when” If the timeout exceeded to force PowerOff the VM.

Start VM using Ansible

Set VM hardware version using Ansible

Change VM configuration file parameters (VMX) using Ansible

In this example we set the unique disk ID property.

Leave a Reply

Your email address will not be published. Required fields are marked *