ansible-collections / community.vmware

Ansible Collection for VMware
GNU General Public License v3.0
348 stars 335 forks source link

vCenter Port Groups with a '%' in the name result in an error #1361

Open Griamce0385 opened 2 years ago

Griamce0385 commented 2 years ago
SUMMARY

When passing a network port group via a variable in module vmware_guest with a '%' character the module crashes

ISSUE TYPE
COMPONENT NAME

vmware_guest module at line 1871

ANSIBLE VERSION
ansible 2.10.11
COLLECTION VERSION
# /home/jschabe1/Ansible-2/collections/ansible_collections
Collection       Version
---------------- -------
community.vmware 2.6.0  

# /usr/local/lib/python3.6/site-packages/ansible_collections
Collection       Version
---------------- -------
community.vmware 1.7.0  
CONFIGURATION
OS / ENVIRONMENT
STEPS TO REPRODUCE
- name: Configure PVS VM NIC
      community.vmware.vmware_guest:
        # As of 2022-06-13 this will always show 'changed' due to a bug in the module, even though it is not really changing
        # https://github.com/ansible-collections/community.vmware/issues/498
        hostname: "{{ vcenter }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_password }}"
        validate_certs: "{{ vmware_validate_certs }}"
        datacenter: "{{ vcenter_datacenter }}"
        cluster: "{{ vcenter_cluster }}"
        folder: "{{ vcenter_vm_folder }}"
        name: "{{ computername }}"
        networks:
          - name: '19.18.17.0%2f24_VLAN999'
            device_type: vmxnet3
            connected: true
            start_connected: true
        force: true
        wait_for_ip_address: false
      delegate_to: localhost
      tags:
        - configure_nic
EXPECTED RESULTS

I expect no errors and have the vm configured then go into the next play

ACTUAL RESULTS

VM is configured with proper port group, but there is an error and subsquent plays dont execute because this one fails

The full traceback is:
Traceback (most recent call last):
  File "/home/jschabe1/.ansible/tmp/ansible-tmp-1655858572.8963387-336989-272444255532823/AnsiballZ_vmware_guest.py", line 102, in <module>
    _ansiballz_main()
  File "/home/jschabe1/.ansible/tmp/ansible-tmp-1655858572.8963387-336989-272444255532823/AnsiballZ_vmware_guest.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/jschabe1/.ansible/tmp/ansible-tmp-1655858572.8963387-336989-272444255532823/AnsiballZ_vmware_guest.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.vmware.plugins.modules.vmware_guest', init_globals=None, run_name='__main__', alter_sys=True)
  File "/usr/lib64/python3.6/runpy.py", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_community.vmware.vmware_guest_payload_9aadrc95/ansible_community.vmware.vmware_guest_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest.py", line 3460, in <module>
  File "/tmp/ansible_community.vmware.vmware_guest_payload_9aadrc95/ansible_community.vmware.vmware_guest_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest.py", line 3408, in main
  File "/tmp/ansible_community.vmware.vmware_guest_payload_9aadrc95/ansible_community.vmware.vmware_guest_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest.py", line 3049, in reconfigure_vm
  File "/tmp/ansible_community.vmware.vmware_guest_payload_9aadrc95/ansible_community.vmware.vmware_guest_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest.py", line 1871, in configure_network
AttributeError: 'NoneType' object has no attribute 'config'
fatal: [stl-avcp1.smrcy.com]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/home/jschabe1/.ansible/tmp/ansible-tmp-1655858572.8963387-336989-272444255532823/AnsiballZ_vmware_guest.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/home/jschabe1/.ansible/tmp/ansible-tmp-1655858572.8963387-336989-272444255532823/AnsiballZ_vmware_guest.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/jschabe1/.ansible/tmp/ansible-tmp-1655858572.8963387-336989-272444255532823/AnsiballZ_vmware_guest.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.vmware.plugins.modules.vmware_guest', init_globals=None, run_name='__main__', alter_sys=True)\n  File \"/usr/lib64/python3.6/runpy.py\", line 205, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib64/python3.6/runpy.py\", line 96, in _run_module_code\n    mod_name, mod_spec, pkg_name, script_name)\n  File \"/usr/lib64/python3.6/runpy.py\", line 85, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_community.vmware.vmware_guest_payload_9aadrc95/ansible_community.vmware.vmware_guest_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest.py\", line 3460, in <module>\n  File \"/tmp/ansible_community.vmware.vmware_guest_payload_9aadrc95/ansible_community.vmware.vmware_guest_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest.py\", line 3408, in main\n  File \"/tmp/ansible_community.vmware.vmware_guest_payload_9aadrc95/ansible_community.vmware.vmware_guest_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest.py\", line 3049, in reconfigure_vm\n  File \"/tmp/ansible_community.vmware.vmware_guest_payload_9aadrc95/ansible_community.vmware.vmware_guest_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest.py\", line 1871, in configure_network\nAttributeError: 'NoneType' object has no attribute 'config'\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}
MallocArray commented 2 years ago

Also occurs with Ansible 2.12

sky-joker commented 2 years ago

Thanks, @Griamce0385 for reporting the issue!

I tried setting the distributed port group name includes '%' to the network name to create a new vm, but I didn't occur the same error in my environment.
The below playbook is that I created.

---
- name: test
  hosts: localhost
  gather_facts: false
  tasks:
    - name: test
      community.vmware.vmware_guest:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: false
        datacenter: "{{ datacenter }}"
        cluster: "{{ cluster }}"
        folder: "{{ folder }}"
        guest_id: centos7_64Guest
        name: "{{ vm_name }}"
        hardware:
          num_cpus: 1
          memory_mb: 2048
        networks:
          - name: '10.0.0.0%2f24'
            device_type: vmxnet3
            connected: true
            start_connected: true
        datastore: "{{ datastore }}"
        force: true

In my case, I got the below error.

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Network '10.0.0.0%2f24' does not exist."}

BTW, the below playbook works well

---
- name: test
  hosts: localhost
  gather_facts: false
  tasks:
    - name: test
      community.vmware.vmware_guest:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: false
        datacenter: "{{ datacenter }}"
        cluster: "{{ cluster }}"
        folder: "{{ folder }}"
        guest_id: centos7_64Guest
        name: "{{ vm_name }}"
        hardware:
          num_cpus: 1
          memory_mb: 2048
        networks:
          - name: '10.0.0.0/24'
            device_type: vmxnet3
            connected: true
            start_connected: true
        datastore: "{{ datastore }}"
        force: true

My Environment Information

Please let me know several things.

Griamce0385 commented 2 years ago

vCenter Version: 7.0.3.00600 It is the name of the distributed port group.

Also I tested on Ansible version 2.12.6 this morning yielding similar results.

sky-joker commented 2 years ago

Thanks for providing the information.
I'm not sure, the cause of the error might have something to do with VCSA version.
I don't have VCSA 7.0.3 now, but I'll deploy it and try again.

mariolenz commented 2 years ago

@Griamce0385 @MallocArray Does this happen when you create a new or when you reconfigure an existing VM? It sounds like @sky-joker only tested creating a new VM. If you run into this issue when reconfiguring one, it might be a (slightly) different code path that we have to look at.

BTW: Is your network name really 19.18.17.0%2f24_VLAN999? This looks a bit like you're trying to encode / with %2f. If I'm right, did you test plain 19.18.17.0/24_VLAN999?

Griamce0385 commented 2 years ago

@mariolenz Yes, unfortunately a lot of our network names have "%2f" in the name as many of them are quite old and were created with old vCenter thick client using a "/". When we migrated to the H5 client names created with a "/" switched to "%2f" . We have newer networks with "/" in the name and those do work. The valid vCenter name for the port group is in fact "19.18.17.0%2f24_VLAN999" and that is what we have to pass to the module.

The right answer is to probably renamed the networks to something not containing a %. I would assume though there's a way to escape the "%" in the name, however trying every way and combination of escape charters I can think of, has yield either this error or an invalid network name.

Griamce0385 commented 2 years ago
10.0.0.0%2f24

Was your port group in vCenter named "10.0.0.0%2f24" or was it named "10.0.0.0/24"? In our case we have port groups using "%2f24" in the name.

sky-joker commented 2 years ago

I will give a supplementary explanation. Slash is displayed that is URL encoded in a VM summary of VCSA UI. So ‘/‘ is displayed as a ‘%2f’.

'%2f' decodes to ‘/‘ in the module, so please specify '/', not '%2f'.

sky-joker commented 2 years ago

FYI

Display in Network

image

Display in VM Summary

image

n3pjk commented 2 years ago

@Griamce0385 We encountered this problem in our environment. The port group is named with a '/', but it is url encoded to '%2f' internally in vCenter. When specifying the network, we needed to use the '%2f' and not the '/'. You need to make sure that the name, with the '%2f', is not being further url encoded before supplying it to the ansible module. In your example, this appears not to be the case.

What works for us is storing the name in a variable, specifically, we use a dict. You need to make sure that the '%' is not being interpreted as anything other than a character in the name. If you wish to avoid the issue all together, you must not use any special characters in the original name that url encode to anything requiring the '%' escape character.

We are currently running 6.7 without issue, so there may be a problem with 7.0's interpretation.

n0ve1 commented 1 year ago

I will give a supplementary explanation. Slash is displayed that is URL encoded in a VM summary of VCSA UI. So ‘/‘ is displayed as a ‘%2f’.

'%2f' decodes to ‘/‘ in the module, so please specify '/', not '%2f'.

@sky-joker This works (playbook executed without errors), but Network not connecting to VM. :(

n3pjk commented 1 year ago

This is not a problem with the module, but with VMware. You need to do something like "{{ request.network | urlencode() | regex_replace('/', '%2f') }}". Where request.network is the name of the port group with the literal '/'. The problem is that VMware displays the '/', but requires the name to be encoded. You can actually see this behavior in vCenter itself.

Sent from my Verizon, Samsung Galaxy smartphone Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: n0ve1 @.> Sent: Tuesday, November 29, 2022 10:03:26 PM To: ansible-collections/community.vmware @.> Cc: Paul Knight @.>; Comment @.> Subject: Re: [ansible-collections/community.vmware] vCenter Port Groups with a '%' in the name result in an error (Issue #1361)

I will give a supplementary explanation. Slash is displayed that is URL encoded in a VM summary of VCSA UI. So ‘/‘ is displayed as a ‘%2f’.

'%2f' decodes to ‘/‘ in the module, so please specify '/', not '%2f'.

@sky-jokerhttps://github.com/sky-joker This works (playbook executed without errors), but Network not connecting to VM. :(

— Reply to this email directly, view it on GitHubhttps://github.com/ansible-collections/community.vmware/issues/1361#issuecomment-1331585042, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AA2LV63C7IPBSQZGRPLSS3DWK27X5ANCNFSM5ZOL52DQ. You are receiving this because you commented.Message ID: @.***>

n0ve1 commented 1 year ago

@n3pjk, I tried different ways of escaping/encoding, but then the error is that the network is not found (deploing a virtual machine from a template).