A Shiny New Way to Manage VMware Guests

August 29, 2016 by Mark Phillips

Ansible

Way back in March of 2015, I wrote a post about managing VMware guests with the vsphere_guest module. A lot has changed since then with VMware support, including a whole bunch of new modules for managing the VMware infrastructure itself. We've also consolidated all VMware interaction around the pyvmomi Python library, replacing the aging and no longer maintained pysphere and psphere libraries. This support even extends to the VMware dynamic inventory, you will be pleased to know!

We took the opportunity to tidy up some of the parameters used in the old vsphere_guest module, and I think the new vmware_guest module is nicer to use. A couple of handy new additional parameters are validate_certs and wait_for_ip_address. I'm sure they don't need explaining, but for the sake of clarity they allow you to connect to vCentre servers that have a self signed SSL certificate, and for the module to wait for an IP address to become visible for the new VM.

This latter parameter is especially nice, because now you can have the single module wait for the IP address, instead of having to do something clunky with a block (as I did in my main demo).

So here is our updated play, using the new module:

  
  ---
  # vim: set ft=ansible et ts=2 sw=2:
  #
  # Create a new VM from a template using the new vmware_guest module
  - name: VM from template
    hosts: vmcreate
    gather_facts: false
    connection: local
    vars:
      vcenter_hostname: vcsa.box
      esxhost: esx1.box
      datastore: datastore1
      network: "VMnetwork"
      vmtemplate: CentOS7-Base
      vmcluster: LAN
      notes: Created by Ansible
      dumpfacts: False
    tasks:
      - name: Check for required variables
        fail: msg="Must pass name and group to -e"
        when: name is not defined or group is not defined

      - name: Check for vSphere access parameters
        fail: msg="Must set vcenter_user and vcenter_pass in a Vault"
        when: (vcenter_user is not defined) or (vcenter_pass is not defined)
        
      - name: Create VM from template
        vmware_guest:
          validate_certs: False
          hostname: "{{ vcenter_hostname }}"
          username: "{{ vcenter_user }}"
          password: "{{ vcenter_pass }}"
          esxi_hostname: "{{ esxhost }}"
          datacenter: BOX
          name: "{{ name }}"
          template: "{{ vmtemplate }}"
          disk:
            - size_gb: 10
              type: thin
              datastore: "{{ datastore }}"
          nic:
            - type: vmxnet3
              network: "{{ network }}"
          hardware:
            memory_mb: "{{ vm_memory | default(1024) }}"
          wait_for_ip_address: True
          state: present
        register: newvm
        
      - name: IP address info
        debug:
          msg: "{{ newvm.instance.ipv4 }} {{ name }}"

As with the original blog post example the most important bit is the 19 lines after vmware_guest, and again note the extensive use of variables to provide flexibility. With this one play you can run from the shell, or within Tower, and alter the way it behaves by simply changing a few variables. One of the things I like to do in Tower is set up a survey to add easy support for spinning up virtual machines with parameters I want to allow others to change (for example, the hostname of the resultant VM, or how much memory it can be specified with)


You can find the above code example in my usual demos GitHub repository.

The new vmware_guest module is currently in the devel branch of Ansible, and should be considered 'work in progress'. It largely works and it will be in the 2.2 release. We encourage you to check it out today though, and let us know how it works for you!

Share:

Topics:
VMware


 

Mark Phillips

Mark Phillips is a UK-based Product Manager. With almost a quarter of a century of industry experience, he has designed and engineered automated infrastructures at every level - from a handful of hosts in startups, to the tens of thousands in investment banks. You can follow him on twitter at @thismarkp.


rss-icon  RSS Feed