2020-12-03 20:00:49 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-07-23 14:23:31 +02:00
|
|
|
# Copyright: (c) 2018, Florian Paul Azim Hoberg <florian.hoberg@credativ.de>
|
2020-12-03 20:00:49 +01:00
|
|
|
#
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
module: yum_versionlock
|
|
|
|
version_added: 2.0.0
|
|
|
|
short_description: Locks / unlocks a installed package(s) from being updated by yum package manager
|
|
|
|
description:
|
2021-02-26 23:16:40 +01:00
|
|
|
- This module adds installed packages to yum versionlock to prevent the package(s) from being updated.
|
2020-12-03 20:00:49 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2022-02-18 23:19:39 +01:00
|
|
|
- Package name or a list of package names with optional wildcards.
|
2020-12-03 20:00:49 +01:00
|
|
|
type: list
|
|
|
|
required: true
|
|
|
|
elements: str
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- If state is C(present), package(s) will be added to yum versionlock list.
|
|
|
|
- If state is C(absent), package(s) will be removed from yum versionlock list.
|
|
|
|
choices: [ 'absent', 'present' ]
|
|
|
|
type: str
|
|
|
|
default: present
|
|
|
|
notes:
|
|
|
|
- Requires yum-plugin-versionlock package on the remote node.
|
|
|
|
- Supports C(check_mode).
|
|
|
|
requirements:
|
|
|
|
- yum
|
|
|
|
- yum-versionlock
|
|
|
|
author:
|
2022-07-23 14:23:31 +02:00
|
|
|
- Florian Paul Azim Hoberg (@gyptazy)
|
2020-12-03 20:00:49 +01:00
|
|
|
- Amin Vakil (@aminvakil)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Prevent Apache / httpd from being updated
|
|
|
|
community.general.yum_versionlock:
|
|
|
|
state: present
|
|
|
|
name: httpd
|
|
|
|
|
|
|
|
- name: Prevent multiple packages from being updated
|
|
|
|
community.general.yum_versionlock:
|
|
|
|
state: present
|
|
|
|
name:
|
|
|
|
- httpd
|
|
|
|
- nginx
|
|
|
|
- haproxy
|
|
|
|
- curl
|
|
|
|
|
|
|
|
- name: Remove lock from Apache / httpd to be updated again
|
|
|
|
community.general.yum_versionlock:
|
|
|
|
state: absent
|
|
|
|
package: httpd
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
packages:
|
|
|
|
description: A list of package(s) in versionlock list.
|
|
|
|
returned: success
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
sample: [ 'httpd' ]
|
|
|
|
state:
|
|
|
|
description: State of package(s).
|
|
|
|
returned: success
|
|
|
|
type: str
|
|
|
|
sample: present
|
|
|
|
'''
|
|
|
|
|
2022-02-18 23:19:39 +01:00
|
|
|
import re
|
2020-12-03 20:00:49 +01:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2021-06-27 16:40:49 +02:00
|
|
|
from fnmatch import fnmatch
|
2020-12-03 20:00:49 +01:00
|
|
|
|
2022-02-18 23:19:39 +01:00
|
|
|
# on DNF-based distros, yum is a symlink to dnf, so we try to handle their different entry formats.
|
|
|
|
NEVRA_RE_YUM = re.compile(r'^(?P<exclude>!)?(?P<epoch>\d+):(?P<name>.+)-'
|
|
|
|
r'(?P<version>.+)-(?P<release>.+)\.(?P<arch>.+)$')
|
|
|
|
NEVRA_RE_DNF = re.compile(r"^(?P<exclude>!)?(?P<name>.+)-(?P<epoch>\d+):(?P<version>.+)-"
|
|
|
|
r"(?P<release>.+)\.(?P<arch>.+)$")
|
|
|
|
|
2020-12-03 20:00:49 +01:00
|
|
|
|
|
|
|
class YumVersionLock:
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.params = module.params
|
|
|
|
self.yum_bin = module.get_bin_path('yum', required=True)
|
|
|
|
|
|
|
|
def get_versionlock_packages(self):
|
|
|
|
""" Get an overview of all packages on yum versionlock """
|
|
|
|
rc, out, err = self.module.run_command([self.yum_bin, "versionlock", "list"])
|
|
|
|
if rc == 0:
|
|
|
|
return out
|
|
|
|
elif rc == 1 and 'o such command:' in err:
|
|
|
|
self.module.fail_json(msg="Error: Please install rpm package yum-plugin-versionlock : " + to_native(err) + to_native(out))
|
|
|
|
self.module.fail_json(msg="Error: " + to_native(err) + to_native(out))
|
|
|
|
|
2021-02-26 23:16:40 +01:00
|
|
|
def ensure_state(self, packages, command):
|
|
|
|
""" Ensure packages state """
|
|
|
|
rc, out, err = self.module.run_command([self.yum_bin, "-q", "versionlock", command] + packages)
|
2020-12-03 20:00:49 +01:00
|
|
|
if rc == 0:
|
|
|
|
return True
|
|
|
|
self.module.fail_json(msg="Error: " + to_native(err) + to_native(out))
|
|
|
|
|
|
|
|
|
2022-02-18 23:19:39 +01:00
|
|
|
def match(entry, name):
|
|
|
|
m = NEVRA_RE_YUM.match(entry)
|
|
|
|
if not m:
|
|
|
|
m = NEVRA_RE_DNF.match(entry)
|
|
|
|
if not m:
|
|
|
|
return False
|
|
|
|
return fnmatch(m.group("name"), name)
|
|
|
|
|
|
|
|
|
2020-12-03 20:00:49 +01:00
|
|
|
def main():
|
|
|
|
""" start main program to add/remove a package to yum versionlock"""
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
name=dict(required=True, type='list', elements='str'),
|
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
packages = module.params['name']
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
yum_v = YumVersionLock(module)
|
|
|
|
|
|
|
|
# Get an overview of all packages that have a version lock
|
|
|
|
versionlock_packages = yum_v.get_versionlock_packages()
|
|
|
|
|
|
|
|
# Ensure versionlock state of packages
|
2021-02-26 23:16:40 +01:00
|
|
|
packages_list = []
|
2022-02-18 23:19:39 +01:00
|
|
|
if state in ('present', ):
|
2020-12-03 20:00:49 +01:00
|
|
|
command = 'add'
|
|
|
|
for single_pkg in packages:
|
2022-02-18 23:19:39 +01:00
|
|
|
if not any(match(pkg, single_pkg) for pkg in versionlock_packages.split()):
|
2021-02-26 23:16:40 +01:00
|
|
|
packages_list.append(single_pkg)
|
|
|
|
if packages_list:
|
2021-06-27 16:40:49 +02:00
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = yum_v.ensure_state(packages_list, command)
|
2022-02-18 23:19:39 +01:00
|
|
|
elif state in ('absent', ):
|
2020-12-03 20:00:49 +01:00
|
|
|
command = 'delete'
|
|
|
|
for single_pkg in packages:
|
2022-02-18 23:19:39 +01:00
|
|
|
if any(match(pkg, single_pkg) for pkg in versionlock_packages.split()):
|
2021-02-26 23:16:40 +01:00
|
|
|
packages_list.append(single_pkg)
|
|
|
|
if packages_list:
|
2021-06-27 16:40:49 +02:00
|
|
|
if module.check_mode:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = yum_v.ensure_state(packages_list, command)
|
2020-12-03 20:00:49 +01:00
|
|
|
|
|
|
|
module.exit_json(
|
|
|
|
changed=changed,
|
|
|
|
meta={
|
|
|
|
"packages": packages,
|
|
|
|
"state": state
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|