1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

zypper_repository: fix idempotency on adding repo with releasever and basearch variables (#2722) (#2753)

* zypper_repository: Check idempotency on adding repo with releasever

* Name required when adding non-repo files.

* Initial try to fix releasever

* Replace re.sub with .replace

* name releaseverrepo releaseverrepo

* Change  to ansible_distribution_version for removing repo

* improve asserts format

* add changelog

* Fix changelog formatting

Co-authored-by: Felix Fontein <felix@fontein.de>

* improve command used for retrieving releasever variable

Co-authored-by: Felix Fontein <felix@fontein.de>

* add basearch replace

* Add basearch to changelog fragment

* Check for releasever and basearch only when they are there

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 94a53adff1)

Co-authored-by: Amin Vakil <info@aminvakil.com>
This commit is contained in:
patchback[bot] 2021-06-08 08:47:15 +02:00 committed by GitHub
parent 42a1318fe3
commit dbc0fe8859
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
bugfixes:
- zypper_repository - fix idempotency on adding repository with
``$releasever`` and ``$basearch`` variables
(https://github.com/ansible-collections/community.general/issues/1985).

View file

@ -175,7 +175,7 @@ def _parse_repos(module):
module.fail_json(msg='Failed to execute "%s"' % " ".join(cmd), rc=rc, stdout=stdout, stderr=stderr)
def _repo_changes(realrepo, repocmp):
def _repo_changes(module, realrepo, repocmp):
"Check whether the 2 given repos have different settings."
for k in repocmp:
if repocmp[k] and k not in realrepo:
@ -186,6 +186,16 @@ def _repo_changes(realrepo, repocmp):
valold = str(repocmp[k] or "")
valnew = v or ""
if k == "url":
if '$releasever' in valold or '$releasever' in valnew:
cmd = ['rpm', '-q', '--qf', '%{version}', '-f', '/etc/os-release']
rc, stdout, stderr = module.run_command(cmd, check_rc=True)
valnew = valnew.replace('$releasever', stdout)
valold = valold.replace('$releasever', stdout)
if '$basearch' in valold or '$basearch' in valnew:
cmd = ['rpm', '-q', '--qf', '%{arch}', '-f', '/etc/os-release']
rc, stdout, stderr = module.run_command(cmd, check_rc=True)
valnew = valnew.replace('$basearch', stdout)
valold = valold.replace('$basearch', stdout)
valold, valnew = valold.rstrip("/"), valnew.rstrip("/")
if valold != valnew:
return True
@ -215,7 +225,7 @@ def repo_exists(module, repodata, overwrite_multiple):
return (False, False, None)
elif len(repos) == 1:
# Found an existing repo, look for changes
has_changes = _repo_changes(repos[0], repodata)
has_changes = _repo_changes(module, repos[0], repodata)
return (True, has_changes, repos)
elif len(repos) >= 2:
if overwrite_multiple:

View file

@ -125,3 +125,61 @@
priority: 100
auto_import_keys: true
state: "present"
- name: add a repo by releasever
community.general.zypper_repository:
name: releaseverrepo
repo: http://download.opensuse.org/repositories/devel:/languages:/ruby/openSUSE_Leap_$releasever/
state: present
register: add_repo
- name: add a repo by releasever again
community.general.zypper_repository:
name: releaseverrepo
repo: http://download.opensuse.org/repositories/devel:/languages:/ruby/openSUSE_Leap_$releasever/
state: present
register: add_repo_again
- assert:
that:
- add_repo is changed
- add_repo_again is not changed
- name: remove added repo
community.general.zypper_repository:
repo: http://download.opensuse.org/repositories/devel:/languages:/ruby/openSUSE_Leap_{{ ansible_distribution_version }}/
state: absent
register: remove_repo
- assert:
that:
- remove_repo is changed
- name: add a repo by basearch
community.general.zypper_repository:
name: basearchrepo
repo: https://packagecloud.io/netdata/netdata/opensuse/13.2/$basearch
state: present
register: add_repo
- name: add a repo by basearch again
community.general.zypper_repository:
name: basearchrepo
repo: https://packagecloud.io/netdata/netdata/opensuse/13.2/$basearch
state: present
register: add_repo_again
- assert:
that:
- add_repo is changed
- add_repo_again is not changed
- name: remove added repo
community.general.zypper_repository:
repo: https://packagecloud.io/netdata/netdata/opensuse/13.2/x86_64
state: absent
register: remove_repo
- assert:
that:
- remove_repo is changed