vmware / ansible-for-nsxt

Ansible delivers simple IT automation that ends repetitive tasks and frees up DevOps teams for more strategic work. This project is to enable this automation for NSX-T installation.
Other
171 stars 145 forks source link

Creating segments from a dict fails if i use portgroup_name and its value has spaces #363

Open ghost opened 3 years ago

ghost commented 3 years ago

Seems that if i use portgroup_name as display_name and its name has spaces or special chars it will fail. NSX-T accepts Segments with spaces, should the ansible module not allow it also?

My data sample looks like this:

TASK [list port_groups] *** task path: /root/scripts/nsx-t/script4/nsxt-automation/1.yaml:25 ok: [127.0.0.1] => { "port_groups": [ { "portgroup_name": "Test - 192.168.20.0_24 - VMotion - 1", "vlan_id": "20" }, { "portgroup_name": "Test - 192.168.60.0_24 - Management", "vlan_id": "0" }, { "portgroup_name": "Test - 192.168.30.0_24 - VSAN", "vlan_id": "25" }, { "portgroup_name": "DVS-Dev-Ork2-DVUplinks-11", "vlan_id": [ "0-4094" ] } ] }

This fails:

This works:

Error: TASK [Create Segments] **** task path: /root/scripts/nsx-t/script4/nsxt-automation/1.yaml:38

ESTABLISH LOCAL CONNECTION FOR USER: root EXEC /bin/sh -c 'echo ~root && sleep 0' EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905 `" && echo ansible-tmp-1621604976.9795952-26415-266025818716905="` echo /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905 `" ) && sleep 0' Using module file /root/scripts/nsx-t/script4/nsxt-automation/library/nsxt_policy_segment.py PUT /root/.ansible/tmp/ansible-local-261381voivn84/tmpcmt7bpon TO /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/AnsiballZ_nsxt_policy_segment.py EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/ /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/AnsiballZ_nsxt_policy_segment.py && sleep 0' EXEC /bin/sh -c '/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/AnsiballZ_nsxt_policy_segment.py && sleep 0' EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1621604976.9795952-26415-266025818716905/ > /dev/null 2>&1 && sleep 0' The full traceback is: File "/tmp/ansible_nsxt_policy_segment_payload_5_a4rdcu/ansible_nsxt_policy_segment_payload.zip/ansible/module_utils/nsxt_base_resource.py", line 713, in _send_request_to_API ignore_errors=ignore_error, method=method, data=data) File "/tmp/ansible_nsxt_policy_segment_payload_5_a4rdcu/ansible_nsxt_policy_segment_payload.zip/ansible/module_utils/policy_communicator.py", line 143, in request ca_path=self.ca_path) File "/tmp/ansible_nsxt_policy_segment_payload_5_a4rdcu/ansible_nsxt_policy_segment_payload.zip/ansible/module_utils/urls.py", line 1399, in open_url unredirected_headers=unredirected_headers) File "/tmp/ansible_nsxt_policy_segment_payload_5_a4rdcu/ansible_nsxt_policy_segment_payload.zip/ansible/module_utils/urls.py", line 1304, in open return urllib_request.urlopen(request, None, timeout) File "/usr/lib64/python3.6/urllib/request.py", line 223, in urlopen return opener.open(url, data, timeout) File "/usr/lib64/python3.6/urllib/request.py", line 526, in open response = self._open(req, data) File "/usr/lib64/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/usr/lib64/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args) File "/tmp/ansible_nsxt_policy_segment_payload_5_a4rdcu/ansible_nsxt_policy_segment_payload.zip/ansible/module_utils/urls.py", line 483, in https_open return self.do_open(self._build_https_connection, req) File "/usr/lib64/python3.6/urllib/request.py", line 1349, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/usr/lib64/python3.6/http/client.py", line 1254, in request self._send_request(method, url, body, headers, encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1265, in _send_request self.putrequest(method, url, **skips) File "/usr/lib64/python3.6/http/client.py", line 1127, in putrequest raise InvalidURL(f"URL can't contain control characters. {url!r} " failed: [127.0.0.1] (item={'portgroup_name': 'Test - 192.168.20.0_24 - VMotion - 1', 'vlan_id': '20'}) => { "ansible_loop_var": "item", "changed": false, "invocation": { "module_args": { "achieve_subresource_state_if_del_parent": false, "address_bindings": null, "admin_state": "UP", "advanced_config": null, "bridge_profiles": null, "ca_path": null, "connectivity_path": null, "create_or_update_subresource_first": false, "delete_subresource_first": true, "description": null, "dhcp_config_path": null, "do_wait_till_create": false, "domain_name": null, "enforcementpoint_id": null, "extra_configs": null, "hostname": "nsx-dev-Ork2.local", "id": null, "l2_extension": null, "mac_pool_id": null, "metadata_proxy_paths": null, "nsx_cert_path": null, "nsx_key_path": null, "overlay_id": null, "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "port": 443, "replication_mode": "MTEP", "request_headers": null, "segment_ports": null, "site_id": null, "state": "present", "subnets": null, "tags": null, "tier0_display_name": null, "tier0_id": null, "tier1_display_name": null, "tier1_id": null, "transport_zone_display_name": "Ork2-vlan-transport-zone", "transport_zone_id": null, "username": "admin", "validate_certs": false, "vlan_ids": [ "20" ] } }, "item": { "portgroup_name": "Test - 192.168.20.0_24 - VMotion - 1", "vlan_id": "20" }, "msg": "Received URL can't contain control characters. '/policy/api/v1/infra/segments/SEG-Test - 192.168.20.0_24 - VMotion - 1' (found at least ' ') from NSX Manager. Please try again. " }
ghost commented 3 years ago

So it turns out the issue is happening due to spaces in the portgroup name. ie. "portgroup_name": "Test - 192.168.20.0_24 - VMotion - 1",

Any ideas on how to correct that at module level or how can i escape the values with spaces?