Creating vSphere VM’s using Ansible

Creating vSphere VM’s using Ansible

I am putting this out here in case anyone else may be interested in spinning up some VM’s using Ansible. I am doing this with Ansible 1.8.4 and connecting to vCenter 6.0 GA. I will be adding to this post as I add more functionality. Currently with Ansible 1.8.4 you cannot spin up VM’s using templates but that is added in 1.9 which is currently only available from GitHub in the dev branch. Not what I want to use for this right now as I like to use products that are release versions for stability and consistency. 🙂 What I will be doing in this post is spinning up 8 VM’s ( 3 CentOS DB and 5 Ubuntu64 Web). Once they are spun up you will need to install the OS on them (for now).

We first need to create an inventory list of the servers along with some variables to be passed to our Ansible playbook which will create the VM’s for us. You could easily run this from Jenkins if you want a webUI driven way to do this as well ( more on this later ).

Let’s create our inventory list which in this example I will be calling create_vms_hosts.

nano create_vms_hosts

Now copy and paste the following or create your own

[web-vms]
ans-web[01:05] disk='10' datastore='SSD-Pool_ELKStack_dev (NAS01)' network='vSS-OpenStack_Default' memory='256' cpucount='1' osid='ubuntu64Guest'

[db-vms]

ans-db[01:03] disk='20' datastore='SSD-Pool_ELKStack_dev (NAS01)' network='vSS-OpenStack_Default' memory='1024' cpucount='2' osid='rhel6_64Guest'

What we are specifying here is the group names (web-vms and db-vms) to specify different variables for each group of servers to build. I will breakdown each variable below to put some meaning to them.
ans-web[01:05] and ans-db[01:03] —- These are the actual VM names that we will be creating. Ansible has the ability to use regex type values to represent a group of items…in this case VM names…So Ansible will be creating ans-web01 – answeb05 and ans-db01 – ans-db03 for us.

disk= — is the size in GB to create for each VM
datastore= — name of the datastore to create the VMs on
network= — name of network (portgroup) to place the VMs on
memory= — the amount of memory in MB to allocate to the VMs
cpucount= — the number of vCPU’s to allocate to the VMs
osid= — the OS Identifier to specify for the OS type…. reference this guide for OSID’s

Now we need to create the actual ansible playbook to run to create the VMs for us. In this case I will be calling it create_vms.yml

nano create_vms.yml

Now copy and paste the below and modify to suit your requirements.

---
- hosts: all
  gather_facts: false
  connection: local
  user: remote
  sudo: true

  vars_prompt:
    - name: "vcenter_hostname"
      prompt: "Enter vcenter hostname"
      private: no
      default: "vcsa"
    - name: "vcenter_user"
      prompt: "Enter vcenter username"
      private: no
    - name: "vcenter_pass"
      prompt: "Enter vcenter password"
      private: yes

  vars:
    datacenter: 'everythingshouldbevirtual'
    esxi_host: 'esxi01.everythingshouldbevirtual.local'
    notes: 'Created by Ansible'

  tasks:
    - vsphere_guest:
        vcenter_hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        guest: "{{ inventory_hostname }}"
        state: present
        vm_extra_config:
          notes: "{{ notes }}"
        vm_disk:
          disk1:
            size_gb: "{{ disk }}"
            type: thin
            datastore: "{{ datastore }}"
        vm_nic:
          nic1:
            type: vmxnet3
            network: "{{ network }}"
            network_type: standard
        vm_hardware:
          memory_mb: "{{ memory }}"
          num_cpus: "{{ cpucount }}"
          osid: "{{ osid }}"
          scsi: paravirtual
        esxi:
          datacenter: "{{ datacenter }}"
          hostname: "{{ esxi_host }}"

This method will prompt for the vCenter hostname to connect to, username and password to connect to vCenter. You will want to modify the section under vars: to meet your specific requirements and naming of datacenter and esxi_host. The esxi_host is just a placeholder to initially create your VMs.

So with this all setup you will need to do one more thing before running this playbook and that is installing the python module for vSphere.

sudo pip install pysphere

Once this is installed you are now ready to run your playbook.

 ansible-playbook -i create_vms_hosts create_vms.yml

