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

apt: mark installed packages manual (#37751)

* apt: mark installed packages manual

* Add fallback for when manual is not a thing
This commit is contained in:
Martin Krizek 2018-03-23 12:23:12 +01:00 committed by GitHub
parent ca225d6da0
commit 00d9345158
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 2 deletions

View file

@ -275,6 +275,7 @@ APT_GET_ZERO = "\n0 upgraded, 0 newly installed"
APTITUDE_ZERO = "\n0 packages upgraded, 0 newly installed" APTITUDE_ZERO = "\n0 packages upgraded, 0 newly installed"
APT_LISTS_PATH = "/var/lib/apt/lists" APT_LISTS_PATH = "/var/lib/apt/lists"
APT_UPDATE_SUCCESS_STAMP_PATH = "/var/lib/apt/periodic/update-success-stamp" APT_UPDATE_SUCCESS_STAMP_PATH = "/var/lib/apt/periodic/update-success-stamp"
APT_MARK_INVALID_OP = 'Invalid operation'
HAS_PYTHON_APT = True HAS_PYTHON_APT = True
try: try:
@ -469,6 +470,19 @@ def parse_diff(output):
return {'prepared': '\n'.join(diff[diff_start:diff_end])} return {'prepared': '\n'.join(diff[diff_start:diff_end])}
def mark_installed_manually(m, packages):
apt_mark_cmd_path = m.get_bin_path("apt-mark", required=True)
cmd = "%s manual %s" % (apt_mark_cmd_path, ' '.join(packages))
rc, out, err = m.run_command(cmd)
if APT_MARK_INVALID_OP in err:
cmd = "%s unmarkauto %s" % (apt_mark_cmd_path, ' '.join(packages))
rc, out, err = m.run_command(cmd)
if rc != 0:
m.fail_json(msg="'%s' failed: %s" % (cmd, err), stdout=out, stderr=err, rc=rc)
def install(m, pkgspec, cache, upgrade=False, default_release=None, def install(m, pkgspec, cache, upgrade=False, default_release=None,
install_recommends=None, force=False, install_recommends=None, force=False,
dpkg_options=expand_dpkg_options(DPKG_OPTIONS), dpkg_options=expand_dpkg_options(DPKG_OPTIONS),
@ -477,6 +491,7 @@ def install(m, pkgspec, cache, upgrade=False, default_release=None,
pkg_list = [] pkg_list = []
packages = "" packages = ""
pkgspec = expand_pkgspec_from_fnmatches(m, pkgspec, cache) pkgspec = expand_pkgspec_from_fnmatches(m, pkgspec, cache)
package_names = []
for package in pkgspec: for package in pkgspec:
if build_dep: if build_dep:
# Let apt decide what to install # Let apt decide what to install
@ -484,6 +499,7 @@ def install(m, pkgspec, cache, upgrade=False, default_release=None,
continue continue
name, version = package_split(package) name, version = package_split(package)
package_names.append(name)
installed, upgradable, has_files = package_status(m, name, version, cache, state='install') installed, upgradable, has_files = package_status(m, name, version, cache, state='install')
if (not installed and not only_upgrade) or (upgrade and upgradable): if (not installed and not only_upgrade) or (upgrade and upgradable):
pkg_list.append("'%s'" % package) pkg_list.append("'%s'" % package)
@ -545,9 +561,14 @@ def install(m, pkgspec, cache, upgrade=False, default_release=None,
if rc: if rc:
status = False status = False
data = dict(msg="'%s' failed: %s" % (cmd, err), stdout=out, stderr=err, rc=rc) data = dict(msg="'%s' failed: %s" % (cmd, err), stdout=out, stderr=err, rc=rc)
return (status, data)
else: else:
return (True, dict(changed=False)) status = True
data = dict(changed=False)
if not build_dep:
mark_installed_manually(m, package_names)
return (status, data)
def get_field_of_deb(m, deb_file, field="Version"): def get_field_of_deb(m, deb_file, field="Version"):

View file

@ -185,6 +185,41 @@
name: /etc/apt/sources.list.d/non-existing.list name: /etc/apt/sources.list.d/non-existing.list
state: absent state: absent
# https://github.com/ansible/ansible/issues/28907
- name: Install parent package
apt:
name: libcaca-dev
- name: Install child package
apt:
name: libslang2-dev
- shell: apt-mark showmanual | grep libcaca-dev
ignore_errors: yes
register: parent_output
- name: Check that parent package is marked as installed manually
assert:
that:
- "'libcaca-dev' in parent_output.stdout"
- shell: apt-mark showmanual | grep libslang2-dev
ignore_errors: yes
register: child_output
- name: Check that child package is marked as installed manually
assert:
that:
- "'libslang2-dev' in child_output.stdout"
- name: Clean up
apt:
name: "{{ item }}"
state: absent
with_items:
- libcaca-dev
- libslang2-dev
- name: autoclean during install - name: autoclean during install
apt: pkg=hello state=present autoclean=yes apt: pkg=hello state=present autoclean=yes