logo

How to use Ansible to orchestrate vSphere Environment

I’ve been doing alot of Linux stuff lately and have been playing with Ansible to create a hosts setup for my Kubernetes nodes.

Now, since i’m still at the ‘try-till-you-break-the-environment’ stage, using VMware snapshot has been very valuable to me. No need to reinstall or “oops moment” for me.

Originally, I’ve been doing snapshot manually from the WebClient but it’s getting repetetive so I wanted a better way of invoking it from a Linux OS.
and Yes – i know – its easy to do it in PowerCLI but I’m using a CentOS Dekstop and don’t want to open can of worms with powershell/ powerCLI for linux.

Since I’m already using Ansible to deploy my K8s cluster (kubespray FTW!), I decided to use the same tools to orchestrate my vSphere Environment.

Here’s a quick rundown of how to do it:

Pre-Requisites:

  • PyVmomi
    • Installed using pip install PyVmomi
  • Ansible
    • Installed either via the official repo or pip. i prefer the official repo

Reference:

https://docs.ansible.com/ansible/2.3/vmware_guest_snapshot_module.html

Here’s my Ansible Playbook on how to Create Snapshot:

# Snapshot VM
---
- hosts
: final
connection
: local
vars
:
datacenter
: 'DR-Sky'
vCenterName
: 'vcdr01.ldc.int'
username
: '[email protected]'
password
: 'VMware1!'

tasks
:
- debug
:
msg
: "Current System is {{ inventory_hostname }}"

- name
: Create snapshot
vmware_guest_snapshot
:
datacenter
: "{{ datacenter }}"
hostname
: "{{ vCenterName }}"
username
: "{{ username }}"
password
: "{{ password }}"
name
: "{{ inventory_hostname }}"
folder
: ''
state
: present
snapshot_name
: 'Snap by Ansible2'
description
: 'generated by ansible2'

Inventory File: (depends on which you want to orchestrate)

[final]
nodedr01.ldc.int
nodedr02.ldc.int
nodedr03.ldc.int

Execute Playbook:

ansible-playbook -i inventory vmware-Snap.yml

 

BONUS:

If you encounter issues about certificates (fist time running PyVmomi): run the following command: export PYTHONHTTPSVERIFY=0

 

ENJOY!

 

 

Leave a Reply

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.