PaloAltoNetworks / pan-os-python

The PAN-OS SDK for Python is a package to help interact with Palo Alto Networks devices (including physical and virtualized Next-generation Firewalls and Panorama). The pan-os-python SDK is object oriented and mimics the traditional interaction with the device via the GUI or CLI/API.
https://pan-os-python.readthedocs.io
ISC License
339 stars 166 forks source link

Issue with #493 when commit is not required, response None #569

Open antoinekh opened 2 weeks ago

antoinekh commented 2 weeks ago

Describe the bug

Was OK before 02/2024

With Ansible, when I commit to Panorama I have this issue. This is related to the pan-os-python as

The full traceback is:
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/panos/base.py", line 4899, in _commit
    jobid = commit_response.find("./result/job").text
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'text'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/root/.ansible/tmp/ansible-tmp-1719845283.5125809-203-3565473327147/AnsiballZ_panos_commit_panorama.py", line 107, in <module>
    _ansiballz_main()
  File "/root/.ansible/tmp/ansible-tmp-1719845283.5125809-203-3565473327147/AnsiballZ_panos_commit_panorama.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/root/.ansible/tmp/ansible-tmp-1719845283.5125809-203-3565473327147/AnsiballZ_panos_commit_panorama.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible_collections.paloaltonetworks.panos.plugins.modules.panos_commit_panorama', init_globals=dict(_module_fqn='ansible_collections.paloaltonetworks.panos.plugins.modules.panos_commit_panorama', _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 "/tmp/ansible_paloaltonetworks.panos.panos_commit_panorama_payload_p38osrq0/ansible_paloaltonetworks.panos.panos_commit_panorama_payload.zip/ansible_collections/paloaltonetworks/panos/plugins/modules/panos_commit_panorama.py", line 222, in <module>
  File "/tmp/ansible_paloaltonetworks.panos.panos_commit_panorama_payload_p38osrq0/ansible_paloaltonetworks.panos.panos_commit_panorama_payload.zip/ansible_collections/paloaltonetworks/panos/plugins/modules/panos_commit_panorama.py", line 200, in main
  File "/usr/local/lib/python3.11/site-packages/panos/base.py", line 4803, in commit
    return self._commit(
           ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/panos/base.py", line 4906, in _commit
    commit_response_msg = commit_response.find("./msg/line").text
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'text'
fatal: [oppano21s.net.meshcore.net]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/usr/local/lib/python3.11/site-packages/panos/base.py\", line 4899, in _commit\n    jobid = commit_response.find(\"./result/job\").text\n            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nAttributeError: 'NoneType' object has no attribute 'text'\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1719845283.5125809-203-3565473327147/AnsiballZ_panos_commit_panorama.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1719845283.5125809-203-3565473327147/AnsiballZ_panos_commit_panorama.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1719845283.5125809-203-3565473327147/AnsiballZ_panos_commit_panorama.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.paloaltonetworks.panos.plugins.modules.panos_commit_panorama', init_globals=dict(_module_fqn='ansible_collections.paloaltonetworks.panos.plugins.modules.panos_commit_panorama', _modlib_path=modlib_path),\n  File \"<frozen runpy>\", line 226, in run_module\n  File \"<frozen runpy>\", line 98, in _run_module_code\n  File \"<frozen runpy>\", line 88, in _run_code\n  File \"/tmp/ansible_paloaltonetworks.panos.panos_commit_panorama_payload_p38osrq0/ansible_paloaltonetworks.panos.panos_commit_panorama_payload.zip/ansible_collections/paloaltonetworks/panos/plugins/modules/panos_commit_panorama.py\", line 222, in <module>\n  File \"/tmp/ansible_paloaltonetworks.panos.panos_commit_panorama_payload_p38osrq0/ansible_paloaltonetworks.panos.panos_commit_panorama_payload.zip/ansible_collections/paloaltonetworks/panos/plugins/modules/panos_commit_panorama.py\", line 200, in main\n  File \"/usr/local/lib/python3.11/site-packages/panos/base.py\", line 4803, in commit\n    return self._commit(\n           ^^^^^^^^^^^^^\n  File \"/usr/local/lib/python3.11/site-packages/panos/base.py\", line 4906, in _commit\n    commit_response_msg = commit_response.find(\"./msg/line\").text\n                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nAttributeError: 'NoneType' object has no attribute 'text'\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

Expected behavior

It should work with a message Commit not needed No issues when modification are there to commit to the device.

Current behavior

Python error

 commit_response_msg = commit_response.find(\"./msg/line\").text\nAttributeError: 'NoneType' object has no attribute 'text'\n",

Possible solution

Change line on /panos/base.py (https://github.com/PaloAltoNetworks/pan-os-python/blob/62bcff3bca86a5ff745eae3e92635d109673963a/panos/base.py#L4906C1-L4906C78)

to add a try except

                try:
                    commit_response_msg = commit_response.find("./msg/line").text
                except AttributeError:
                    commit_response_msg = "No message in commit response"
                self._logger.debug("No job id. Msg: " + commit_response_msg)

Steps to reproduce

  1. Commit without changes

Screenshots

Context

Issue is with last version of ansible, and pan-os-python

Your Environment

antoinekh# ansible --version
ansible [core 2.17.1]
  config file = /home/w132231/w132231/ansible_performance/ansible.cfg
  configured module search path = ['/home/w132231/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/w132231/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/w132231/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/w132231/.local/bin/ansible
  python version = 3.10.13 (main, Aug 25 2023, 13:20:03) [GCC 9.4.0] (/usr/bin/python3.10)
  jinja version = 3.1.2
  libyaml = True

antoinekh# pip3 freeze                   
pan-os-python==1.12.1
pan-python==0.17.0
antoinekh commented 2 weeks ago

Maybe similar to #471

antoinekh commented 2 weeks ago

I have tried with success to add the following lines and this fix the issue

L4906 on base.py

    try:
        commit_response_msg = commit_response.find("./msg/line").text
    except AttributeError:
        commit_response_msg = "No message in commit response"
    self._logger.debug("No job id. Msg: " + commit_response_msg)
antoinekh commented 2 weeks ago

the problem has been added here #493 the 07/02/2024

antoinekh commented 2 weeks ago

Pull request #557

antoinekh commented 2 weeks ago

I can confirm that it works in pan-os-python 1.11.0