ansible-collections / community.vmware

Ansible Collection for VMware
GNU General Public License v3.0
349 stars 336 forks source link

vmware_guest_disk Adding new disk to wrong datastore folder #501

Open hadfiiw opened 3 years ago

hadfiiw commented 3 years ago
SUMMARY

I have a playbook that spins out a new Ubuntu VM that is re-using the name of an existing VM (a stateless VM replacement playbook). When the playbook goes to add a second HDD to the new VM, it errors out saying the HDD already exists.

This is incorrect because it is looking in the original VMs datastore folder and not the new VMs datastore folder.

Before the new VM is spun out, the original VM is being renamed with an "-OldBackup" suffix to remove conflicts with the VM name (as they both end up in the same "VM and Templates" Folder, they cannot be named the same). This re-name is only for the VM name: the actual files that make up the original VM are on the datastore, in a folder, named the original VM name.

The playbook then spins out a new VM with the original VMs name. Because the original VM is still around in its original-name datastore folder, the system creates a new datastore folder of "VM-name_1". The "_1" at the end is added by the system (vSphere) as the folders cannot have duplicate names.

When the playbook gets to the vmware_guest_disk module to add a second HDD to the VM, it does not look in the datastore folder of "VM-name_1" but incorrectly looks at folder "VM-name".

The module is assuming that the VM it is adding the HDD to keeps its data in the datastore directory that matches its name. This is not always the case, especially when one gets into backup/restore scenarios. I have seen VMs living in folders with _1, _2, _3, etc, etc in the datastore folder name or with totally different names if the VM has been renamed in the past.

As a second test, I spun out the VM (which the system put in the "VM-name_1" folder) then, before the playbook got to the vmware_guest_disk module, I renamed the original VMs datastore folder from (for example) "VM-name" to "VM-name-OLD". The HDD add worked as it put the new HDD in datastore folder "VM-name". So I now have two datastore folders, "VM-name", "VM-name_1" for this single VM (with the original VM being in the "VM-name-OLD" folder).

NOTE: Before going to ansible 2.9 and Python 3, I used to add this second HDD via the "vmware_guest" module without a problem

ISSUE TYPE
COMPONENT NAME

vmware_guest_disk / disk

ANSIBLE VERSION
operations@had01-0-cds-a:~/Workspaces/homelab$ ansible --version
ansible 2.9.9
  config file = /home/operations/Workspaces/homelab/ansible.cfg
  configured module search path = ['/home/operations/Workspaces/homelab/library', '/home/operations/Workspaces/homelab/library/vmware', '/home/operations/Workspaces/homelab/library/windows', '/home/operations/Workspaces/nsxansible/library']
  ansible python module location = /home/operations/.local/lib/python3.6/site-packages/ansible
  executable location = /home/operations/.local/bin/ansible
  python version = 3.6.9 (default, Oct  8 2020, 12:12:24) [GCC 8.4.0]
CONFIGURATION
operations@had01-0-cds-a:~/Workspaces/homelab$ ansible-config dump --only-changed
DEFAULT_FORKS(/home/operations/Workspaces/homelab/ansible.cfg) = 20
DEFAULT_HOST_LIST(/home/operations/Workspaces/homelab/ansible.cfg) = ['/home/operations/Workspaces/homelab/inventory/hosts']
DEFAULT_MODULE_PATH(/home/operations/Workspaces/homelab/ansible.cfg) = ['/home/operations/Workspaces/homelab/library', '/home/operations/Workspaces/homelab
DEFAULT_ROLES_PATH(/home/operations/Workspaces/homelab/ansible.cfg) = ['/home/operations/Workspaces/homelab/roles', '/home/operations/Workspaces/nsxansible
HOST_KEY_CHECKING(/home/operations/Workspaces/homelab/ansible.cfg) = False
INTERPRETER_PYTHON(/home/operations/Workspaces/homelab/ansible.cfg) = auto
operations@had01-0-cds-a:~/Workspaces/homelab$ 
OS / ENVIRONMENT

It is a Ubuntu 20 VM running on vSphere 6.7.0.45000 (which I believe is 6.7U2)

STEPS TO REPRODUCE
  1. Have a VM up and running that has a second HDD created with the vmware_guest_disk module
  2. Rename the VM to something else (just rename the VM - not the datastore directory the VM is stored in)
  3. Spin up the same VM as step one. The new VM will be created but in a datastore directory of the VM_1
  4. When vmware_guest_disk goes to create the new HDD, it will fail with an error that the disk already exists (which is not true as the module is looking in the wrong folder)
- name: Add a second HDD
  connection: local
  vmware_guest_disk:
    name: "{{ server_name }}"
    datacenter: "{{ datacenter }}"
    folder: "{{ datacenter }}/vm/{{ vcenter_folder }}"
    hostname: "{{ deploy_vcenter }}"
    username: "{{ vcenter_username }}"
    password: "{{ hostvars[ deploy_vcenter ]['vcenter_passwd'] }}"
    validate_certs: "{{ validate_certs }}"
    disk:
    - size_gb: 30
      type: thin
      datastore: "{{ deploy_datastore }}"
      scsi_controller: 0
      unit_number: 0
      state: present
    - size_gb: 60
      type: thin
      datastore: "{{ deploy_datastore }}"
      scsi_controller: 0
      unit_number: 1
      state: present

In this case, the 60GB disk is what is throwing the error (the OVA I am creating the Ubuntu VM from already has the 30GB disk)

EXPECTED RESULTS

The new HDD should be placed in the same datastore directory as the VM it is trying to add the HDD to.

ACTUAL RESULTS

The play is trying to create the new HDD in the "had01-0-webproxy-a" directory but this new VM is actually running from the "had01-0-webproxy-a_1" directory. The module is looking into the wrong datastore directory (it looks like it is assuming the datastore directory is going to be the same as the VM name when that is, sometimes, not the case)

TASK [webproxy : Add a second HDD] ************************************************************************************************************************
fatal: [had01-0-webproxy-a]: FAILED! => {"changed": false, "msg": "Failed to manage disks for virtual machine 'had01-0-webproxy-a' with exception : ('Cannot complete the operation because the file or folder [Infra-VM-Synology-Work] had01-0-webproxy-a/had01-0-webproxy-a_1000_1.vmdk already exists', None)"}

It should be adding, [Infra-VM-Synology-Work] had01-0-webproxy-a_1/had01-0-webproxy-a_1000_1.vmdk (note the _1 in the folder name)

ansibullbot commented 3 years ago

Files identified in the description: None

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

Akasurde commented 3 years ago

!component =plugins/modules/vmware_guest_disk.py

ansibullbot commented 3 years ago

Files identified in the description:

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 3 years ago

cc @Tomorrow9 @goneri @lparkes @pgbidkar @warthog9 click here for bot help

hadfiiw commented 2 years ago

Any idea if this fix is being added? Is there a roadmap by chance? Thanks