Screen Shot 2015-03-25 at 4.36.53 PMScreen Shot 2015-03-25 at 4.37.28 PMScreen Shot 2015-03-25 at 4.37.36 PM

And there you have it…You have now successfully created some VMs using Ansible 🙂

Updated!!! 01/13/2015

Here is a newer updated playbook to create VMs.

Enjoy!

27 thoughts on “Creating vSphere VM’s using Ansible

  1. Pingback: LdS S13-2015 | Anayrat.info

  2. Thanks for sharing this cool stuff. I have a question: I’m using ansible to create new guests (Windows Servers) from templates, the question is: How can I add the IP address and how can I join it to a Windows domain ? My DNS is infoblox and I use static IP’s.

    • hi,

      just came across this post when googling. have you managed to assign the IP to the guest VM from the ansible script?

      • @Sri – The only thing I have been able to get close on in regards to this would be to obtain an IP initially, discover it and then assign a new IP. Not the most elegant by any means. I am sure there is/will be a more elegant way.

        • Hello Larry,

          I am pretty much in the same situation. The guest VM has been provisioned using ansbile and a kickstart image. Now in need of setting it up with an IP. Any ways to do it with Ansible?

          Thanks,
          Jammy

  3. any insight on how to deploy to a host cluster instead of a specific host?
    My perspective is we allow vcenter to place the guest on the appropriate host based on DRS – basically a call to the host cluster – instead of the specific host.

  4. Hi,

    I am having issues deploying VM’s from vcenter or vsphere client on a windows machine. I do have ansible configured on a Linux(Ubuntu) machine. I get SSH errors trying to get the Linux(ansible) to ping windows(vcenter/vsphere client). I have also read you cannot communicate with Windows via ssh only with winrm. Please any help on this?

  5. Hi Larry,
    How did you get the IP and other networking information setup? Is it throught PXE boot/DHCP setup? Also do you have the ansible script that modifies the networking information on red hat linux servers?

  6. How to set meta to VM like we do for Openstack.
    for Openstack os_server module.
    meta no None A list of key value pairs that should be provided as a metadata to the new instance or a string containing a list of key-value pairs. Eg: meta: “key1=value1,key2=value2”

    Can we do same thing for vmware ?

    • @Pritam – That is a great question. I have not used the VMware modules for Ansible in quite some time as I have had issues with them since moving to vSphere 6.x but I am planning on jumping back into them very soon. So to answer your question, I am not sure if/how this can be done.

      • Thanks for Quick reply Larry,
        As per the current module, i am not finding any option to do that.. and this is a blocker for me. Please let me know if you could find any way to accomplish. I am using vSphere 5.x


  7. – name: create some vms
    hosts: localhost
    connection: local

    Hi,

    Here why we use connection local and hosts: localhost as remote host.

    what i thinking that my ansbile host ( where i deployed playbook ) is going to connect esxi host and create new vms by vsphere module.

  8. Hi, great article!

    Is it possible to ‘tag’ (set/modify an attribute) a VM in vsphere with ansible?

    I’ve only seen the option to set a ‘note’ on the VM.

    Thanks in advance!

  9. Is there anyway to specify a storage cluster vs. an actual datastore? I’ve tried several times with ansible 2.3 and vSphere 6.0.

      • Hi Larry,
        If you could circle back to do some updated testing with vsphere it would be really appreciated.
        We are moving to automation of our VM builds. We are also looking at modifying existing builds. The ansible doc’s are good but they do not delve deep enough into how to make changes and use all the different options with installing virtual machines. At present we are installing only RH servers.

  10. Dear Larry,
    Thanks a lot for a great manual! I have a question about your second scenario:
    how do (or where) you declare those variables:
    for example.
    vm_extra_config:
    vcpu.hotadd: “{{ item.vcpu_hotadd|default(omit) }}”
    mem.hotadd: “{{ item.mem_hotadd|default(omit) }}”
    notes: “{{ item.notes|default(omit) }}”
    folder: “{{ vcenter_folder }}”

    in first case – all clear: from inventory file, but i so understand second script.. ((

  11. Dear Larry,

    Thanks for good article,
    Please let me know ,whether this module is compatible with anisble 2.3 and how we can add iso in this module.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*