Skip to content

Fix for UnboundLocalError: cannot access local variable 'resp' ...#822

Open
andyjsharp wants to merge 6 commits into
Juniper:masterfrom
andyjsharp:master
Open

Fix for UnboundLocalError: cannot access local variable 'resp' ...#822
andyjsharp wants to merge 6 commits into
Juniper:masterfrom
andyjsharp:master

Conversation

@andyjsharp
Copy link
Copy Markdown

As described in issue #821, running the file-delete RPC can return module failure output that includes:

  • No start of json char found
  • UnboundLocalError: cannot access local variable 'resp' ...

This pull request fixes both issues by initializing resp before RPC execution.

In addition, some RPC operations such as file-delete may return an empty reply body. Previously this could lead to Unexpected response type <class 'NoneType'>. This change adds explicit handling for an empty file-delete reply so the task is treated as successful, without requiring failed_when: false.

Using the playbook from issue #821, with the following playbook change:

    - name: DELETE file from device (RPC file-delete)                                           
      juniper.device.rpc:                                                                       
        rpc: file-delete                                                                        
        kwargs:                                                                                 
          path: "{{ jmuse_remote_path }}"                                                       
      register: jmuse_delete_result                                                             
      changed_when: true 

The playbook now runs without failures:

TASK [PUT file to device] ********************************************************************
changed: [srx]

TASK [DELETE file from device (RPC file-delete)] *********************************************
changed: [srx]

TASK [Verify file no longer exists] **********************************************************
ok: [srx]

TASK [Show RPC delete summary] ***************************************************************
ok: [srx] => {
    "msg": {
        "contains_json_start_error": false,
        "contains_unbound_local_error": false,
        "delete_exception": "",
        "delete_failed": false,
        "delete_module_stderr": "",
        "delete_msg": "The RPC executed successfully with an empty reply.",
        "put_failed": false,
        "remote_path": "/var/tmp/jmuse-dev-bug-rpc-20260506T093003.txt",
        "verify_output": "\n/var/tmp/jmuse-dev-bug-rpc-20260506T093003.txt: No such file or directory\n"
    }
}

andyjsharp added 6 commits May 5, 2026 21:31
Fix for UnboundLocalError: cannot access local variable 'resp' ...
UnboundLocalError: cannot access local variable 'resp' ...
Fix for file-delete rpc when resp is None
PEP8 fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant