| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2014, Peter Oliver <[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: pkg5 |
| 11 |
author: |
| 12 |
- Peter Oliver (@mavit) |
| 13 |
short_description: Manages packages with the Solaris 11 Image Packaging System |
| 14 |
description: |
| 15 |
- IPS packages are the native packages in Solaris 11 and higher. |
| 16 |
notes: |
| 17 |
- The naming of IPS packages is explained at U(http://www.oracle.com/technetwork/articles/servers-storage-admin/ips-package-versioning-2232906.html). |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
name: |
| 27 |
description: |
| 28 |
- An FRMI of the package(s) to be installed/removed/updated. |
| 29 |
- Multiple packages may be specified, separated by V(,). |
| 30 |
required: true |
| 31 |
type: list |
| 32 |
elements: str |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Whether to install (V(present), V(latest)), or remove (V(absent)) a package. |
| 36 |
choices: [absent, latest, present, installed, removed, uninstalled] |
| 37 |
default: present |
| 38 |
type: str |
| 39 |
accept_licenses: |
| 40 |
description: |
| 41 |
- Accept any licences. |
| 42 |
type: bool |
| 43 |
default: false |
| 44 |
aliases: [accept, accept_licences] |
| 45 |
be_name: |
| 46 |
description: |
| 47 |
- Creates a new boot environment with the given name. |
| 48 |
type: str |
| 49 |
refresh: |
| 50 |
description: |
| 51 |
- Refresh publishers before execution. |
| 52 |
type: bool |
| 53 |
default: true |
| 54 |
verbose: |
| 55 |
description: |
| 56 |
- Set to V(true) to disable quiet execution. |
| 57 |
type: bool |
| 58 |
default: false |
| 59 |
version_added: 9.0.0 |
| 60 |
""" |
| 61 |
EXAMPLES = r""" |
| 62 |
- name: Install Vim |
| 63 |
community.general.pkg5: |
| 64 |
name: editor/vim |
| 65 |
|
| 66 |
- name: Install Vim without refreshing publishers |
| 67 |
community.general.pkg5: |
| 68 |
name: editor/vim |
| 69 |
refresh: false |
| 70 |
|
| 71 |
- name: Remove finger daemon |
| 72 |
community.general.pkg5: |
| 73 |
name: service/network/finger |
| 74 |
state: absent |
| 75 |
|
| 76 |
- name: Install several packages at once |
| 77 |
community.general.pkg5: |
| 78 |
name: |
| 79 |
- /file/gnu-findutils |
| 80 |
- /text/gnu-grep |
| 81 |
""" |
| 82 |
|
| 83 |
import re |
| 84 |
|
| 85 |
from ansible.module_utils.basic import AnsibleModule |
| 86 |
|
| 87 |
|
| 88 |
def main(): |
| 89 |
module = AnsibleModule( |
| 90 |
argument_spec=dict( |
| 91 |
name=dict(type="list", elements="str", required=True), |
| 92 |
state=dict( |
| 93 |
type="str", |
| 94 |
default="present", |
| 95 |
choices=["absent", "installed", "latest", "present", "removed", "uninstalled"], |
| 96 |
), |
| 97 |
accept_licenses=dict(type="bool", default=False, aliases=["accept", "accept_licences"]), |
| 98 |
be_name=dict(type="str"), |
| 99 |
refresh=dict(type="bool", default=True), |
| 100 |
verbose=dict(type="bool", default=False), |
| 101 |
), |
| 102 |
supports_check_mode=True, |
| 103 |
) |
| 104 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 105 |
|
| 106 |
params = module.params |
| 107 |
packages = [] |
| 108 |
|
| 109 |
# pkg(5) FRMIs include a comma before the release number, but |
| 110 |
# AnsibleModule will have split this into multiple items for us. |
| 111 |
# Try to spot where this has happened and fix it. |
| 112 |
for fragment in params["name"]: |
| 113 |
if re.search(r"^\d+(?:\.\d+)*", fragment) and packages and re.search(r"@[^,]*$", packages[-1]): |
| 114 |
packages[-1] += f",{fragment}" |
| 115 |
else: |
| 116 |
packages.append(fragment) |
| 117 |
|
| 118 |
if params["state"] in ["present", "installed"]: |
| 119 |
ensure(module, "present", packages, params) |
| 120 |
elif params["state"] in ["latest"]: |
| 121 |
ensure(module, "latest", packages, params) |
| 122 |
elif params["state"] in ["absent", "uninstalled", "removed"]: |
| 123 |
ensure(module, "absent", packages, params) |
| 124 |
|
| 125 |
|
| 126 |
def ensure(module, state, packages, params): |
| 127 |
response = { |
| 128 |
"results": [], |
| 129 |
"msg": "", |
| 130 |
} |
| 131 |
behaviour = { |
| 132 |
"present": { |
| 133 |
"filter": lambda p: not is_installed(module, p), |
| 134 |
"subcommand": "install", |
| 135 |
}, |
| 136 |
"latest": { |
| 137 |
"filter": lambda p: not is_installed(module, p) or not is_latest(module, p), |
| 138 |
"subcommand": "install", |
| 139 |
}, |
| 140 |
"absent": { |
| 141 |
"filter": lambda p: is_installed(module, p), |
| 142 |
"subcommand": "uninstall", |
| 143 |
}, |
| 144 |
} |
| 145 |
|
| 146 |
if module.check_mode: |
| 147 |
dry_run = ["-n"] |
| 148 |
else: |
| 149 |
dry_run = [] |
| 150 |
|
| 151 |
if params["accept_licenses"]: |
| 152 |
accept_licenses = ["--accept"] |
| 153 |
else: |
| 154 |
accept_licenses = [] |
| 155 |
|
| 156 |
if params["be_name"]: |
| 157 |
beadm = [f"--be-name={module.params['be_name']}"] |
| 158 |
else: |
| 159 |
beadm = [] |
| 160 |
|
| 161 |
if params["refresh"]: |
| 162 |
no_refresh = [] |
| 163 |
else: |
| 164 |
no_refresh = ["--no-refresh"] |
| 165 |
|
| 166 |
if params["verbose"]: |
| 167 |
verbosity = [] |
| 168 |
else: |
| 169 |
verbosity = ["-q"] |
| 170 |
|
| 171 |
to_modify = list(filter(behaviour[state]["filter"], packages)) |
| 172 |
if to_modify: |
| 173 |
rc, out, err = module.run_command( |
| 174 |
["pkg", behaviour[state]["subcommand"]] |
| 175 |
+ dry_run |
| 176 |
+ accept_licenses |
| 177 |
+ beadm |
| 178 |
+ no_refresh |
| 179 |
+ verbosity |
| 180 |
+ ["--"] |
| 181 |
+ to_modify |
| 182 |
) |
| 183 |
response["rc"] = rc |
| 184 |
response["results"].append(out) |
| 185 |
response["msg"] += err |
| 186 |
response["changed"] = True |
| 187 |
if rc == 4: |
| 188 |
response["changed"] = False |
| 189 |
response["failed"] = False |
| 190 |
elif rc != 0: |
| 191 |
module.fail_json(**response) |
| 192 |
|
| 193 |
module.exit_json(**response) |
| 194 |
|
| 195 |
|
| 196 |
def is_installed(module, package): |
| 197 |
rc, out, err = module.run_command(["pkg", "list", "--", package]) |
| 198 |
return not bool(int(rc)) |
| 199 |
|
| 200 |
|
| 201 |
def is_latest(module, package): |
| 202 |
rc, out, err = module.run_command(["pkg", "list", "-u", "--", package]) |
| 203 |
return bool(int(rc)) |
| 204 |
|
| 205 |
|
| 206 |
if __name__ == "__main__": |
| 207 |
main() |