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

yum_versionlock: fix idempotency when using wildcard (asterisk) (#2787)

* Check idempotency on yum_versionlock

* Lock packages wildcard

* fix formatting

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

* Fix formatting in asserts

* little closer but not still there

* Import fnmatch

* Change check_mode logic

* Add check_mode for add

* Add changelog

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Amin Vakil 2021-06-27 19:10:49 +04:30 committed by GitHub
parent c7cf6f2eb7
commit 0a9cf38118
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 14 deletions

View file

@ -0,0 +1,3 @@
---
bugfixes:
- yum_versionlock - fix idempotency when using wildcard (asterisk) in ``name`` option (https://github.com/ansible-collections/community.general/issues/2761).

View file

@ -76,6 +76,7 @@ state:
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
from fnmatch import fnmatch
class YumVersionLock: class YumVersionLock:
@ -125,22 +126,22 @@ def main():
if state in ('present'): if state in ('present'):
command = 'add' command = 'add'
for single_pkg in packages: for single_pkg in packages:
if single_pkg not in versionlock_packages: if not any(fnmatch(pkg.split(":", 1)[-1], single_pkg) for pkg in versionlock_packages.split()):
if module.check_mode:
changed = True
continue
packages_list.append(single_pkg) packages_list.append(single_pkg)
if packages_list: if packages_list:
if module.check_mode:
changed = True
else:
changed = yum_v.ensure_state(packages_list, command) changed = yum_v.ensure_state(packages_list, command)
elif state in ('absent'): elif state in ('absent'):
command = 'delete' command = 'delete'
for single_pkg in packages: for single_pkg in packages:
if single_pkg in versionlock_packages: if any(fnmatch(pkg, single_pkg) for pkg in versionlock_packages.split()):
if module.check_mode:
changed = True
continue
packages_list.append(single_pkg) packages_list.append(single_pkg)
if packages_list: if packages_list:
if module.check_mode:
changed = True
else:
changed = yum_v.ensure_state(packages_list, command) changed = yum_v.ensure_state(packages_list, command)
module.exit_json( module.exit_json(

View file

@ -29,6 +29,18 @@
state: present state: present
register: lock_all_packages register: lock_all_packages
- name: Lock all packages again
community.general.yum_versionlock:
name: "{{ yum_updates.results | map(attribute='name') | list }}"
state: present
register: lock_all_packages_again
- name: Lock packages wildcard
community.general.yum_versionlock:
name: "nss*"
state: present
register: lock_nss_wildcard
# This should fail when it needs user interaction and missing -y is on purpose. # This should fail when it needs user interaction and missing -y is on purpose.
- name: Update all packages (not really) - name: Update all packages (not really)
command: yum update --setopt=obsoletes=0 command: yum update --setopt=obsoletes=0
@ -54,10 +66,12 @@
- name: Assert everything is fine - name: Assert everything is fine
assert: assert:
that: that:
- "{{ lock_all_packages.changed }}" - lock_all_packages is changed
- "{{ not update_all_locked_packages.changed }}" - lock_all_packages_again is not changed
- "{{ unlock_all_packages.changed }}" - lock_nss_wildcard is not changed
- "{{ update_all_packages.changed }}" - update_all_locked_packages is not changed
- unlock_all_packages is changed
- update_all_packages is changed
when: yum_updates.results | length != 0 when: yum_updates.results | length != 0
- name: Remove installed packages in case it was not installed - name: Remove installed packages in case it was not installed