Add cacheonly option to yum and dnf modules (#73820)

* Add integration test

* Add changelog

Co-authored-by: Matt Clay <matt@mystile.com>
This commit is contained in:
Amin Vakil 2021-04-16 19:38:52 +04:30 committed by GitHub
parent 0c101f3f76
commit fdee5ca16d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 0 deletions

View file

@ -0,0 +1,4 @@
---
minor_changes:
- yum - Add ``cacheonly`` option (https://github.com/ansible/ansible/issues/69397).
- dnf - Add ``cacheonly`` option (https://github.com/ansible/ansible/issues/69397).

View file

@ -26,6 +26,7 @@ yumdnf_argument_spec = dict(
allow_downgrade=dict(type='bool', default=False), allow_downgrade=dict(type='bool', default=False),
autoremove=dict(type='bool', default=False), autoremove=dict(type='bool', default=False),
bugfix=dict(required=False, type='bool', default=False), bugfix=dict(required=False, type='bool', default=False),
cacheonly=dict(type='bool', default=False),
conf_file=dict(type='str'), conf_file=dict(type='str'),
disable_excludes=dict(type='str', default=None), disable_excludes=dict(type='str', default=None),
disable_gpg_check=dict(type='bool', default=False), disable_gpg_check=dict(type='bool', default=False),
@ -71,6 +72,7 @@ class YumDnf(with_metaclass(ABCMeta, object)):
self.allow_downgrade = self.module.params['allow_downgrade'] self.allow_downgrade = self.module.params['allow_downgrade']
self.autoremove = self.module.params['autoremove'] self.autoremove = self.module.params['autoremove']
self.bugfix = self.module.params['bugfix'] self.bugfix = self.module.params['bugfix']
self.cacheonly = self.module.params['cacheonly']
self.conf_file = self.module.params['conf_file'] self.conf_file = self.module.params['conf_file']
self.disable_excludes = self.module.params['disable_excludes'] self.disable_excludes = self.module.params['disable_excludes']
self.disable_gpg_check = self.module.params['disable_gpg_check'] self.disable_gpg_check = self.module.params['disable_gpg_check']

View file

@ -228,6 +228,12 @@ options:
type: bool type: bool
default: "no" default: "no"
version_added: "2.11" version_added: "2.11"
cacheonly:
description:
- Tells dnf to run entirely from system cache; does not download or update metadata.
type: bool
default: "no"
version_added: "2.12"
notes: 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. - 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.
- Group removal doesn't work if the group was installed with Ansible because - Group removal doesn't work if the group was installed with Ansible because
@ -609,6 +615,9 @@ class DnfModule(YumDnf):
if self.download_dir: if self.download_dir:
conf.destdir = self.download_dir conf.destdir = self.download_dir
if self.cacheonly:
conf.cacheonly = True
# Default in dnf upstream is true # Default in dnf upstream is true
conf.clean_requirements_on_remove = self.autoremove conf.clean_requirements_on_remove = self.autoremove

View file

@ -236,6 +236,12 @@ options:
version_added: "1.5" version_added: "1.5"
default: "yes" default: "yes"
type: bool type: bool
cacheonly:
description:
- Tells yum to run entirely from system cache; does not download or update metadata.
default: "no"
type: bool
version_added: "2.12"
notes: notes:
- When used with a `loop:` each package will be processed individually, - 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. it is much more efficient to pass the list directly to the `name` option.
@ -1558,6 +1564,9 @@ class YumModule(YumDnf):
if self.disable_excludes: if self.disable_excludes:
self.yum_basecmd.extend(['--disableexcludes=%s' % self.disable_excludes]) self.yum_basecmd.extend(['--disableexcludes=%s' % self.disable_excludes])
if self.cacheonly:
self.yum_basecmd.extend(['--cacheonly'])
if self.download_only: if self.download_only:
self.yum_basecmd.extend(['--downloadonly']) self.yum_basecmd.extend(['--downloadonly'])

View file

@ -0,0 +1,15 @@
---
- name: Test cacheonly (clean before testing)
command: dnf clean all
- name: Try installing from cache where it has been cleaned
dnf:
name: sos
state: latest
cacheonly: true
register: dnf_result
- name: Verify dnf has not changed
assert:
that:
- "not dnf_result is changed"

View file

@ -66,3 +66,7 @@
- include_tasks: nobest.yml - include_tasks: nobest.yml
when: (ansible_distribution == 'Fedora' and ansible_distribution_major_version is version('24', '>=')) or when: (ansible_distribution == 'Fedora' and ansible_distribution_major_version is version('24', '>=')) or
(ansible_distribution in ['RedHat', 'CentOS'] and ansible_distribution_major_version is version('8', '>=')) (ansible_distribution in ['RedHat', 'CentOS'] and ansible_distribution_major_version is version('8', '>='))
- include_tasks: cacheonly.yml
when: (ansible_distribution == 'Fedora' and ansible_distribution_major_version is version('23', '>=')) or
(ansible_distribution in ['RedHat', 'CentOS'] and ansible_distribution_major_version is version('8', '>='))

View file

@ -0,0 +1,16 @@
---
- name: Test cacheonly (clean before testing)
command: yum clean all
- name: Try installing from cache where it has been cleaned
yum:
name: sos
state: latest
cacheonly: true
register: yum_result
ignore_errors: true
- name: Verify yum failure
assert:
that:
- "yum_result is failed"

View file

@ -76,3 +76,7 @@
- ansible_architecture == 'x86_64' - ansible_architecture == 'x86_64'
# Our output parsing expects us to be on yum, not dnf # Our output parsing expects us to be on yum, not dnf
- ansible_distribution_major_version is version('7', '<=') - ansible_distribution_major_version is version('7', '<=')
- import_tasks: cacheonly.yml
when:
- ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux', 'Fedora']