diff --git a/changelogs/fragments/382-install_upgrade_specific_args.yaml b/changelogs/fragments/382-install_upgrade_specific_args.yaml new file mode 100644 index 0000000000..19af198ada --- /dev/null +++ b/changelogs/fragments/382-install_upgrade_specific_args.yaml @@ -0,0 +1,2 @@ +minor_changes: + - zypper - Added ``allow_vendor_change`` and ``replacefiles`` zypper options (https://github.com/ansible-collections/community.general/issues/381) diff --git a/plugins/modules/packaging/os/zypper.py b/plugins/modules/packaging/os/zypper.py index 785fbbb3b2..e8b6ae23af 100644 --- a/plugins/modules/packaging/os/zypper.py +++ b/plugins/modules/packaging/os/zypper.py @@ -106,6 +106,18 @@ options: description: - Add additional options to C(zypper) command. - Options should be supplied in a single line as if given in the command line. + allow_vendor_change: + type: bool + required: false + default: false + description: + - Adds C(--allow_vendor_change) option to I(zypper) dist-upgrade command. + replacefiles: + type: bool + required: false + default: false + description: + - Adds C(--replacefiles) option to I(zypper) install/update command. notes: - When used with a `loop:` each package will be processed individually, it is much more efficient to pass the list directly to the `name` option. @@ -164,7 +176,14 @@ EXAMPLES = ''' zypper: name: '*' state: dist-upgrade - extra_args: '--no-allow-vendor-change --allow-arch-change' + allow_vendor_change: true + extra_args: '--allow-arch-change' + +- name: Perform a installaion of nmap with the install option replacefiles + zypper: + name: 'nmap' + state: latest + replacefiles: true - name: Refresh repositories and update package openssl zypper: @@ -188,7 +207,6 @@ EXAMPLES = ''' import xml import re from xml.dom.minidom import parseString as parseXML -from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native # import module snippets @@ -335,6 +353,10 @@ def get_cmd(m, subcommand): cmd.append('--force-resolution') if m.params['oldpackage']: cmd.append('--oldpackage') + if m.params['replacefiles']: + cmd.append('--replacefiles') + if subcommand == 'dist-upgrade' and m.params['allow_vendor_change']: + cmd.append('--allow-vendor-change') if m.params['extra_args']: args_list = m.params['extra_args'].split(' ') cmd.extend(args_list) @@ -478,6 +500,8 @@ def main(): update_cache=dict(required=False, aliases=['refresh'], default='no', type='bool'), oldpackage=dict(required=False, default='no', type='bool'), extra_args=dict(required=False, default=None), + allow_vendor_change=dict(required=False, default=False, type='bool'), + replacefiles=dict(required=False, default=False, type='bool') ), supports_check_mode=True ) diff --git a/tests/integration/targets/zypper/tasks/zypper.yml b/tests/integration/targets/zypper/tasks/zypper.yml index 2b29a96679..1e4cfb5b40 100644 --- a/tests/integration/targets/zypper/tasks/zypper.yml +++ b/tests/integration/targets/zypper/tasks/zypper.yml @@ -419,6 +419,12 @@ - zypper_result_update_cache_check is successful - zypper_result_update_cache_check is not changed +- name: ensure no previous netcat package still exists + zypper: + name: + - netcat-openbsd + - gnu-netcat + state: absent - name: install netcat-openbsd which conflicts with gnu-netcat zypper: @@ -442,3 +448,78 @@ name: gnu-netcat state: present force_resolution: True + +- name: duplicate rpms block + vars: + looplist: + - 1 + - 2 + block: + - name: Deploy spec files to build 2 packages with duplicate files. + template: + src: duplicate.spec.j2 + dest: "{{ output_dir | expanduser }}/zypper2/duplicate{{ item }}.spec" + loop: "{{ looplist }}" + + - name: build rpms with duplicate files + command: | + rpmbuild -bb \ + --define "_topdir {{output_dir | expanduser }}/zypper2/rpm-build" + --define "_builddir %{_topdir}" \ + --define "_rpmdir %{_topdir}" \ + --define "_srcrpmdir %{_topdir}" \ + --define "_specdir {{output_dir | expanduser}}/zypper2" \ + --define "_sourcedir %{_topdir}" \ + {{ output_dir | expanduser }}/zypper2/duplicate{{ item }}.spec + loop: "{{ looplist }}" + + - name: install duplicate rpms + zypper: + name: >- + {{ output_dir | expanduser }}/zypper2/rpm-build/noarch/duplicate{{ item }}-1-0.noarch.rpm + disable_gpg_check: true + ignore_errors: true + register: zypper_duplicate_result + loop: "{{ looplist }}" + + - name: Read in duplicate file contents + slurp: + src: /usr/lib/duplicate/duplicate.txt + register: duplicate_out + + - name: Check failure when installing rpms with duplicate files without replacefiles option + assert: + that: + - zypper_duplicate_result.results[0] is successful + - zypper_duplicate_result.results[1] is failed + - '"fileconflict" in zypper_duplicate_result.results[1].stdout' + - '"/usr/lib/duplicate/duplicate.txt" in zypper_duplicate_result.results[1].stdout' + - '"duplicate1" in duplicate_out.content | b64decode' + + - name: install duplicate rpms + zypper: + name: >- + {{ output_dir | expanduser }}/zypper2/rpm-build/noarch/duplicate{{ item }}-1-0.noarch.rpm + disable_gpg_check: true + replacefiles: true + ignore_errors: true + register: zypper_duplicate_result + loop: "{{ looplist }}" + + - name: Read in duplicate file contents + slurp: + src: /usr/lib/duplicate/duplicate.txt + register: duplicate_out + + - name: Check success installing rpms with duplicate files using replacefiles option + assert: + that: + - zypper_duplicate_result is successful + - zypper_duplicate_result is changed + - '"duplicate2" in duplicate_out.content | b64decode' + + - name: Remove installed duplicate rpms + zypper: + name: "duplicate{{ item }}-1-0" + state: absent + loop: "{{ looplist }}" diff --git a/tests/integration/targets/zypper/templates/duplicate.spec.j2 b/tests/integration/targets/zypper/templates/duplicate.spec.j2 new file mode 100644 index 0000000000..9d1dd56a53 --- /dev/null +++ b/tests/integration/targets/zypper/templates/duplicate.spec.j2 @@ -0,0 +1,18 @@ +Summary: Duplicate{{ item }} RPM. Installs one file that is a duplicate of other Duplicate# RPMs +Name: duplicate{{ item }} +Version: 1 +Release: 0 +License: GPLv3 +Group: Applications/System +BuildArch: noarch + +%description +Duplicate {{ item }} RPM. Package one file that will be a duplicate of other Duplicate RPM contents. +This is only for testing of the replacefiles zypper option. + +%install +mkdir -p "%{buildroot}/usr/lib/duplicate" +echo "%{name}" > "%{buildroot}/usr/lib/duplicate/duplicate.txt" + +%files +/usr/lib/duplicate/duplicate.txt