Repository: ansible-collections/community.general

Issue:
jenkins_plugin module has undefined Variable tmp_updates_file on update of plugins
### Summary

When updating plugins in jenkins with module jenkins_plugins we get the following error:

msg": "Cannot load JSON data from the tmp updates file."
"details": "local variable 'tmp_updates_file' referenced before assignment"

The error doesnt happen on old version of ansible. The jenkins-plugin Module code shows that this error code is caused by
https://github.com/ansible-collections/community.general/pull/5565

### Issue Type

Bug Report

### Component Name

jenkins_plugin

### Ansible Version

```console
$ ansible --version
ansible [core 2.13.7]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
  ansible collection location = /home/runner/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.8.13 (default, Jun 24 2022, 15:27:57) [GCC 8.5.0 20210514 (Red Hat 8.5.0-13)]
  jinja version = 3.1.2
  libyaml = True
```


### Community.general Version

```console
$ ansible-galaxy collection list community.general
# /usr/local/lib/python3.8/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 5.8.0 # /usr/share/ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.general 6.1.0
```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed

```


### OS / Environment

RHEL 7

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: update Jenkins plugins to latest
  jenkins_plugin:
    name: "{{ item.path | basename }}"
    updates_url: "{{ jenkins_updates_url }}"
    state: latest
    jenkins_home: "{{ jenkins_home }}"
    force: yes
    with_dependencies: yes
    url: "https://{{ jenkins_hostname }}:{{ jenkins_https_port }}{{ jenkins_url_prefix }}"
    url_username: "redacted"
    url_password: "{{ jenkins_admin_password }}"
    timeout: "300"
    validate_certs: no
  loop: "{{ installed_plugins.files }}"
```


### Expected Results

I expect No error message. The Jenkins Plugin should be downloaded and updated

### Actual Results

```console (paste below)
full trace
`<REDACTED> (1, b'\\n{"msg": "Cannot load JSON data from the tmp updates file.", "failed": true, "exception": "WARNING: The below traceback may *not* be related to the actual failure.\\\\n  File \\\\"/tmp/ansible_jenkins_plugin_payload_6FcmUB/ansible_jenkins_plugin_payload.zip/ansible_collections/community/general/plugins/modules/jenkins_plugin.py\\\\", line 641, in _download_updates\\\\n", "details": "local variable \\'tmp_updates_file\\' referenced before assignment", "invocation": {"module_args": {"force": true, "owner": "jenkins", "client_key": null, "latest_plugins_url_segments": ["latest"], "group": "jenkins", "use_proxy": true, "unsafe_writes": false, "serole": null, "updates_url": ["https://updates.jenkins.io/dynamic-stable-2.375.1"], "state": "present", "version": "latest", "updates_expiration": 86400, "client_cert": null, "setype": null, "url_username": "redacted", "selevel": null, "force_basic_auth": true, "versioned_plugins_url_segments": ["download/plugins", "plugins"], "http_agent": "ansible-httpget", "use_gssapi": false, "jenkins_home": "/var/lib/jenkins", "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "name": "javax-mail-api", "url": "https://localhost:8443", "seuser": null, "with_dependencies": true, "mode": "0644", "timeout": 300.0, "attributes": null, "validate_certs": false, "update_json_url_segment": ["update-center.json", "updates/update-center.json"]}}}\\n', b'/etc/profile.d/lang.sh: line 19: warning: setlocale: LC_CTYPE: cannot change locale (C.UTF-8)\\n')
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
  File "/tmp/ansible_jenkins_plugin_payload_6FcmUB/ansible_jenkins_plugin_payload.zip/ansible_collections/community/general/plugins/modules/jenkins_plugin.py", line 641, in _download_updates
failed: [REDACTED] (item={'uid': 993, 'woth': False, 'mtime': 1672747583.4952004, 'inode': 1610643167, 'isgid': False, 'size': 56, 'roth': True, 'isuid': False, 'isreg': False, 'pw_name': 'jenkins', 'gid': 10080, 'ischr': False, 'wusr': True, 'xoth': True, 'rusr': True, 'nlink': 4, 'issock': False, 'rgrp': True, 'gr_name': 'jenkins', 'path': '/var/lib/jenkins/plugins/javax-mail-api', 'xusr': True, 'atime': 1672747583.4712002, 'isdir': True, 'ctime': 1672747583.4952004, 'isblk': False, 'xgrp': True, 'dev': 64777, 'wgrp': False, 'isfifo': False, 'mode': '0755', 'islnk': False}) => {
    "ansible_loop_var": "item",
    "changed": false,
    "details": "local variable 'tmp_updates_file' referenced before assignment",
    "invocation": {
        "module_args": {
            "attributes": null,
            "client_cert": null,
            "client_key": null,
            "force": true,
            "force_basic_auth": true,
            "group": "jenkins",
            "http_agent": "ansible-httpget",
            "jenkins_home": "/var/lib/jenkins",
            "latest_plugins_url_segments": [
                "latest"
            ],
            "mode": "0644",
            "name": "javax-mail-api",
            "owner": "jenkins",
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "state": "present",
            "timeout": 300.0,
            "unsafe_writes": false,
            "update_json_url_segment": [
                "update-center.json",
                "updates/update-center.json"
            ],
            "updates_expiration": 86400,
            "updates_url": [
                "https://updates.jenkins.io/dynamic-stable-2.375.1"
            ],
            "url": "https://localhost:8443",
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "url_username": "redacted",
            "use_gssapi": false,
            "use_proxy": true,
            "validate_certs": false,
            "version": "latest",
            "versioned_plugins_url_segments": [
                "download/plugins",
                "plugins"
            ],
            "with_dependencies": true
        }
    },
    "item": {
        "atime": 1672747583.4712002,
        "ctime": 1672747583.4952004,
        "dev": 64777,
        "gid": 10080,
        "gr_name": "jenkins",
        "inode": 1610643167,
        "isblk": false,
        "ischr": false,
        "isdir": true,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": false,
        "issock": false,
        "isuid": false,
        "mode": "0755",
        "mtime": 1672747583.4952004,
        "nlink": 4,
        "path": "/var/lib/jenkins/plugins/javax-mail-api",
        "pw_name": "jenkins",
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 56,
        "uid": 993,
        "wgrp": false,
        "woth": false,
        "wusr": true,
        "xgrp": true,
        "xoth": true,
        "xusr": true
    },
    "msg": "Cannot load JSON data from the tmp updates file."
}`
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/jenkins_plugin.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/jenkins_plugin.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @jtyr
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
The error occurs only when the update-center.json is not downloaded because ins this case the the variable is not initialized.
following workaround exists therefore: set updates_expiration to 0 to force download
CC @russoz
Hi @MichaelEBi thanks for reporting this issue. Into it. 
@russoz ping
I created a fix for this in #6100.

Resolving patch:
diff --git a/plugins/modules/jenkins_plugin.py b/plugins/modules/jenkins_plugin.py
--- a/plugins/modules/jenkins_plugin.py
+++ b/plugins/modules/jenkins_plugin.py
@@ -641,6 +641,8 @@ def _download_updates(self):
                 self.module.fail_json(
                     msg="Cannot close the tmp updates file %s." % tmp_updates_file,
                     details=to_native(e))
+        else:
+            tmp_updates_file = updates_file
 
         # Open the updates file
         try:
@@ -651,15 +653,15 @@ def _download_updates(self):
             data = json.loads(f.readline())
         except IOError as e:
             self.module.fail_json(
-                msg="Cannot open temporal updates file.",
+                msg="Cannot open%s updates file." % (" temporary" if tmp_updates_file != updates_file else ""),
                 details=to_native(e))
         except Exception as e:
             self.module.fail_json(
-                msg="Cannot load JSON data from the tmp updates file.",
+                msg="Cannot load JSON data from the%s updates file." % (" temporary" if tmp_updates_file != updates_file else ""),
                 details=to_native(e))
 
         # Move the updates file to the right place if we could read it
-        if download_updates:
+        if tmp_updates_file != updates_file:
             self.module.atomic_move(tmp_updates_file, updates_file)
 
         # Check if we have the plugin data available


Test patch: