Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2023, Andrew Hyatt <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: dnf_config_manager
10 short_description: Enable or disable dnf repositories using config-manager
11 version_added: 8.2.0
12 description:
13 - This module enables or disables repositories using the C(dnf config-manager) sub-command.
14 author: Andrew Hyatt (@ahyattdev) <[email protected]>
15 requirements:
16 - dnf
17 - dnf-plugins-core
18 extends_documentation_fragment:
19 - community.general._attributes
20 attributes:
21 check_mode:
22 support: full
23 diff_mode:
24 support: none
25 options:
26 name:
27 description:
28 - Repository ID, for example V(crb).
29 default: []
30 type: list
31 elements: str
32 state:
33 description:
34 - Whether the repositories should be V(enabled) or V(disabled).
35 default: enabled
36 type: str
37 choices: [enabled, disabled]
38 notes:
39 - Does not work with C(dnf5).
40 seealso:
41 - module: ansible.builtin.dnf
42 - module: ansible.builtin.yum_repository
43 """
44
45 EXAMPLES = r"""
46 - name: Ensure the crb repository is enabled
47 community.general.dnf_config_manager:
48 name: crb
49 state: enabled
50
51 - name: Ensure the appstream and zfs repositories are disabled
52 community.general.dnf_config_manager:
53 name:
54 - appstream
55 - zfs
56 state: disabled
57 """
58
59 RETURN = r"""
60 repo_states_pre:
61 description: Repo IDs before action taken.
62 returned: success
63 type: dict
64 contains:
65 enabled:
66 description: Enabled repository IDs.
67 returned: success
68 type: list
69 elements: str
70 disabled:
71 description: Disabled repository IDs.
72 returned: success
73 type: list
74 elements: str
75 sample:
76 enabled:
77 - appstream
78 - baseos
79 - crb
80 disabled:
81 - appstream-debuginfo
82 - appstream-source
83 - baseos-debuginfo
84 - baseos-source
85 - crb-debug
86 - crb-source
87 repo_states_post:
88 description: Repository states after action taken.
89 returned: success
90 type: dict
91 contains:
92 enabled:
93 description: Enabled repository IDs.
94 returned: success
95 type: list
96 elements: str
97 disabled:
98 description: Disabled repository IDs.
99 returned: success
100 type: list
101 elements: str
102 sample:
103 enabled:
104 - appstream
105 - baseos
106 - crb
107 disabled:
108 - appstream-debuginfo
109 - appstream-source
110 - baseos-debuginfo
111 - baseos-source
112 - crb-debug
113 - crb-source
114 changed_repos:
115 description: Repositories changed.
116 returned: success
117 type: list
118 elements: str
119 sample: ["crb"]
120 """
121
122 import os
123 import re
124
125 from ansible.module_utils.basic import AnsibleModule
126
127 DNF_BIN = "/usr/bin/dnf"
128 REPO_ID_RE = re.compile(r"^Repo-id\s*:\s*(\S+)$")
129 REPO_STATUS_RE = re.compile(r"^Repo-status\s*:\s*(disabled|enabled)$")
130
131
132 def get_repo_states(module):
133 rc, out, err = module.run_command([DNF_BIN, "repolist", "--all", "--verbose"], check_rc=True)
134
135 repos = dict()
136 last_repo = ""
137 for line in out.split("\n"):
138 m = REPO_ID_RE.match(line)
139 if m:
140 if len(last_repo) > 0:
141 module.fail_json(msg="dnf repolist parse failure: parsed another repo id before next status")
142 last_repo = m.group(1)
143 continue
144 m = REPO_STATUS_RE.match(line)
145 if m:
146 if len(last_repo) == 0:
147 module.fail_json(msg="dnf repolist parse failure: parsed status before repo id")
148 repos[last_repo] = m.group(1)
149 last_repo = ""
150 return repos
151
152
153 def set_repo_states(module, repo_ids, state):
154 module.run_command([DNF_BIN, "config-manager", "--assumeyes", f"--set-{state}"] + repo_ids, check_rc=True)
155
156
157 def pack_repo_states_for_return(states):
158 enabled = []
159 disabled = []
160 for repo_id in states:
161 if states[repo_id] == "enabled":
162 enabled.append(repo_id)
163 else:
164 disabled.append(repo_id)
165
166 # Sort for consistent results
167 enabled.sort()
168 disabled.sort()
169
170 return {"enabled": enabled, "disabled": disabled}
171
172
173 def main():
174 module_args = dict(
175 name=dict(type="list", elements="str", default=[]),
176 state=dict(type="str", choices=["enabled", "disabled"], default="enabled"),
177 )
178
179 result = dict(changed=False)
180
181 module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
182 module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C")
183
184 if not os.path.exists(DNF_BIN):
185 module.fail_json(msg=f"{DNF_BIN} was not found")
186
187 repo_states = get_repo_states(module)
188 result["repo_states_pre"] = pack_repo_states_for_return(repo_states)
189
190 desired_repo_state = module.params["state"]
191 names = module.params["name"]
192
193 to_change = []
194 for repo_id in names:
195 if repo_id not in repo_states:
196 module.fail_json(msg=f"did not find repo with ID '{repo_id}' in dnf repolist --all --verbose")
197 if repo_states[repo_id] != desired_repo_state:
198 to_change.append(repo_id)
199 result["changed"] = len(to_change) > 0
200 result["changed_repos"] = to_change
201
202 if module.check_mode:
203 module.exit_json(**result)
204
205 if len(to_change) > 0:
206 set_repo_states(module, to_change, desired_repo_state)
207
208 repo_states_post = get_repo_states(module)
209 result["repo_states_post"] = pack_repo_states_for_return(repo_states_post)
210
211 for repo_id in to_change:
212 if repo_states_post[repo_id] != desired_repo_state:
213 module.fail_json(msg=f"dnf config-manager failed to make '{repo_id}' {desired_repo_state}")
214
215 module.exit_json(**result)
216
217
218 if __name__ == "__main__":
219 main()