ansible-collections / community.vmware

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

Unable to create a 2-disks vm on different datastores #353

Open saito-hideki opened 4 years ago

saito-hideki commented 4 years ago
SUMMARY

The summary of the original issue(ansible/ansible#26095) is below:

When you try to create a 2-disks vm with each disk on a different datastore, both are created on the datastore specified as the first one.

I have moved the original issue from ansible/ansible to ansible-collections/vmware along to the following suggestion in the original issue:

Original issue: ansible/ansible#26095 Suggestion: https://github.com/ansible/ansible/issues/26095#issuecomment-675273920

The following sections is a copy from the original issue.

ISSUE TYPE
COMPONENT NAME
ANSIBLE VERSION
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ansible/infra/library', u'/usr/lib/python2.7/site-packages/ara/plugins/modules']
  python version = 2.7.5 (default, Sep 15 2016, 22:37:39) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
CONFIGURATION

N.A.

OS / ENVIRONMENT

Red Hat Enterprise Linux 7.3 VMware Vsphere 6.0

STEPS TO REPRODUCE

Create a playbook with the below example and specify 2 different datastores as the datastore and the disk2_datastore variables.

- name: Create VM from template
  vmware_guest:
    name: "{{ inventory_hostname }}"

    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_user }}"
    password: "{{ vcenter_pass }}"
    validate_certs: False

    esxi_hostname: "{{ esxhost }}"
    datacenter: "{{ datacenter }}"

    template: '{{ os_family}}{{ os_vers }}{{os_arch}}-template'
    folder: "{{ folder | default('/vm') }}"

    hardware:
      num_cpus: "{{ vm_cpus }}"
      memory_mb: "{{ vm_memory * 1024 | default(1024) }}"

    disk:
      - size_gb: "{{ disk_size | default(10) }}"
        type: thin
        datastore: "{{ datastore }}"
        autoselect_datastore: False
      - size_gb: "{{ disk2_size | default(10) }}"
        type: thin
        datastore: "{{ disk2_datastore }}"
        autoselect_datastore: False

    networks:
      - name: "{{ vlan }}"
        ip: "{{ primary_ip }}"
        netmask: "{{ netmask }}"
        gateway: "{{ gateway }}"
      - name: "{{ nic2_vlan }}"
        ip: "{{ nic2_ip }}"
        netmask: "{{ nic2_netmask }}"

    customization:
      domain: "{{ domain }}"
      dns_servers: "{{ dns_servers }}"

    annotation: "Created with Ansible"
    wait_for_ip_address: true

  register: newvm
  delegate_to: localhost
EXPECTED RESULTS

The new vm is created with the each disk on the his own datastore

ACTUAL RESULTS

The new vm is created with the both disks on the same datastore

ansibullbot commented 4 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 4 years ago

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

jnm27 commented 4 years ago

I am hitting this issue as well. Ansible 2.11, pyvmomi 7.0, vmware 6.7.

jnm27 commented 4 years ago

