Home VMwareAutomation Ansible with VMware Cloud on AWS

Ansible with VMware Cloud on AWS

by Mohamed Imthiyaz
Ansible with VMware Cloud on AWS

I was playing on something with Ansible when I realized we at VMC don’t do much with Ansible. So let’s see how to deploy VMs on VMware Cloud on AWS with Ansible

Installing Python and Ansible

Assuming you are on Linux (Ubuntu) and don’t have Ansible installed

  • Switch to root, update and install common software
$ sudo -s
$ apt update
$ apt install -y software-properties-common
apt update
  • Add Ansible Repo and install Ansible 
$ apt-add-repository --yes --update ppa:ansible/ansible
$ apt install ansible

I have already installed Ansible on my Ubuntu Box, Reference of Ansible doc for the Same Install Ansible on Ubuntu

Install Ansible

Installing pyVmomi this is a Python SDK from VMware which allows you to manage ESXi and vCenter via VMware vSphere API.

Install Python if you don’t have it already

$ add-apt-repository universe
$ apt update && apt install -y python3
$ apt update && apt install python3-pyvmomi

We are all set to start writing Ansible Playbooks

Writing Ansible Playbook

Ansible Playbooks are in YAML format, we will create a basic VM deployment on your VMC vCenter which will use vmware_guest module to deploy VM.  

Create new file with .yml extension

nano deploy-vm.yml

Copy the below code, please change credentials accordingly

---
- hosts: localhost
  gather_facts: no
  vars:
    vcenter_server: "vcenter.xxxxxx.vmwarevmc.com"
    vcenter_user: "[email protected]"
    vcenter_pass: "PassWord"
    datacenter_name: "SDDC-Datacenter"
    cluster_name: "Cluster-1"
    datastore_name: "WorkloadDatastore"
  tasks:
  - name: Clone Windows template
    vmware_guest:
      hostname: "{{ vcenter_server }}"
      username: "{{ vcenter_user }}"
      password: "{{ vcenter_pass }}"
      validate_certs: False
      name: imhulp-Ansible
      template: Win2019-Template
      datacenter: "{{ datacenter_name }}"
      folder: /{{ datacenter_name }}/vm
      cluster: "{{ cluster_name }}"
      datastore: "{{ datastore_name }}"
      networks:
      - name: Imthi-seg
      state: poweredon
      wait_for_ip_address: no
    delegate_to: localhost

This playbook will create a new VM named imHulp-Ansible using the template on my vCenter Win2019-Template on WorkloadDatastore in Cluster-1. The VM will be powered on and the deployment is completed from Ansible side you can change the wait_for_ip_address: no to Yes but this will take little longer to complete.

Running the Playbook

Ansible script in Progress
Ansible deployed VM

You can also Customize your Template while deployment by using customization: add it right after the Networks.

customization:
   hostname: "imhulp"
   dns_servers:
   - 10.10.1.5
   - 8.8.8.8

This was simple playbook to deploy VMs on your VMC vCenter, you can play around with Ansible using more examples of vmware_guest_module from Ansible documentation

How to Connect and manage your VMC (VMware Cloud on AWS) via PowerCLI

You may also like

Leave a Comment