2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2013, Evgenii Terechkov
|
2020-03-09 10:11:07 +01:00
|
|
|
# Written by Evgenii Terechkov <evg@altlinux.org>
|
|
|
|
# Based on urpmi module written by Philippe Makowski <philippem@mageia.org>
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: apt_rpm
|
2022-11-09 07:18:40 +01:00
|
|
|
short_description: APT-RPM package manager
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-06-15 19:04:07 +02:00
|
|
|
- Manages packages with C(apt-rpm). Both low-level (C(rpm)) and high-level (C(apt-get)) package manager binaries required.
|
2023-02-24 09:25:20 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
2020-06-17 18:04:27 +02:00
|
|
|
package:
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-03-25 08:22:33 +01:00
|
|
|
- List of packages to install, upgrade, or remove.
|
2020-06-17 18:04:27 +02:00
|
|
|
aliases: [ name, pkg ]
|
|
|
|
type: list
|
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicates the desired package state.
|
2020-11-13 13:41:11 +01:00
|
|
|
choices: [ absent, present, installed, removed ]
|
2020-03-09 10:11:07 +01:00
|
|
|
default: present
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
update_cache:
|
|
|
|
description:
|
2023-03-25 08:22:33 +01:00
|
|
|
- Run the equivalent of C(apt-get update) before the operation. Can be run as part of the package installation or as a separate step.
|
|
|
|
- Default is not to update the cache.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 19:59:13 +02:00
|
|
|
default: false
|
2023-03-25 08:22:33 +01:00
|
|
|
clean:
|
|
|
|
description:
|
|
|
|
- Run the equivalent of C(apt-get clean) to clear out the local repository of retrieved package files. It removes everything but
|
|
|
|
the lock file from C(/var/cache/apt/archives/) and C(/var/cache/apt/archives/partial/).
|
|
|
|
- Can be run as part of the package installation (clean runs before install) or as a separate step.
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
version_added: 6.5.0
|
|
|
|
dist_upgrade:
|
|
|
|
description:
|
|
|
|
- If true performs an C(apt-get dist-upgrade) to upgrade system.
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
version_added: 6.5.0
|
|
|
|
update_kernel:
|
|
|
|
description:
|
|
|
|
- If true performs an C(update-kernel) to upgrade kernel packages.
|
|
|
|
type: bool
|
|
|
|
default: false
|
|
|
|
version_added: 6.5.0
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
|
|
|
- Evgenii Terechkov (@evgkrsk)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Install package foo
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.apt_rpm:
|
2020-03-09 10:11:07 +01:00
|
|
|
pkg: foo
|
|
|
|
state: present
|
|
|
|
|
2020-06-17 18:04:27 +02:00
|
|
|
- name: Install packages foo and bar
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.apt_rpm:
|
2020-06-17 18:04:27 +02:00
|
|
|
pkg:
|
|
|
|
- foo
|
|
|
|
- bar
|
|
|
|
state: present
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: Remove package foo
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.apt_rpm:
|
2020-03-09 10:11:07 +01:00
|
|
|
pkg: foo
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: Remove packages foo and bar
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.apt_rpm:
|
2020-03-09 10:11:07 +01:00
|
|
|
pkg: foo,bar
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
# bar will be the updated if a newer version exists
|
|
|
|
- name: Update the package database and install bar
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.apt_rpm:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: bar
|
|
|
|
state: present
|
2022-08-24 19:59:13 +02:00
|
|
|
update_cache: true
|
2023-03-25 08:22:33 +01:00
|
|
|
|
|
|
|
- name: Run the equivalent of "apt-get clean" as a separate step
|
|
|
|
community.general.apt_rpm:
|
|
|
|
clean: true
|
|
|
|
|
|
|
|
- name: Perform cache update and complete system upgrade (includes kernel)
|
|
|
|
community.general.apt_rpm:
|
|
|
|
update_cache: true
|
|
|
|
dist_upgrade: true
|
|
|
|
update_kernel: true
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
APT_PATH = "/usr/bin/apt-get"
|
|
|
|
RPM_PATH = "/usr/bin/rpm"
|
2023-03-25 08:22:33 +01:00
|
|
|
APT_GET_ZERO = "\n0 upgraded, 0 newly installed"
|
|
|
|
UPDATE_KERNEL_ZERO = "\nTry to install new kernel "
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def query_package(module, name):
|
|
|
|
# rpm -q returns 0 if the package is installed,
|
|
|
|
# 1 if it is not installed
|
|
|
|
rc, out, err = module.run_command("%s -q %s" % (RPM_PATH, name))
|
|
|
|
if rc == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def query_package_provides(module, name):
|
|
|
|
# rpm -q returns 0 if the package is installed,
|
|
|
|
# 1 if it is not installed
|
|
|
|
rc, out, err = module.run_command("%s -q --provides %s" % (RPM_PATH, name))
|
|
|
|
return rc == 0
|
|
|
|
|
|
|
|
|
|
|
|
def update_package_db(module):
|
2023-03-25 08:22:33 +01:00
|
|
|
rc, update_out, err = module.run_command([APT_PATH, "update"], check_rc=True, environ_update={"LANG": "C"})
|
|
|
|
return (False, update_out)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
|
|
|
|
def dir_size(module, path):
|
|
|
|
total_size = 0
|
|
|
|
for path, dirs, files in os.walk(path):
|
|
|
|
for f in files:
|
|
|
|
total_size += os.path.getsize(os.path.join(path, f))
|
|
|
|
return total_size
|
|
|
|
|
|
|
|
|
|
|
|
def clean(module):
|
|
|
|
t = dir_size(module, "/var/cache/apt/archives")
|
|
|
|
rc, out, err = module.run_command([APT_PATH, "clean"], check_rc=True)
|
|
|
|
return (t != dir_size(module, "/var/cache/apt/archives"), out)
|
|
|
|
|
|
|
|
|
|
|
|
def dist_upgrade(module):
|
|
|
|
rc, out, err = module.run_command([APT_PATH, "-y", "dist-upgrade"], check_rc=True, environ_update={"LANG": "C"})
|
|
|
|
return (APT_GET_ZERO not in out, out)
|
|
|
|
|
|
|
|
|
|
|
|
def update_kernel(module):
|
|
|
|
rc, out, err = module.run_command(["/usr/sbin/update-kernel", "-y"], check_rc=True, environ_update={"LANG": "C"})
|
|
|
|
return (UPDATE_KERNEL_ZERO not in out, out)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def remove_packages(module, packages):
|
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
if packages is None:
|
|
|
|
return (False, "Empty package list")
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
remove_c = 0
|
|
|
|
# Using a for loop in case of error, we can report the package that failed
|
|
|
|
for package in packages:
|
|
|
|
# Query the package first, to see if we even need to remove
|
|
|
|
if not query_package(module, package):
|
|
|
|
continue
|
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
rc, out, err = module.run_command("%s -y remove %s" % (APT_PATH, package), environ_update={"LANG": "C"})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="failed to remove %s: %s" % (package, err))
|
|
|
|
|
|
|
|
remove_c += 1
|
|
|
|
|
|
|
|
if remove_c > 0:
|
2023-03-25 08:22:33 +01:00
|
|
|
return (True, "removed %s package(s)" % remove_c)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
return (False, "package(s) already absent")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def install_packages(module, pkgspec):
|
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
if pkgspec is None:
|
|
|
|
return (False, "Empty package list")
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
packages = ""
|
|
|
|
for package in pkgspec:
|
|
|
|
if not query_package_provides(module, package):
|
|
|
|
packages += "'%s' " % package
|
|
|
|
|
|
|
|
if len(packages) != 0:
|
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
rc, out, err = module.run_command("%s -y install %s" % (APT_PATH, packages), environ_update={"LANG": "C"})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
installed = True
|
2024-04-22 06:53:24 +02:00
|
|
|
for package in pkgspec:
|
2020-03-09 10:11:07 +01:00
|
|
|
if not query_package_provides(module, package):
|
|
|
|
installed = False
|
|
|
|
|
|
|
|
# apt-rpm always have 0 for exit code if --force is used
|
|
|
|
if rc or not installed:
|
|
|
|
module.fail_json(msg="'apt-get -y install %s' failed: %s" % (packages, err))
|
|
|
|
else:
|
2023-03-25 08:22:33 +01:00
|
|
|
return (True, "%s present(s)" % packages)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2023-03-25 08:22:33 +01:00
|
|
|
return (False, "Nothing to install")
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2020-06-26 15:17:11 +02:00
|
|
|
state=dict(type='str', default='present', choices=['absent', 'installed', 'present', 'removed']),
|
2022-04-26 11:45:15 +02:00
|
|
|
update_cache=dict(type='bool', default=False),
|
2023-03-25 08:22:33 +01:00
|
|
|
clean=dict(type='bool', default=False),
|
|
|
|
dist_upgrade=dict(type='bool', default=False),
|
|
|
|
update_kernel=dict(type='bool', default=False),
|
|
|
|
package=dict(type='list', elements='str', aliases=['name', 'pkg']),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
if not os.path.exists(APT_PATH) or not os.path.exists(RPM_PATH):
|
|
|
|
module.fail_json(msg="cannot find /usr/bin/apt-get and/or /usr/bin/rpm")
|
|
|
|
|
|
|
|
p = module.params
|
2023-03-25 08:22:33 +01:00
|
|
|
modified = False
|
|
|
|
output = ""
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if p['update_cache']:
|
|
|
|
update_package_db(module)
|
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
if p['clean']:
|
|
|
|
(m, out) = clean(module)
|
|
|
|
modified = modified or m
|
|
|
|
|
|
|
|
if p['dist_upgrade']:
|
|
|
|
(m, out) = dist_upgrade(module)
|
|
|
|
modified = modified or m
|
|
|
|
output += out
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
if p['update_kernel']:
|
|
|
|
(m, out) = update_kernel(module)
|
|
|
|
modified = modified or m
|
|
|
|
output += out
|
|
|
|
|
|
|
|
packages = p['package']
|
2020-03-09 10:11:07 +01:00
|
|
|
if p['state'] in ['installed', 'present']:
|
2023-03-25 08:22:33 +01:00
|
|
|
(m, out) = install_packages(module, packages)
|
|
|
|
modified = modified or m
|
|
|
|
output += out
|
|
|
|
|
|
|
|
if p['state'] in ['absent', 'removed']:
|
|
|
|
(m, out) = remove_packages(module, packages)
|
|
|
|
modified = modified or m
|
|
|
|
output += out
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-03-25 08:22:33 +01:00
|
|
|
# Return total modification status and output of all commands
|
|
|
|
module.exit_json(changed=modified, msg=output)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|