I found this at line 2681 of vmware_guest:

        # which datastore?
        if expected_disk_spec.get('datastore'):
            # TODO: This is already handled by the relocation spec,
            # but it needs to eventually be handled for all the
            # other disks defined
            pass`
sdavid-fr commented 3 years ago

Same problem here. ansible 2.10.3, pyvmomi 7.0.1, vmware 6.5

I tried to workaround the problem by using vmware_guest_disk module to add supplementary disks one by one, but the module has the same bug (https://github.com/ansible-collections/community.vmware/issues/530)

I didn't find any solution to workaround this issue yet.

This bug is quite old : • https://github.com/ansible/ansible/issues/24887 (22/05/2017 vmware_guest not work with defined datastore) • https://github.com/ansible/ansible/issues/26095 (26/06/2017 Unable to create a 2-disks vm on different datastores) • https://github.com/ansible/ansible/issues/41227 (07/06/2018 vmware_guest module always create disks in a same datastore even configure another datastore) There were 2 attempts to fix this with no success : --> PR closed : https://github.com/ansible/ansible/pull/33230 --> PR closed : https://github.com/ansible/ansible/pull/50923https://github.com/ansible/ansible/issues/60286 (08/08/2019 vmware_guest- disks getting created on same datastore when cloning from template)

jnm27 commented 3 years ago

https://github.com/Akasurde/ansible/commit/7ad11a1f8bbf757226ed190fa0596ac76542f947 was made but never merged. Per comments in https://github.com/ansible/ansible/issues/26095, this improved the situation, in that it allows one disk per datastore. It will fail with multiple disks in a single datastore but that is okay for our use case since we use OS partitioning for those cases. I will try this out later.

jnm27 commented 3 years ago

I just tested it and this patch does work! I will not be bringing it through process here but plan to use it informally until it is officially merged.

JiffsMaverick commented 3 years ago

I have same problem. Ansible Project 2.10.7, pyVmomi 7.0.1, WMware 6.7.0.45000.

MrBones757 commented 3 years ago

We ran into this issue internally in our organization running ansible v2.9.19.

There seems to be two discrete issues here (at least from my perspective) that are as below: 1 - the use of the datastore parameter in disks is disregarded when creating new disks this includes:

We have been working on vmware_guest from this version of ansible and believe we have a fix. We want to do a little more testing internally, but I think we're nearly done..

To give a high level of the changes we've made:

Its looking good as far as my testing goes against vmware 6.7.x.

I will attempt to port these changes to the latest version of the module here and submit a PR, however i don't think ill be able to test it correctly as there seems to be another bug / incompatibility with the latest collection version and ansible 2.9.x where windows customization fails to run correctly, meaning i cant run it in my environment meaningfully. Will update this thread in the next few days.

Update: Found some issues where when cloning a template, the vm config files were created where the template was stored, updated this behavior to use the index 0 datastore, and moved some datastore logic into configure_disks. Validation going well so far. Also, if the user specified more than 7 disks, the disk with index 7 was always missed due to the scsi rule, have also fixed this i believe.

jnm27 commented 3 years ago

Is there any chance of this making it into an official build? I've been carrying around an unofficial patch for awhile now but I want to step up to the latest collection version...

jnm27 commented 2 years ago

@Akasurde?

jnm27 commented 2 years ago

We found a bug in @Akasurde's patch code. The directory creation in vmware_guest.py:create_directory_for_datadisk is not necessary and it actually causes duplicate directories to be created. It creates , and then VMware later creates _1 because already exists. And then is empty.

Commenting out the code like so:

       try:
            pass
            #self.content.fileManager.MakeDirectory(name=path_on_ds,
            #                                       datacenter=parent_dc,
            #                                       createParentDirectories=True)

fixes that issue and does not appear to regress anything.

jnm27 commented 2 years ago

Follow-up to my previous "fix". It appears to be version specific. That code should not be there for ESXi 6.7, but it may need to be there for ESXi 7.0.

jnm27 commented 2 years ago

Yet another fix... @Akasurde when will this be worked for real?

I also needed to apply a similar patch in the configure_multiple_controllers_disks function to handle the case where a new disk is being added to an already-existing VM.

                if len(disk_list) == 0 or not hard_disk_exist:
                    hard_disk = self.device_helper.create_hard_disk(disk_ctl_spec, disk_unit_number)
                    hard_disk.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create
                    disk_modified = self.set_disk_parameters(hard_disk, ctl['disk'][j])

                    # PATCH Also apply the multi-datastore directory naming fix to this multi-disk add/reconfigure situation
                    datastore_name = ctl['disk'][j]['datastore']
                    datastore = self.cache.find_obj(self.content, [vim.Datastore], datastore_name)
                    parent_dc = self.cache.get_parent_datacenter(datastore)
                    vmdk_file_name = self.create_directory_for_datadisk(datacenter=parent_dc, datastore=datastore, vm_name=vm_obj.name, diskspec=hard_disk)
                    hard_disk.device.backing.datastore = datastore
                    hard_disk.device.backing.fileName = vmdk_file_name
                    # END PATCH

                    self.configspec.deviceChange.append(hard_disk)
Tinashecj commented 2 years ago

Hi @jnm27, I am currently working on a fix for this too, as my company is invested in resolving this issue. I was wondering if you could either open a PR or share a branch in your fork so we could collaborate towards arriving at a permanent fix for this problem?

jnm27 commented 2 years ago

@Tinashecj this is the patch we have related to this ticket. It is mostly @Akasurde's code with additional small fixes on top (his original code plus my two recent comments) vmware_guest.py.patch.txt

jnm27 commented 2 years ago

I found another edge case.

I previously commented out this part of the patch

       try:
            pass
            #self.content.fileManager.MakeDirectory(name=path_on_ds,
            #                                       datacenter=parent_dc,
            #                                       createParentDirectories=True)

because it was creating extra _1 directories.

However, it appears that this IS needed when creating multiple disks through ESXi directly (NOT vCenter).

So I changed it to this:

            if not self.is_vcenter():
                self.content.fileManager.MakeDirectory(name=path_on_ds,
                                                       datacenter=parent_dc,
                                                       createParentDirectories=True)

Further care needs to be taken when working this to compare 6.7 ESXi and 7.0 ESXi, both through vcenter and through ESXi directly.

mak241265 commented 8 months ago

any news?? i have the same problem