Add new disks to VMware VM

Summary

I was asked by a user to add a new disk to a VMware VM. I immediatly thought, I have to automate this. Although there is an ansible module available in 2.8 that will do this, I am working in an environment that uses Ansible 2.7, so I had to achieve this task using the VMware API interface with the Ansible URI mdoule.

Requirements

  • An account with appropriate VMware access to add disks to a VM.
  • Enough storage to accommodate the disk being added to the VM.

Let do this..

The playbook requires a VM name and a dictionary containing pairs of disks and sizes to be added. I have made this flexible enough to allow a user to add 1 or more disks to a VM. At this point the disks are just being added to the SCSI controller 0, if you wish to extend it to be more flexible, knock yourself out :). What is doing on with the dictsort in the loop? I wanted a way to order the keys in the dictionary so that the disks would be added in the order they are listed.

Disk sizes are entered in GB. Ie 1 GB disk is entered simply as a 1.

main.yml

---

- name: create and add new disk(s) to vmware vm
  hosts: localhost
  connection: local
  vars:
    vm_name: vmhostname
    disk_list:
      disk1_test: 1
      disk2_test: 2
    vcentre: vcentre.hostname.com
    username: vcentre_user
    password: XXXX
    dc_name: vmware datacenter 
  tasks:
  - name: iterate over disk_list dict, run create_disk.yml on each
    include_tasks: create_disk.yml
    loop: "{{ disk_list|dictsort }}"
    loop_control:
      loop_var: disk
    

create_disk.yml

---

- name: check if VM exists
  vmware_guest_facts:
    hostname: "{{ vcentre }}"
    username: "{{ username }}"
    password: "{{ password }}"
    # only required for self signed certs
    validate_certs: no
    datacenter: "{{ dc_name }}" 
    name: "{{ vm_name }}"
  register: vm_facts

- name: get auth session token for API
  uri:
    url: "https://{{ vcentre }}/rest/com/vmware/cis/session"
    method: POST
    user: "{{ username }}"
    password: "{{ password }}"
    return_content: yes
    validate_certs: no
    force_basic_auth: yes
  register: auth_json

- name: create fact with session id
  set_fact:
    session_id: "{{ auth_json.cookies['vmware-api-session-id'] }}"

- name: get VM id
  uri:
    url: "https://{{ vcentre }}/rest/vcenter/vm?filter.names.1={{ vm_name }}"
    method: GET
    headers:
      vmware-api-session-id: "{{ session_id }}"
    return_content: yes
    validate_certs: no
  register: vm_json

- name: set vm_id fact
  set_fact:
    vm_id: "{{ vm_json.json.value[0]['vm'] }}"

- name: add disk to VM
  uri:
    url: "https://{{ vcentre }}/rest/vcenter/vm/{{ vm_id }}/hardware/disk"
    method: POST
    headers:
      vmware-api-session-id: "{{ session_id }}"
    return_content: yes
    validate_certs: no
    body:
      spec:
        type: "SCSI"
        scsi:
          bus: 0
        new_vmdk:
          name: "{{ disk.0 }}"
          capacity: "{{ disk.1 * 1073741824 }}"
  register: add_disk_json








One thought on “Add new disks to VMware VM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s