# ansible && vmware\_guest\_disk unit\_number required

**URL:** <https://forum.ansible.com/t/ansible-vmware-guest-disk-unit-number-required/31799>\
**Category:** Ansible Project\
**Created:** [December 10, 2019, 1:58pm UTC](https://forum.ansible.com/t/ansible-vmware-guest-disk-unit-number-required/31799 "2019-12-10T13:58:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [December 10, 2019, 1:58pm UTC](https://forum.ansible.com/t/ansible-vmware-guest-disk-unit-number-required/31799/1 "2019-12-10T13:58:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [December 10, 2019, 5:14pm UTC](https://forum.ansible.com/t/ansible-vmware-guest-disk-unit-number-required/31799/2 "2019-12-10T17:14:28Z")

</div>

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](https://github.com/Akasurde/ansible-reproducers/blob/master/55999/i55999.yml)

Let me know if you need any help on this.

Thanks,  
Abhijeet Kasurde

---

<div class="post-metadata">

**Author:** ![Doug\_OLeary](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Doug\_OLeary](https://forum.ansible.com/u/Doug_OLeary)\
**Post date:** [December 10, 2019, 6:18pm UTC](https://forum.ansible.com/t/ansible-vmware-guest-disk-unit-number-required/31799/3 "2019-12-10T18:18:39Z")

</div>

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
