Search code examples
ansiblevmware

Ansible vmware_guest customization_spec doesn't change anything if a VM is already created


Background:

I'm working on some automation to be able to deploy a VM from a content library OVF template. I am able to successfully deploy the VM from the OVF template, however, when I try to apply a customization_spec template through the vmware_guest module, it doesn't change anything. The module runs, but just outputs OK instead of changed.

I found that this is also the case if I try to apply this customization_spec to a different VM that I cloned from a template through the vmware_guest module. I created a separate playbook that would solely do the customizations so there wouldn't be anything else to trip it up, and I'm working with static names and whatnot because this is some initial testing and development.

Here's the kicker though, if I clone a VM from a template using the same vmware_guest module, it will clone the VM and apply the customization_spec in the same process. The content library deployment is preferred due to the environment, but if I can't deploy a customization_spec after the fact, it kind of ruins it.

I can manually apply the VM Customization through vCenter, so I know the customization template is good, and as shown earlier, it does work when cloning a template through the vmware_guest module. I do not see it show up as a task in the vCenter, so I know it's not taking effect through the separate module. I should also note that I can use the same vmware_guest module to power on the VMs just fine, and if I combine the customization_spec line with it, it still doesn't initiate the guest customization within vCenter. Am I missing something in the module to force it to take effect?

Scrubbed code:

Cloning from a template - Customization works

- name: Clone the template with customizations - wait
  vmware_guest:
    hostname: {{ hostname }}
    username: {{ username }}
    password: {{ password }}
    validate_certs: False
    name: ansible_test_custom_yes_wait
    template: def_Win2019
    datacenter: {{ datacenter }}
    datastore: {{ datastore }}
    folder: {{ folder }}
    state: poweredon
    cluster: non-PRD v4
    customization_spec: customization
    wait_for_customization: yes

Cloning from content library ovf - No option for customization, but cloning works

- name: Clone template from content library
  community.vmware.vmware_content_deploy_ovf_template:
    hostname: {{ hostname }}
    username: {{ username }}
    password: {{ password }}
    content_library: {{ library }}
    ovf_template: def_Win2019
    datastore_cluster: {{ datastore_cluster }}
    datacenter: {{ datacenter }}
    folder: {{ folder }}
    name: ansible_test_content_library
    cluster: {{ cluster }}
    validate_certs: false

Setting VM customization - Does not work after VM is created

- name: Set VM customization
  vmware_guest:
    hostname: {{ hostname }}
    username: {{ username }}
    password: {{ password }}
    name: ansible_test_content_library
    state: present
    customization_spec: {{ customization }}
    wait_for_customization: yes

Solution

  • Ugh, I swear, every time I post a question here, I figure out the solution 5 minutes later...

    The problem is that since the VM already exists, you have to provide

    customization:
       existing_vm: True
    

    in the module. So, it should look like this:

    - name: Set VM customization
      vmware_guest:
        hostname: {{ hostname }}
        username: {{ username }}
        password: {{ password }}
        name: ansible_test_content_library
        state: present
        customization:
          existing_vm: True
        customization_spec: {{ customization }}
        wait_for_customization: yes
    

    Once that's in there, it'll run through the VM customization_spec. Otherwise, that existing_vm field defaults to False, and is only applied if the VM is being cloned.