Add new disks to VMware VM


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.


  • 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.



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



- name: check if VM exists
    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
    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
    session_id: "{{ auth_json.cookies['vmware-api-session-id'] }}"

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

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

- name: add disk to VM
    url: "https://{{ vcentre }}/rest/vcenter/vm/{{ vm_id }}/hardware/disk"
    method: POST
      vmware-api-session-id: "{{ session_id }}"
    return_content: yes
    validate_certs: no
        type: "SCSI"
          bus: 0
          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: Logo

You are commenting using your 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