mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-06-16 12:57:40 +00:00
Implement support for DNF5
This commit is contained in:
parent
d3358053a6
commit
3ba3c15e3e
9 changed files with 863 additions and 53 deletions
|
|
@ -125,12 +125,27 @@ import re
|
|||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
DNF_BIN = "/usr/bin/dnf"
|
||||
REPO_ID_RE = re.compile(r"^Repo-id\s*:\s*(\S+)$")
|
||||
REPO_STATUS_RE = re.compile(r"^Repo-status\s*:\s*(disabled|enabled)$")
|
||||
REPO_ID_RE = re.compile(r"^Repo[-\s]id\s*:\s*(\S+)$", re.IGNORECASE)
|
||||
REPO_STATUS_RE = re.compile(r"^(?:Repo-)?status\s*:\s*(disabled|enabled)$", re.IGNORECASE)
|
||||
|
||||
|
||||
def get_repo_states(module):
|
||||
rc, out, err = module.run_command([DNF_BIN, "repolist", "--all", "--verbose"], check_rc=True)
|
||||
def get_dnf_version(module) -> 4 | 5:
|
||||
rc, out, err = module.run_command([DNF_BIN, "--version"], check_rc=True)
|
||||
line, separator, rest = out.partition("\n")
|
||||
if re.compile(r"^dnf5\s*").match(line):
|
||||
return 5
|
||||
else:
|
||||
return 4
|
||||
|
||||
|
||||
def get_repo_states(module, dnf_v):
|
||||
command = [DNF_BIN]
|
||||
if dnf_v == 4:
|
||||
command.extend(["repolist", "--all", "--verbose"])
|
||||
else:
|
||||
command.extend(["repo", "info", "--all"])
|
||||
|
||||
rc, out, err = module.run_command(command, check_rc=True)
|
||||
|
||||
repos = dict()
|
||||
last_repo = ""
|
||||
|
|
@ -150,8 +165,13 @@ def get_repo_states(module):
|
|||
return repos
|
||||
|
||||
|
||||
def set_repo_states(module, repo_ids, state):
|
||||
module.run_command([DNF_BIN, "config-manager", "--assumeyes", f"--set-{state}"] + repo_ids, check_rc=True)
|
||||
def set_repo_states(module, dnf_v, repo_ids, state):
|
||||
if dnf_v == 4:
|
||||
module.run_command([DNF_BIN, "config-manager", "--assumeyes", f"--set-{state}"] + repo_ids, check_rc=True)
|
||||
else:
|
||||
state = "1" if state == "enabled" else "0"
|
||||
opts = map(lambda v: f"{v}.enabled={state}", repo_ids)
|
||||
module.run_command([DNF_BIN, "config-manager", "setopt"] + list(opts), check_rc=True)
|
||||
|
||||
|
||||
def pack_repo_states_for_return(states):
|
||||
|
|
@ -184,7 +204,9 @@ def main():
|
|||
if not os.path.exists(DNF_BIN):
|
||||
module.fail_json(msg=f"{DNF_BIN} was not found")
|
||||
|
||||
repo_states = get_repo_states(module)
|
||||
dnf_v = get_dnf_version(module)
|
||||
|
||||
repo_states = get_repo_states(module, dnf_v)
|
||||
result["repo_states_pre"] = pack_repo_states_for_return(repo_states)
|
||||
|
||||
desired_repo_state = module.params["state"]
|
||||
|
|
@ -203,9 +225,9 @@ def main():
|
|||
module.exit_json(**result)
|
||||
|
||||
if len(to_change) > 0:
|
||||
set_repo_states(module, to_change, desired_repo_state)
|
||||
set_repo_states(module, dnf_v, to_change, desired_repo_state)
|
||||
|
||||
repo_states_post = get_repo_states(module)
|
||||
repo_states_post = get_repo_states(module, dnf_v)
|
||||
result["repo_states_post"] = pack_repo_states_for_return(repo_states_post)
|
||||
|
||||
for repo_id in to_change:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue