| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Kairo Araujo <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: installp |
| 11 |
author: |
| 12 |
- Kairo Araujo (@kairoaraujo) |
| 13 |
short_description: Manage packages on AIX |
| 14 |
description: |
| 15 |
- Manage packages using 'installp' on AIX. |
| 16 |
deprecated: |
| 17 |
removed_in: 15.0.0 |
| 18 |
why: The module is not actively maintained. |
| 19 |
alternative: >- |
| 20 |
Use C(ibm.power_aix.installp) instead. |
| 21 |
See U(https://ibm.github.io/ansible-power-aix/modules/installp.html) for details. |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._attributes |
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: full |
| 27 |
diff_mode: |
| 28 |
support: none |
| 29 |
options: |
| 30 |
accept_license: |
| 31 |
description: |
| 32 |
- Whether to accept the license for the package(s). |
| 33 |
type: bool |
| 34 |
default: false |
| 35 |
name: |
| 36 |
description: |
| 37 |
- One or more packages to install or remove. |
| 38 |
- Use V(all) to install all packages available on informed O(repository_path). |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
required: true |
| 42 |
aliases: [pkg] |
| 43 |
repository_path: |
| 44 |
description: |
| 45 |
- Path with AIX packages (required to install). |
| 46 |
type: path |
| 47 |
state: |
| 48 |
description: |
| 49 |
- Whether the package needs to be present on or absent from the system. |
| 50 |
type: str |
| 51 |
choices: [absent, present] |
| 52 |
default: present |
| 53 |
notes: |
| 54 |
- If the package is already installed, even the package/fileset is new, the module does not install it. |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
- name: Install package foo |
| 59 |
community.general.installp: |
| 60 |
name: foo |
| 61 |
repository_path: /repository/AIX71/installp/base |
| 62 |
accept_license: true |
| 63 |
state: present |
| 64 |
|
| 65 |
- name: Install bos.sysmgt that includes bos.sysmgt.nim.master, bos.sysmgt.nim.spot |
| 66 |
community.general.installp: |
| 67 |
name: bos.sysmgt |
| 68 |
repository_path: /repository/AIX71/installp/base |
| 69 |
accept_license: true |
| 70 |
state: present |
| 71 |
|
| 72 |
- name: Install bos.sysmgt.nim.master only |
| 73 |
community.general.installp: |
| 74 |
name: bos.sysmgt.nim.master |
| 75 |
repository_path: /repository/AIX71/installp/base |
| 76 |
accept_license: true |
| 77 |
state: present |
| 78 |
|
| 79 |
- name: Install bos.sysmgt.nim.master and bos.sysmgt.nim.spot |
| 80 |
community.general.installp: |
| 81 |
name: bos.sysmgt.nim.master, bos.sysmgt.nim.spot |
| 82 |
repository_path: /repository/AIX71/installp/base |
| 83 |
accept_license: true |
| 84 |
state: present |
| 85 |
|
| 86 |
- name: Remove packages bos.sysmgt.nim.master |
| 87 |
community.general.installp: |
| 88 |
name: bos.sysmgt.nim.master |
| 89 |
state: absent |
| 90 |
""" |
| 91 |
|
| 92 |
RETURN = r""" # """ |
| 93 |
|
| 94 |
import os |
| 95 |
import re |
| 96 |
|
| 97 |
from ansible.module_utils.basic import AnsibleModule |
| 98 |
|
| 99 |
|
| 100 |
def _check_new_pkg(module, package, repository_path): |
| 101 |
""" |
| 102 |
Check if the package of fileset is correct name and repository path. |
| 103 |
|
| 104 |
:param module: Ansible module arguments spec. |
| 105 |
:param package: Package/fileset name. |
| 106 |
:param repository_path: Repository package path. |
| 107 |
:return: Bool, package information. |
| 108 |
""" |
| 109 |
|
| 110 |
if os.path.isdir(repository_path): |
| 111 |
installp_cmd = module.get_bin_path("installp", True) |
| 112 |
rc, package_result, err = module.run_command([installp_cmd, "-l", "-MR", "-d", repository_path]) |
| 113 |
if rc != 0: |
| 114 |
module.fail_json(msg="Failed to run installp.", rc=rc, err=err) |
| 115 |
|
| 116 |
if package == "all": |
| 117 |
pkg_info = "All packages on dir" |
| 118 |
return True, pkg_info |
| 119 |
|
| 120 |
else: |
| 121 |
pkg_info = {} |
| 122 |
for line in package_result.splitlines(): |
| 123 |
if re.findall(package, line): |
| 124 |
pkg_name = line.split()[0].strip() |
| 125 |
pkg_version = line.split()[1].strip() |
| 126 |
pkg_info[pkg_name] = pkg_version |
| 127 |
|
| 128 |
return True, pkg_info |
| 129 |
|
| 130 |
return False, None |
| 131 |
|
| 132 |
else: |
| 133 |
module.fail_json(msg=f"Repository path {repository_path} is not valid.") |
| 134 |
|
| 135 |
|
| 136 |
def _check_installed_pkg(module, package, repository_path): |
| 137 |
""" |
| 138 |
Check the package on AIX. |
| 139 |
It verifies if the package is installed and information |
| 140 |
|
| 141 |
:param module: Ansible module parameters spec. |
| 142 |
:param package: Package/fileset name. |
| 143 |
:param repository_path: Repository package path. |
| 144 |
:return: Bool, package data. |
| 145 |
""" |
| 146 |
|
| 147 |
lslpp_cmd = module.get_bin_path("lslpp", True) |
| 148 |
rc, lslpp_result, err = module.run_command([lslpp_cmd, "-lcq", f"{package}*"]) |
| 149 |
|
| 150 |
if rc == 1: |
| 151 |
package_state = " ".join(err.split()[-2:]) |
| 152 |
if package_state == "not installed.": |
| 153 |
return False, None |
| 154 |
else: |
| 155 |
module.fail_json(msg="Failed to run lslpp.", rc=rc, err=err) |
| 156 |
|
| 157 |
if rc != 0: |
| 158 |
module.fail_json(msg="Failed to run lslpp.", rc=rc, err=err) |
| 159 |
|
| 160 |
pkg_data = {} |
| 161 |
full_pkg_data = lslpp_result.splitlines() |
| 162 |
for line in full_pkg_data: |
| 163 |
pkg_name, fileset, level = line.split(":")[0:3] |
| 164 |
pkg_data[pkg_name] = fileset, level |
| 165 |
|
| 166 |
return True, pkg_data |
| 167 |
|
| 168 |
|
| 169 |
def remove(module, installp_cmd, packages): |
| 170 |
repository_path = None |
| 171 |
remove_count = 0 |
| 172 |
removed_pkgs = [] |
| 173 |
not_found_pkg = [] |
| 174 |
for package in packages: |
| 175 |
pkg_check, dummy = _check_installed_pkg(module, package, repository_path) |
| 176 |
|
| 177 |
if pkg_check: |
| 178 |
if not module.check_mode: |
| 179 |
rc, remove_out, err = module.run_command([installp_cmd, "-u", package]) |
| 180 |
if rc != 0: |
| 181 |
module.fail_json(msg="Failed to run installp.", rc=rc, err=err) |
| 182 |
remove_count += 1 |
| 183 |
removed_pkgs.append(package) |
| 184 |
|
| 185 |
else: |
| 186 |
not_found_pkg.append(package) |
| 187 |
|
| 188 |
if remove_count > 0: |
| 189 |
if len(not_found_pkg) > 1: |
| 190 |
not_found_pkg.insert(0, "Package(s) not found: ") |
| 191 |
|
| 192 |
changed = True |
| 193 |
msg = f"Packages removed: {' '.join(removed_pkgs)}. {' '.join(not_found_pkg)} " |
| 194 |
|
| 195 |
else: |
| 196 |
changed = False |
| 197 |
msg = f"No packages removed, all packages not found: {' '.join(not_found_pkg)}" |
| 198 |
|
| 199 |
return changed, msg |
| 200 |
|
| 201 |
|
| 202 |
def install(module, installp_cmd, packages, repository_path, accept_license): |
| 203 |
installed_pkgs = [] |
| 204 |
not_found_pkgs = [] |
| 205 |
already_installed_pkgs = {} |
| 206 |
|
| 207 |
accept_license_param = { |
| 208 |
True: ["-Y"], |
| 209 |
False: [], |
| 210 |
} |
| 211 |
|
| 212 |
# Validate if package exists on repository path. |
| 213 |
for package in packages: |
| 214 |
pkg_check, pkg_data = _check_new_pkg(module, package, repository_path) |
| 215 |
|
| 216 |
# If package exists on repository path, check if package is installed. |
| 217 |
if pkg_check: |
| 218 |
pkg_check_current, pkg_info = _check_installed_pkg(module, package, repository_path) |
| 219 |
|
| 220 |
# If package is already installed. |
| 221 |
if pkg_check_current: |
| 222 |
# Check if package is a package and not a fileset, get version |
| 223 |
# and add the package into already installed list |
| 224 |
if package in pkg_info.keys(): |
| 225 |
already_installed_pkgs[package] = pkg_info[package][1] |
| 226 |
|
| 227 |
else: |
| 228 |
# If the package is not a package but a fileset, confirm |
| 229 |
# and add the fileset/package into already installed list |
| 230 |
for key in pkg_info.keys(): |
| 231 |
if package in pkg_info[key]: |
| 232 |
already_installed_pkgs[package] = pkg_info[key][1] |
| 233 |
|
| 234 |
else: |
| 235 |
if not module.check_mode: |
| 236 |
rc, out, err = module.run_command( |
| 237 |
[installp_cmd, "-a"] |
| 238 |
+ accept_license_param[accept_license] |
| 239 |
+ ["-X", "-d", repository_path, package] |
| 240 |
) |
| 241 |
if rc != 0: |
| 242 |
module.fail_json(msg="Failed to run installp", rc=rc, err=err) |
| 243 |
installed_pkgs.append(package) |
| 244 |
|
| 245 |
else: |
| 246 |
not_found_pkgs.append(package) |
| 247 |
|
| 248 |
if len(installed_pkgs) > 0: |
| 249 |
installed_msg = f" Installed: {' '.join(installed_pkgs)}." |
| 250 |
else: |
| 251 |
installed_msg = "" |
| 252 |
|
| 253 |
if len(not_found_pkgs) > 0: |
| 254 |
not_found_msg = f" Not found: {' '.join(not_found_pkgs)}." |
| 255 |
else: |
| 256 |
not_found_msg = "" |
| 257 |
|
| 258 |
if len(already_installed_pkgs) > 0: |
| 259 |
already_installed_msg = f" Already installed: {already_installed_pkgs}." |
| 260 |
else: |
| 261 |
already_installed_msg = "" |
| 262 |
|
| 263 |
if len(installed_pkgs) > 0: |
| 264 |
changed = True |
| 265 |
msg = f"{installed_msg}{not_found_msg}{already_installed_msg}" |
| 266 |
else: |
| 267 |
changed = False |
| 268 |
msg = f"No packages installed.{installed_msg}{not_found_msg}{already_installed_msg}" |
| 269 |
|
| 270 |
return changed, msg |
| 271 |
|
| 272 |
|
| 273 |
def main(): |
| 274 |
module = AnsibleModule( |
| 275 |
argument_spec=dict( |
| 276 |
name=dict(type="list", elements="str", required=True, aliases=["pkg"]), |
| 277 |
repository_path=dict(type="path"), |
| 278 |
accept_license=dict(type="bool", default=False), |
| 279 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 280 |
), |
| 281 |
supports_check_mode=True, |
| 282 |
) |
| 283 |
|
| 284 |
name = module.params["name"] |
| 285 |
repository_path = module.params["repository_path"] |
| 286 |
accept_license = module.params["accept_license"] |
| 287 |
state = module.params["state"] |
| 288 |
|
| 289 |
installp_cmd = module.get_bin_path("installp", True) |
| 290 |
|
| 291 |
if state == "present": |
| 292 |
if repository_path is None: |
| 293 |
module.fail_json(msg="repository_path is required to install package") |
| 294 |
|
| 295 |
changed, msg = install(module, installp_cmd, name, repository_path, accept_license) |
| 296 |
|
| 297 |
elif state == "absent": |
| 298 |
changed, msg = remove(module, installp_cmd, name) |
| 299 |
|
| 300 |
else: |
| 301 |
module.fail_json(changed=False, msg="Unexpected state.") |
| 302 |
|
| 303 |
module.exit_json(changed=changed, msg=msg) |
| 304 |
|
| 305 |
|
| 306 |
if __name__ == "__main__": |
| 307 |
main() |