Debian Codegraph @

ansible_collections/community/general/plugins/modules/zypper_repository_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2024, Tobias Zeuch <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8
9 from __future__ import annotations
10
11 DOCUMENTATION = r"""
12 module: zypper_repository_info
13 author: "Tobias Zeuch (@TobiasZeuch181)"
14 version_added: 10.0.0
15 short_description: List Zypper repositories
16 description:
17 - List Zypper repositories on SUSE and openSUSE.
18 extends_documentation_fragment:
19 - community.general._attributes
20 - community.general._attributes.info_module
21
22 requirements:
23 - "zypper >= 1.0 (included in openSUSE >= 11.1 or SUSE Linux Enterprise Server/Desktop >= 11.0)"
24 - python-xml
25 notes:
26 - For info about packages, use the module M(ansible.builtin.package_facts).
27 """
28
29 EXAMPLES = r"""
30 - name: List registered repositories and store in variable repositories
31 community.general.zypper_repository_info: {}
32 register: repodatalist
33 """
34
35 RETURN = r"""
36 repodatalist:
37 description:
38 - A list of repository descriptions like it is returned by the command C(zypper repos).
39 type: list
40 returned: always
41 elements: dict
42 contains:
43 alias:
44 description: The alias of the repository.
45 type: str
46 sample: "SLE-Product-SLES"
47 autorefresh:
48 description: Indicates, if autorefresh is enabled on the repository.
49 type: int
50 sample: "1"
51 enabled:
52 description: Indicates, if the repository is enabled.
53 type: int
54 sample: "1"
55 gpgcheck:
56 description: Indicates, if the GPG signature of the repository meta data is checked.
57 type: int
58 sample: "1"
59 name:
60 description: The name of the repository.
61 type: str
62 sample: "SLE-Product-SLES"
63 priority:
64 description: The priority of the repository.
65 type: int
66 sample: "99"
67 url:
68 description: The URL of the repository on the internet.
69 type: str
70 sample: "http://repo:50000/repo/SUSE/Products/SLE-Product-SLES/15-SP2/x86_64/product"
71 """
72
73
74 from ansible_collections.community.general.plugins.module_utils import _deps as deps
75
76 with deps.declare("parseXML"):
77 from xml.dom.minidom import parseString as parseXML
78
79 from ansible.module_utils.basic import AnsibleModule
80
81 REPO_OPTS = ["alias", "name", "priority", "enabled", "autorefresh", "gpgcheck"]
82
83
84 def _get_cmd(module, *args):
85 """Combines the non-interactive zypper command with arguments/subcommands"""
86 cmd = [module.get_bin_path("zypper", required=True), "--quiet", "--non-interactive"]
87 cmd.extend(args)
88
89 return cmd
90
91
92 def _parse_repos(module):
93 """parses the output of zypper --xmlout repos and return a parse repo dictionary"""
94 cmd = _get_cmd(module, "--xmlout", "repos")
95
96 rc, stdout, stderr = module.run_command(cmd, check_rc=False)
97 if rc == 0:
98 repos = []
99 dom = parseXML(stdout)
100 repo_list = dom.getElementsByTagName("repo")
101 for repo in repo_list:
102 opts = {}
103 for o in REPO_OPTS:
104 opts[o] = repo.getAttribute(o)
105 opts["url"] = repo.getElementsByTagName("url")[0].firstChild.data
106 # A repo can be uniquely identified by an alias + url
107 repos.append(opts)
108 return repos
109 # exit code 6 is ZYPPER_EXIT_NO_REPOS (no repositories defined)
110 elif rc == 6:
111 return []
112 else:
113 module.fail_json(msg=f'Failed to execute "{" ".join(cmd)}"', rc=rc, stdout=stdout, stderr=stderr)
114
115
116 def main():
117 module = AnsibleModule(argument_spec=dict(), supports_check_mode=True)
118 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
119
120 deps.validate(parseXML)
121
122 repodatalist = _parse_repos(module)
123 module.exit_json(changed=False, repodatalist=repodatalist)
124
125
126 if __name__ == "__main__":
127 main()