Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix icx_system.py UnboundLocalError: local variable 'hostname' referenced before assignment #436

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

omegafoo
Copy link

@omegafoo omegafoo commented May 18, 2022

SUMMARY

Testing against a config with the following items fail:

tacacs-server enable vlan 10
tacacs-server host 100.100.100.1
tacacs-server key 2 $SomeEncryptedKey

Setting hostname to 'None', as was done for other k:v pairs in parse_aaa_servers allows correct parsing, script completion and ansible playbooks to run successfully.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

icx_system.py

ADDITIONAL INFORMATION

If parsing an ICX style config without tacacs-server vlan filtering enabled, an unmodified icx_system.py successfully completes and allows the playbooks to finish without error. However if VLAN filtering is enabled then the icx_system.py errors out and ansible playbooks fail for that host.

A switch with the following config works correctly without modifying icx_system.py

tacacs-server host 100.100.100.1
tacacs-server key 2 $EncryptedString
$ ansible-playbook -i /etc/ansible/inventory/test.yaml /etc/ansible/playbooks/edge/switches.yaml --vault-id user@prompt

PLAY [Configure ALL edge switches] *******************************************************************************************

TASK [edge-switch : Configure Foundry/Brocade/Ruckus hostname] ***************************************************************
changed: [<hostname>]

PLAY RECAP *******************************************************************************************************************
<hostname>    : ok=1    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

A switch with the following config fails due to attempting to map the hostname before it is assigned while parsing the vlan filter line before the host line.

tacacs-server enable vlan 10
tacacs-server host 100.100.100.1
tacacs-server key 2 $EncryptedString

output:

$ ansible-playbook -i /etc/ansible/inventory/bes.yaml /etc/ansible/playbooks/edge/switches.yaml --vault-id user@prompt

PLAY [Configure ALL edge switches] *******************************************************************************************

TASK [edge-switch : Configure Foundry/Brocade/Ruckus hostname] ***************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: UnboundLocalError: local variable 'hostname' referenced before assignment
fatal: [<hostname>]: FAILED! => changed=false 
  module_stderr: |-
    Traceback (most recent call last):
      File "/home/jmn7047/.ansible/tmp/ansible-local-55894ua4gzdsl/ansible-tmp-1652895247.394549-55903-147099394034167/AnsiballZ_icx_system.py", line 102, in <module>
        _ansiballz_main()
      File "/home/uid/.ansible/tmp/ansible-local-55894ua4gzdsl/ansible-tmp-1652895247.394549-55903-147099394034167/AnsiballZ_icx_system.py", line 94, in _ansiballz_main
        invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
      File "/home/uid/.ansible/tmp/ansible-local-55894ua4gzdsl/ansible-tmp-1652895247.394549-55903-147099394034167/AnsiballZ_icx_system.py", line 40, in invoke_module
        runpy.run_module(mod_name='ansible_collections.community.network.plugins.modules.icx_system', 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.network.icx_system_payload_cjqy4vi6/ansible_community.network.icx_system_payload.zip/ansible_collections/community/network/plugins/modules/icx_system.py", line 466, in <module>
      File "/tmp/ansible_community.network.icx_system_payload_cjqy4vi6/ansible_community.network.icx_system_payload.zip/ansible_collections/community/network/plugins/modules/icx_system.py", line 453, in main
      File "/tmp/ansible_community.network.icx_system_payload_cjqy4vi6/ansible_community.network.icx_system_payload.zip/ansible_collections/community/network/plugins/modules/icx_system.py", line 398, in map_config_to_obj
      File "/tmp/ansible_community.network.icx_system_payload_cjqy4vi6/ansible_community.network.icx_system_payload.zip/ansible_collections/community/network/plugins/modules/icx_system.py", line 379, in parse_aaa_servers
    UnboundLocalError: local variable 'hostname' referenced before assignment
  module_stdout: ''
  msg: |-
    MODULE FAILURE
    See stdout/stderr for the exact error
  rc: 1

PLAY RECAP *******************************************************************************************************************
<hostname>    : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Since the first line parsed is not the host entry then the hostname is never assigned. Setting hostname to 'None' as other attributes are done allows the module to correctly process the aaa config and results in a successful playbook run.

Switch with the following config now completes successfully after the code changes

tacacs-server enable vlan 10
tacacs-server host 100.100.100.1
tacacs-server key 2 $EncryptedString

$ ansible-playbook -i /etc/ansible/inventory/test.yaml /etc/ansible/playbooks/edge/switches.yaml --vault-id user@prompt

PLAY [Configure ALL edge switches] *******************************************************************************************

TASK [edge-switch : Configure Foundry/Brocade/Ruckus hostname] ***************************************************************
changed: []

PLAY RECAP *******************************************************************************************************************
: ok=1 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

Testing against a config with the following items fail:

tacacs-server enable vlan 10
tacacs-server host 100.100.100.1
tacacs-server key 2 $SomeEncryptedKey

Setting hostname to 'None', as was done for other k:v pairs in parse_aaa_servers allows correct parsing, script completion and ansible playbooks to run successfully.
@gundalow
Copy link
Contributor

gundalow commented Jun 1, 2022

bot_status

@ansibullbot
Copy link
Collaborator

@ansibullbot
Copy link
Collaborator

Components

plugins/modules/network/icx/icx_system.py
support: core
maintainers: Commscope sushma-alethea

Metadata

waiting_on: ansible
changes_requested_by: null
needs_info: False
needs_revision: False
needs_rebase: False
merge_commits: []
too many files or commits: False
mergeable_state: clean
ci_status: None
maintainer_shipits (module maintainers): 0
community_shipits (namespace maintainers): 0
ansible_shipits (core team members): 0
shipit_actors (maintainer or core team member): []
shipit_actors_other: []
automerge: automerge shipit test failed

click here for bot help

@ansibullbot ansibullbot added bug This issue/PR relates to a bug module module needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI new_contributor Help guide this first time contributor plugins plugin (any type) small_patch Hopefully easy to review traceback stale_ci CI is older than 7 days, rerun before merging and removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI labels Jun 9, 2022
@omegafoo omegafoo closed this Jun 14, 2022
@omegafoo omegafoo reopened this Jun 14, 2022
@omegafoo
Copy link
Author

ready_for_review

@omegafoo
Copy link
Author

bot_status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug module module new_contributor Help guide this first time contributor plugins plugin (any type) small_patch Hopefully easy to review stale_ci CI is older than 7 days, rerun before merging traceback
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants