Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
If you are interested in working on this issue or have submitted a pull request, please leave a comment
Description
Running a query to mso_schema_template_anp_epg_contract with no contract or contracts specified, will return an UnboundLocalError when trying to access contract_ref (undefined) in plugins/modules/mso_schema_template_anp_epg_contract.py line 354.
Affected Module Name(s):
cisco.mso.mso_schema_template_anp_epg_contract
MSO version and MSO Platform
any
APIC version and APIC Platform for Site Level Resources
any
Collection versions
cisco.mso 2.9.0
Output/ Error message
Traceback (most recent call last):
File "/Users/jbrink/.ansible/tmp/ansible-local-9280142kc74ki/ansible-tmp-1724359497.482809-92913-215026567338156/AnsiballZ_mso_schema_template_anp_epg_contract.py", line 259, in <module>
_ansiballz_main()
File "/Users/jbrink/.ansible/tmp/ansible-local-9280142kc74ki/ansible-tmp-1724359497.482809-92913-215026567338156/AnsiballZ_mso_schema_template_anp_epg_contract.py", line 249, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/Users/jbrink/.ansible/tmp/ansible-local-9280142kc74ki/ansible-tmp-1724359497.482809-92913-215026567338156/AnsiballZ_mso_schema_template_anp_epg_contract.py", line 122, in invoke_module
runpy.run_module(mod_name='ansible_collections.cisco.mso.plugins.modules.mso_schema_template_anp_epg_contract', init_globals=dict(_module_fqn='ansible_collections.cisco.mso.plugins.modules.mso_schema_template_anp_epg_contract', _modlib_path=modlib_path),
File "<frozen runpy>", line 226, in run_module
File "<frozen runpy>", line 98, in _run_module_code
File "<frozen runpy>", line 88, in _run_code
File "/var/folders/x4/lfbrl1gj5hb178gchm8df2dh0000gn/T/ansible_cisco.mso.mso_schema_template_anp_epg_contract_payload_bhz7s79i/ansible_cisco.mso.mso_schema_template_anp_epg_contract_payload.zip/ansible_collections/cisco/mso/plugins/modules/mso_schema_template_anp_epg_contract.py", line 449, in <module>
File "/var/folders/x4/lfbrl1gj5hb178gchm8df2dh0000gn/T/ansible_cisco.mso.mso_schema_template_anp_epg_contract_payload_bhz7s79i/ansible_cisco.mso.mso_schema_template_anp_epg_contract_payload.zip/ansible_collections/cisco/mso/plugins/modules/mso_schema_template_anp_epg_contract.py", line 345, in main
UnboundLocalError: cannot access local variable 'contract_ref' where it is not associated with a value
Expected Behavior
When no contract or contracts variables are provided, return a list of only existing contracts assigned to the EPG or an empty list if there are none
Actual Behavior
When no contract or contracts variables are provided, and the EPG has no assigned contracts, the error message provided is trying to access contract_ref and contract.type, as if the contract was provided. This check should only be run if contract is not null, and return [] otherwise
if contracts:
if len(found_contracts) == len(contracts):
mso.existing = found_contracts
else:
not_found_contracts = [
"Contract with Reference '{0}' and type '{1}' not found".format(mso.contract_ref(**contract), contract.get("type"))
for contract in contracts
if contract not in found_contracts
]
mso.fail_json(msg=not_found_contracts)
- elif not mso.existing:
+ elif contract and not mso.existing:
mso.fail_json(msg="Contract with Reference '{0}' and type '{1}' not found".format(contract_ref, contract.get("type")))
mso.exit_json()
Community Note
Description
Affected Module Name(s):
MSO version and MSO Platform
APIC version and APIC Platform for Site Level Resources
Collection versions
Output/ Error message
Expected Behavior
Actual Behavior
Playbook tasks to Reproduce
The fix is easy and I can provide a PR if needed: