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.
Contents
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.
1 |
Pip install pyvmomi |
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.
1 |
pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
- name: VMWare tasks hosts: localhost connection: local gather_facts: no vars: vcenter_ip: 'VCENTERIP' vcenter_user: 'VCENTERUSER' vcenter_password: 'VCENTERPASS' vmname: 'VMNAME' tasks: - name: Wait for the virtual machine to shutdown vmware_guest_powerstate: hostname: "{{ vcenter_ip }}" username: "{{ vcenter_user }}" password: "{{ vcenter_password }}" name: "{{ vmname }}" validate_certs: False state: shutdown-guest state_change_timeout: 900 register: wait ignore_errors: yes - name: ForcePoweroff vmware_guest_powerstate: hostname: "{{ vcenter_ip }}" username: "{{ vcenter_user }}" password: "{{ vcenter_password }}" name: "{{ vmname }}" validate_certs: False state: powered-off register: poweroff when: wait.msg is search('Timeout') |
Start VM using Ansible
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- name: VMWare tasks hosts: localhost connection: local gather_facts: no vars: vcenter_ip: 'VCENTERIP' vcenter_user: 'VCENTERUSER' vcenter_password: 'VCENTERPASS' vmname: "VMNAME" tasks: - name: ForcePowerOn vmware_guest_powerstate: hostname: "{{ vcenter_ip }}" username: "{{ vcenter_user }}" password: "{{ vcenter_password }}" name: "{{ vmname }}" validate_certs: False state: powered-on |
Set VM hardware version using Ansible
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
- name: VMWare tasks hosts: localhost connection: local gather_facts: no vars: vcenter_ip: 'VCENTERIP' vcenter_user: 'VCENTERUSER' vcenter_password: 'VCENTERPASS' vmname: "VMNAME" tasks: - name: Set hardware version vmware_guest: hostname: "{{ vcenter_ip }}" username: "{{ vcenter_user }}" password: "{{ vcenter_password }}" name: "{{ vmname }}" validate_certs: False hardware: version: 13 register: changeid - debug: var: changeid |
Change VM configuration file parameters (VMX) using Ansible
In this example we set the unique disk ID property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
- name: VMWare tasks hosts: localhost connection: local gather_facts: no vars: vcenter_ip: 'VCENTERIP' vcenter_user: 'VCENTERUSER' vcenter_password: 'VCENTERPASS' vmname: "VMNAME" tasks: - name: Set disk UUID vmware_guest: hostname: "{{ vcenter_ip }}" username: "{{ vcenter_user }}" password: "{{ vcenter_password }}" name: "{{ vmname }}" validate_certs: False customvalues: - key: "disk.EnableUUID" value: "true" register: changeid - debug: var: changeid |