Deploy vSphere VMs with Ansible!
Tags: ansible, fosslinux, homelab, networkautomation
Published: 2023-02-04 18:23:58

---
title: Deploy vSphere VMs with Ansible!
published: true
date: 2023-02-04 18:10:00 UTC
tags: Ansible,FOSSLinux,HomeLab,NetworkAutomation
canonical_url: https://blog.engyak.co/2023/02/deploy-vsphere-vms-with-ansible.html
---

In a previous post, we covered how to [create a virtual machine from a VM template in vSphere using Python and the REST API](https://blog.engyak.co/2023/01/why-automate-vm-deployment-with.html) as an example of service-agnostic methods to invoke infrastructure resources.

[VMware's PowerCLI](https://developer.vmware.com/docs/15315/powercli-user-s-guide) is a fantastic tool, but it's not for me. Tweaking or porting functionality from PowerCLI to other languages beyond Windows and PowerShell isn't supported. Technology professionals with more than a few years of experience are leery of code portability issues; standard hardware architectures today are the results of consolidation efforts and caused some quite painful transition points. Change safety features like idempotency or context awareness are important to me; I prefer the approach of "check parameters, check destructiveness, execute, test."

Engineers develop linguistic preferences as part of a normal progression throughout their careers. Bash/Zsh/Python/Perl and JSON/YAML/XML appear more intuitive to me as universal formats to store artifacts and execution code. BASIC-style languages like PowerShell/CLI are not for everybody.

It's possible to use Ansible to manage and deploy Virtual Machines now - the previously covered Python code leverages the same RESTful API as the Ansible modules. The Ansible modules shift responsibility for code maintenance away from internal teams. Let's not forget the cost of writing and maintaining custom code:

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirrCQWX_d908r8mLWCjyrGpUBZlSLJrnEmc1AyhmwAlqVNje5_0byjINE-uQm0l9-4d9EZeD_ybwAidkZ6QWtMRNiyCSEgnU_2MmgVRqIGGuRSDO3rS6sMf4MzH-moTjD_Mo9avBJ_Q2fSdat4zwhF68xnmzsJmz5p0FyQY6Ns_ajurQ8FXPNm8J3j/w640-h104/automation-value-prop.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirrCQWX_d908r8mLWCjyrGpUBZlSLJrnEmc1AyhmwAlqVNje5_0byjINE-uQm0l9-4d9EZeD_ybwAidkZ6QWtMRNiyCSEgnU_2MmgVRqIGGuRSDO3rS6sMf4MzH-moTjD_Mo9avBJ_Q2fSdat4zwhF68xnmzsJmz5p0FyQY6Ns_ajurQ8FXPNm8J3j/s671/automation-value-prop.png)

Let's take a look. We need to install the Ansible "Collection" to leverage the REST API (this does require the Python package `aiohttp`:

```
ansible-galaxy collection install vmware.vmware_restStarting galaxy collection install processProcess install dependency mapStarting collection install processInstalling 'vmware.vmware_rest:2.2.0' to '/root/.ansible/collections/ansible_collections/vmware/vmware_rest'Downloading https://galaxy.ansible.com/download/vmware-vmware_rest-2.2.0.tar.gz to /root/.ansible/tmp/ansible-local-346454pqg9aobo/tmpi1o29dhovmware.vmware_rest (2.2.0) was installed successfullyInstalling 'cloud.common:2.1.2' to '/root/.ansible/collections/ansible_collections/cloud/common'Downloading https://galaxy.ansible.com/download/cloud-common-2.1.2.tar.gz to /root/.ansible/tmp/ansible-local-346454pqg9aobo/tmpi1o29dhocloud.common (2.1.2) was installed successfully
```

First, let's plan **where** automation will deploy workloads. Good work starts with good data - Ansible combines with a popular template tool (Jinja), enabling automation engineers to compile "pretty" reports from variables we collect during the execution phase:

<script src="https://gist.github.com/ngschmidt/9095a8e5c3c6ab43b6a416026ef3f62c.js"></script>

The playbook will help any planning efforts with consistent infrastructure. At work, I'd recommend rendering an HTML page with these artifacts (Jinja can do that as well) periodically with a CI tool and publishing it to a web server for other teams to reference.

Let's use that information to deploy a virtual machine:

<script src="https://gist.github.com/ngschmidt/9dfbfbeb998dba751192a7c28197dbaa.js"></script>

**Correctly setting the `session_timeout` parameter in the playbook is critical.** VM Deployment times depend on storage backing; the default timeout is **300 seconds**. Deployment timeouts require adjustment to the equipment they live on - I set it to **1200 seconds** to accommodate spinning disks.

This playbook is parameterized, with variables identified by double curly brackets,  `{{ variable_name }}`. Ansible Playbook examples don't always provide _what output the code will expect,_ a consistent issue with community-generated code.

Ansible supports variable injection from an external file or the command line; the best way to manage these input variables for self-service is to use file-based inputs. CI tools ship with APIs that allow you to invoke a pipeline with a JSON payload, and this method gives you an easy way to convert back and forth. I have provided an example JSON document to show what format the playbook wants.

To invoke extra variables in Ansible, use the _ext-vars_ feature:

```
ansible-playbook build_vm.yml "@parameters.json"
```

Jenkins has a parameter injection feature that makes this consumable as an end user as well:

[![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjba3LKohbsTPh5hcf64Ya7daYCl6bbcwfBQS0CeVr19_KVrok7Ky45xKBZzkI1onHroeH7C2YJl3ZslKw_0p6YCjOwh-Id-0_vhkhSQj5yWKKx1A1UD7w3Y1zBcLpENrwpSd04ac4bspProVMTh6VXWh7FWvQfybQF0hiF1z3CQuN_o5OeUX0CRpnR/w400-h190/2023-02-04%2008_17_45-vm-build%20Config%20%5BJenkins%5D%20%E2%80%94%20Mozilla%20Firefox.png)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjba3LKohbsTPh5hcf64Ya7daYCl6bbcwfBQS0CeVr19_KVrok7Ky45xKBZzkI1onHroeH7C2YJl3ZslKw_0p6YCjOwh-Id-0_vhkhSQj5yWKKx1A1UD7w3Y1zBcLpENrwpSd04ac4bspProVMTh6VXWh7FWvQfybQF0hiF1z3CQuN_o5OeUX0CRpnR/s403/2023-02-04%2008_17_45-vm-build%20Config%20%5BJenkins%5D%20%E2%80%94%20Mozilla%20Firefox.png)

Consumers perceive infrastructure's quality with a heavy bias towards convenience. We should construct easy ways to provision resources and enable creativity to boost positive perception of infrastructure services. Ansible's `vmware_rest` module combines with an execution engine like Jenkins to create an open method for deploying Virtual Machines, a pivotal step towards making infrastructure appealing.