apt: honor cache_update=false on auto-install deps (#56549)

* apt: honor cache_update=false on auto-install deps
* add porting guide
This commit is contained in:
René Moser 2019-08-30 21:57:58 +02:00 committed by Sam Doran
parent 01ae6991bd
commit fd4ff54580
4 changed files with 69 additions and 6 deletions

View file

@ -0,0 +1,4 @@
---
bugfixes:
- apt - Fixed the issue the cache being updated while auto-installing its
dependencies even when ``update_cache`` is set to false.

View file

@ -61,7 +61,7 @@ Modules
=======
* The ``win_get_url`` and ``win_uri`` module now sends requests with a default ``User-Agent`` of ``ansible-httpget``. This can be changed by using the ``http_agent`` key.
* The ``apt`` module now honors ``update_cache=false`` while installing its own dependency and skips the cache update. Explicitly setting ``update_cache=true`` or omitting the param ``update_cache`` will result in a cache update while installing its own dependency.
Writing modules
---------------

View file

@ -1047,8 +1047,14 @@ def main():
module.fail_json(msg="%s must be installed to use check mode. "
"If run normally this module can auto-install it." % PYTHON_APT)
try:
module.warn("Updating cache and auto-installing missing dependency: %s" % PYTHON_APT)
module.run_command(['apt-get', 'update'], check_rc=True)
# We skip cache update in auto install the dependency if the
# user explicitly declared it with update_cache=no.
if module.params.get('update_cache') is False:
module.warn("Auto-installing missing dependency without updating cache: %s" % PYTHON_APT)
else:
module.warn("Updating cache and auto-installing missing dependency: %s" % PYTHON_APT)
module.run_command(['apt-get', 'update'], check_rc=True)
module.run_command(['apt-get', 'install', '--no-install-recommends', PYTHON_APT, '-y', '-q'], check_rc=True)
global apt, apt_pkg
import apt

View file

@ -31,6 +31,57 @@
register: apt_result
when: dpkg_result is successful
# In check mode, auto-install of `python-apt` must fail
- name: test fail uninstall hello without required apt deps in check mode
apt:
pkg: hello
state: absent
purge: yes
register: apt_result
check_mode: yes
ignore_errors: yes
- name: verify fail uninstall hello without required apt deps in check mode
assert:
that:
- apt_result is failed
- '"If run normally this module can auto-install it." in apt_result.msg'
- name: check {{ python_apt }} with dpkg
shell: dpkg -s {{ python_apt }}
register: dpkg_result
ignore_errors: true
# UNINSTALL 'hello'
# With 'python-apt' uninstalled, the first call to 'apt' should install
# python-apt without updating the cache.
- name: uninstall hello with apt and prevent updating the cache
apt:
pkg: hello
state: absent
purge: yes
update_cache: no
register: apt_result
- name: check hello with dpkg
shell: dpkg-query -l hello
failed_when: False
register: dpkg_result
- name: verify uninstall hello with apt and prevent updating the cache
assert:
that:
- "'changed' in apt_result"
- apt_result is not changed
- "dpkg_result.rc == 1"
- "'Auto-installing missing dependency without updating cache: {{ python_apt }}' in apt_result.warnings"
- name: uninstall {{ python_apt }} with apt again
apt:
pkg: "{{ python_apt }}"
state: absent
purge: yes
# UNINSTALL 'hello'
# With 'python-apt' uninstalled, the first call to 'apt' should install
# python-apt.
@ -46,8 +97,10 @@
- name: verify uninstallation of hello
assert:
that:
- "'changed' in apt_result"
- "dpkg_result.rc == 1"
- "'changed' in apt_result"
- apt_result is not changed
- "dpkg_result.rc == 1"
- "'Updating cache and auto-installing missing dependency: {{ python_apt }}' in apt_result.warnings"
# UNINSTALL AGAIN
- name: uninstall hello with apt
@ -259,7 +312,7 @@
that:
- apt_result is not changed
# check policy_rc_d parameter
# check policy_rc_d parameter
- name: Install unscd but forbid service start
apt: