diff --git a/changelogs/fragments/8783-preserve-parameters-provided-from-task-in-zypper-module.yaml b/changelogs/fragments/8783-preserve-parameters-provided-from-task-in-zypper-module.yaml new file mode 100644 index 00000000000..aab575a0809 --- /dev/null +++ b/changelogs/fragments/8783-preserve-parameters-provided-from-task-in-zypper-module.yaml @@ -0,0 +1,2 @@ +bugfixes: + - zypper-module - only read attributes ``enabled``, ``disable_gpg_check`` and ``autorefresh`` from local repo files, if the parameters were not already provided by the task parameters (https://github.com/ansible-collections/community.general/pull/9108). diff --git a/plugins/modules/zypper_repository.py b/plugins/modules/zypper_repository.py index 5a0356cc37b..716fd6e5fa5 100644 --- a/plugins/modules/zypper_repository.py +++ b/plugins/modules/zypper_repository.py @@ -356,19 +356,6 @@ def main(): 'name': module.params['description'], 'priority': module.params['priority'], } - # rewrite bools in the language that zypper lr -x provides for easier comparison - if module.params['enabled']: - repodata['enabled'] = '1' - else: - repodata['enabled'] = '0' - if module.params['disable_gpg_check']: - repodata['gpgcheck'] = '0' - else: - repodata['gpgcheck'] = '1' - if module.params['autorefresh']: - repodata['autorefresh'] = '1' - else: - repodata['autorefresh'] = '0' def exit_unchanged(): module.exit_json(changed=False, repodata=repodata, state=state) @@ -393,6 +380,14 @@ def exit_unchanged(): if not alias and state == "present": module.fail_json(msg='Name required when adding non-repo files.') + # fill boolean attributes with defaults + if 'enabled' not in repodata: + repodata['enabled'] = '0' + if 'autorefresh' not in repodata: + repodata['autorefresh'] = '0' + if 'gpgcheck' not in repodata: + repodata['gpgcheck'] = '0' + # Download / Open and parse .repo file to ensure idempotency if repo and repo.endswith('.repo'): if repo.startswith(('http://', 'https://')): @@ -443,6 +438,12 @@ def exit_unchanged(): if 'gpgcheck' in repofile_items: repodata['gpgcheck'] = repofile_items['gpgcheck'] + # override boolean parameters in repodata with provided entries in module.params + # in the language that zypper lr -x provides for easier comparison + repodata['enabled'] = '1' if module.params['enabled'] else '0' + repodata['gpgcheck'] = '0' if module.params['disable_gpg_check'] else '1' + repodata['autorefresh'] = '1' if module.params['autorefresh'] else '0' + exists, mod, old_repos = repo_exists(module, repodata, overwrite_multiple) if alias: diff --git a/tests/integration/targets/zypper_repository/tasks/zypper_repository.yml b/tests/integration/targets/zypper_repository/tasks/zypper_repository.yml index ec362af1088..1d6f83dedeb 100644 --- a/tests/integration/targets/zypper_repository/tasks/zypper_repository.yml +++ b/tests/integration/targets/zypper_repository/tasks/zypper_repository.yml @@ -246,6 +246,44 @@ that: - added_again_by_repo_file is not changed + - name: change the repository config by name with flag - disable + community.general.zypper_repository: + name: systemsmanagement_Uyuni_Stable + state: present + enabled: false + register: modified_repo_file_result + + - name: modified_repo_file_result is changed + assert: + that: + - modified_repo_file_result is changed + + - name: get repository details again from zypper after disabling the repository + command: zypper --xmlout lr systemsmanagement_Uyuni_Stable + register: get_repository_details_from_zypper + + - name: verify modifying via .repo file was successful - the module is now disabled + assert: + that: + - "'enabled=\"0\"' in get_repository_details_from_zypper.stdout" + + - name: change flag autorefresh in the same zypper-module + community.general.zypper_repository: + name: systemsmanagement_Uyuni_Stable + state: present + autorefresh: false + register: modified_repo_file_result + + - name: get repository details again from zypper after disabling the repository + command: zypper --xmlout lr systemsmanagement_Uyuni_Stable + register: get_repository_details_from_zypper + + - name: verify modifying via .repo file was successful - the autorefesh is disabled, but the enabled-flag was not modified (it is still disabled) + assert: + that: + - "'enabled=\"0\"' in get_repository_details_from_zypper.stdout" + - "'autorefresh=\"0\"' in get_repository_details_from_zypper.stdout" + - name: remove repository via url to .repo file community.general.zypper_repository: repo: http://download.opensuse.org/repositories/systemsmanagement:/Uyuni:/Stable/openSUSE_Leap_{{ ansible_distribution_version }}/systemsmanagement:Uyuni:Stable.repo