ansible && vmware_guest_disk unit_number required

Hi;

I’m trying to automate the addition of disk devices to VM guests. After a bit of work and lots of lessons learned, I’ve gotten to this point:

`

  • name: add disk space
    vmware_guest_disk:
    hostname: “{{s}}”
    username: “{{vu}}”
    password: “{{vp}}”
    datacenter: “{{dc}}”
    validate_certs: no
    name: “{{inventory_hostname}}”
    disk:
  • size_gb: 20
    type: thick
    datastore: “{{dsi.datastores[0].datastore_cluster}}”
    scsi_controller: 0
    state: present
    delegate_to: localhost
    when:
  • dsi.datastores[0].datastore_cluster|length > 0
  • dsi.datastores[0].freeSpace / dsi.datastores[0].capacity * 100 > 20
    `

This results in:

TASK [add disk space] *************************************************************************************************** fatal: [cl1vinfspt1000 -> localhost]: FAILED! => {"changed": false, "msg": "Please specify 'unit_number' under disk parameter at index [0], which is required while creating disk."}

The number of disks on the vms which need disk additions is going to vary - in fact, some may even have multiple controllers. I’ve used govc to add disks to vms and it just tacks the disk on the end of the bus.

So, two questions:

  • Is there a way to tell vmware_guest_disk to use the next available unit number?
  • Failing that, is there a clever way to identify the next available scsi unit number? I’m even willing to use OS commands and create a variable… interesting thought, that. I’ll look there next.

Thanks for any hints/tips/suggestions.

Doug O’Leary

Hi Doug O’Leary,

I came up with this -

tasks:

  • name: Get all disks in all controllers
    vmware_guest_disk_info:
    validate_certs: False
    hostname: ‘{{ vcenter_hostname }}’
    username: ‘{{ vcenter_username }}’
    password: ‘{{ vcenter_password }}’
    datacenter: Asia-Datacenter1
    name: VM_8046
    register: existing_disk

  • name: Create a dict with controller and the total number of disks
    set_fact:
    disk_info: “{{ disk_info | default({}) | combine({ item.value.controller_bus_number : item.value.unit_number} )}}”
    with_dict: “{{ existing_disk.guest_disk_info }}”

  • debug:
    msg: “{{ disk_info }}”

Here, I am iterating over all disks and controllers in the given VM. I get a final count of all disks available on each controller in disk_info dictionary. Once I have that information I can figure out what is next disk in the given controller.

Also, you can take a look at this example to add more disk using vmware_guest_disk - https://github.com/Akasurde/ansible-reproducers/blob/master/55999/i55999.yml

Let me know if you need any help on this.

Thanks,
Abhijeet Kasurde

Wow; that’s going to take some time to unwrap. I haven’t done much with lookups yet. I have more than a few use cases for them so it’s probably about time.

I worked around the problem by using a script task to send a short script to the host which parses info under /sys/class::

`
sdir=‘/sys/class/scsi_generic/sg0/device/driver/’

cd ${sdir} || exit 2
f1=$(ls -d * | grep : | sort -V | head -1)
l1=$(ls -d ${f1%%:} | sort -V | tail -1)
n1=${l1%:}; n1=${n1##:}
echo $((n1+1))
`

That works and got me around the issue but definitely not the cleanest approach.

While I got you, sir, thank you for the efforts on the vmware modules. So far, without exception, they’ve worked like champs. They are helping me avoid logging into vsphere even more than the limited times I do now so that’s all to the good.

Thanks again.

Doug