ansible-for-nsxt icon indicating copy to clipboard operation
ansible-for-nsxt copied to clipboard

Certificate Creation Failure

Open zhaq90 opened this issue 3 years ago • 1 comments

Describe the bug

I am using the module nsxt_certificates.py and when i try to create the certificate, i get an error `The full traceback is: Traceback (most recent call last): File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/modules/nsxt_certificates.py", line 197, in main File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/modules/nsxt_certificates.py", line 127, in update_params_with_pem_encoding File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/module_utils/vmware_nsxt.py", line 111, in get_private_key_string FileNotFoundError: [Errno 2] No such file or directory: 'private_key.pem'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/root/.ansible/tmp/ansible-tmp-1645216851.5207298-1082-23267758012974/AnsiballZ_nsxt_certificates.py", line 107, in _ansiballz_main() File "/root/.ansible/tmp/ansible-tmp-1645216851.5207298-1082-23267758012974/AnsiballZ_nsxt_certificates.py", line 99, in _ansiballz_main invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS) File "/root/.ansible/tmp/ansible-tmp-1645216851.5207298-1082-23267758012974/AnsiballZ_nsxt_certificates.py", line 47, in invoke_module runpy.run_module(mod_name='ansible_collections.vmware.ansible_for_nsxt.plugins.modules.nsxt_certificates', init_globals=dict(_module_fqn='ansible_collections.vmware.ansible_for_nsxt.plugins.modules.nsxt_certificates', _modlib_path=modlib_path), File "/usr/lib/python3.8/runpy.py", line 207, in run_module return _run_module_code(code, init_globals, run_name, mod_spec) File "/usr/lib/python3.8/runpy.py", line 97, in _run_module_code _run_code(code, mod_globals, init_globals, File "/usr/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/modules/nsxt_certificates.py", line 225, in File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/modules/nsxt_certificates.py", line 204, in main UnboundLocalError: local variable 'request_data' referenced before assignment fatal: [vsphere-basic-z-external -> localhost]: FAILED! => { "changed": false, "module_stderr": "Traceback (most recent call last):\n File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/modules/nsxt_certificates.py", line 197, in main\n File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/modules/nsxt_certificates.py", line 127, in update_params_with_pem_encoding\n File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/module_utils/vmware_nsxt.py", line 111, in get_private_key_string\nFileNotFoundError: [Errno 2] No such file or directory: 'private_key.pem'\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File "/root/.ansible/tmp/ansible-tmp-1645216851.5207298-1082-23267758012974/AnsiballZ_nsxt_certificates.py", line 107, in \n _ansiballz_main()\n File "/root/.ansible/tmp/ansible-tmp-1645216851.5207298-1082-23267758012974/AnsiballZ_nsxt_certificates.py", line 99, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File "/root/.ansible/tmp/ansible-tmp-1645216851.5207298-1082-23267758012974/AnsiballZ_nsxt_certificates.py", line 47, in invoke_module\n runpy.run_module(mod_name='ansible_collections.vmware.ansible_for_nsxt.plugins.modules.nsxt_certificates', init_globals=dict(_module_fqn='ansible_collections.vmware.ansible_for_nsxt.plugins.modules.nsxt_certificates', _modlib_path=modlib_path),\n File "/usr/lib/python3.8/runpy.py", line 207, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File "/usr/lib/python3.8/runpy.py", line 97, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File "/usr/lib/python3.8/runpy.py", line 87, in _run_code\n exec(code, run_globals)\n File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/modules/nsxt_certificates.py", line 225, in \n File "/tmp/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload_b4raqiul/ansible_vmware.ansible_for_nsxt.nsxt_certificates_payload.zip/ansible_collections/vmware/ansible_for_nsxt/plugins/modules/nsxt_certificates.py", line 204, in main\nUnboundLocalError: local variable 'request_data' referenced before assignment\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1 }`

I have generated a self-signed certificate as follows, https://www.scottbrady91.com/openssl/creating-rsa-keys-using-openssl

`# generate a private key with the correct length openssl genrsa -out private-key.pem 3072

generate corresponding public key

openssl rsa -in private-key.pem -pubout -out public-key.pem

optional: create a self-signed certificate

openssl req -new -x509 -key private-key.pem -out cert.pem -days 360`

My playbook is as such, - name: Add a new certificate delegate_to: localhost nsxt_certificates: hostname: <hostname> username: "admin" password: <password> validate_certs: false display_name: "manual-self-sign" pem_encoded_file: cert.pem private_key_file: private_key.pem id: "manual-self-sign" state: "present"

When i run the playbook without the private-key, it works fine. Once i add the private-key file thats when i get the error. Reason I need the private-key, is because ive noticed that is the only way i can specify the "id" for the cert.

Reproduction steps

1. Generate a self-signed cert. following the first 3 commands https://www.scottbrady91.com/openssl/creating-rsa-keys-using-openssl
2. Fill in data for "pem_encoded", and "private_key_file", "id" and run the playbook/module nsxt_certificates.
3. 
...

Expected behavior

Should have a new "Self Signed Cert with Private Key" with the "id" we specified

Additional context

No response

zhaq90 avatar Feb 18 '22 20:02 zhaq90

Hey @zhaq90 Is your pem file path correct? As specified here in the example..

It may be the case that is failing because of directory structure change.. Not sure though... Try giving absolute path of the pem file that should work mostly..

AkhileshK8y avatar Feb 22 '22 23:02 AkhileshK8y