| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2021, Alexei Znamensky <[email protected]> |
| 3 |
# |
| 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: ansible_galaxy_install |
| 11 |
author: |
| 12 |
- "Alexei Znamensky (@russoz)" |
| 13 |
short_description: Install Ansible roles or collections using ansible-galaxy |
| 14 |
version_added: 3.5.0 |
| 15 |
description: |
| 16 |
- This module allows the installation of Ansible collections or roles using C(ansible-galaxy). |
| 17 |
notes: |
| 18 |
- Support for B(Ansible 2.9/2.10) was removed in community.general 8.0.0. |
| 19 |
- The module tries to run using the C(C.UTF-8) locale. If that fails, it tries C(en_US.UTF-8). If that one also fails, the |
| 20 |
module fails. |
| 21 |
seealso: |
| 22 |
- name: C(ansible-galaxy) command manual page |
| 23 |
description: Manual page for the command. |
| 24 |
link: https://docs.ansible.com/projects/ansible/latest/cli/ansible-galaxy.html |
| 25 |
|
| 26 |
requirements: |
| 27 |
- ansible-core 2.11 or newer |
| 28 |
extends_documentation_fragment: |
| 29 |
- community.general._attributes |
| 30 |
attributes: |
| 31 |
check_mode: |
| 32 |
support: none |
| 33 |
diff_mode: |
| 34 |
support: none |
| 35 |
options: |
| 36 |
state: |
| 37 |
description: |
| 38 |
- If O(state=present) then the collection or role is installed. Note that the collections and roles are not updated |
| 39 |
with this option. |
| 40 |
- Currently the O(state=latest) is ignored unless O(type=collection), and it ensures the collection is installed and |
| 41 |
updated to the latest available version. |
| 42 |
- Please note that O(force=true) can be used to perform upgrade regardless of O(type). |
| 43 |
type: str |
| 44 |
choices: [present, latest] |
| 45 |
default: present |
| 46 |
version_added: 9.1.0 |
| 47 |
type: |
| 48 |
description: |
| 49 |
- The type of installation performed by C(ansible-galaxy). |
| 50 |
- If O(type=both), then O(requirements_file) must be passed and it may contain both roles and collections. |
| 51 |
- 'Note however that the opposite is not true: if using a O(requirements_file), then O(type) can be any of the three |
| 52 |
choices.' |
| 53 |
type: str |
| 54 |
choices: [collection, role, both] |
| 55 |
required: true |
| 56 |
name: |
| 57 |
description: |
| 58 |
- Name of the collection or role being installed. |
| 59 |
- Versions can be specified with C(ansible-galaxy) usual formats. For example, the collection V(community.docker:1.6.1) |
| 60 |
or the role V(ansistrano.deploy,3.8.0). |
| 61 |
- O(name) and O(requirements_file) are mutually exclusive. |
| 62 |
type: str |
| 63 |
requirements_file: |
| 64 |
description: |
| 65 |
- Path to a file containing a list of requirements to be installed. |
| 66 |
- It works for O(type) equals to V(collection) and V(role). |
| 67 |
- O(name) and O(requirements_file) are mutually exclusive. |
| 68 |
type: path |
| 69 |
dest: |
| 70 |
description: |
| 71 |
- The path to the directory containing your collections or roles, according to the value of O(type). |
| 72 |
- Please notice that C(ansible-galaxy) does not install collections with O(type=both), when O(requirements_file) contains |
| 73 |
both roles and collections and O(dest) is specified. |
| 74 |
type: path |
| 75 |
executable: |
| 76 |
description: |
| 77 |
- Path to the C(ansible-galaxy) executable. |
| 78 |
- When not specified, the module uses C(ansible-galaxy) found by Ansible. |
| 79 |
type: path |
| 80 |
version_added: 12.5.0 |
| 81 |
no_deps: |
| 82 |
description: |
| 83 |
- Refrain from installing dependencies. |
| 84 |
version_added: 4.5.0 |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
force: |
| 88 |
description: |
| 89 |
- Force overwriting existing roles and/or collections. |
| 90 |
- It can be used for upgrading, but the module output always reports RV(ignore:changed=true). |
| 91 |
- Using O(force=true) is mandatory when downgrading. |
| 92 |
type: bool |
| 93 |
default: false |
| 94 |
""" |
| 95 |
|
| 96 |
EXAMPLES = r""" |
| 97 |
- name: Install collection community.network |
| 98 |
community.general.ansible_galaxy_install: |
| 99 |
type: collection |
| 100 |
name: community.network |
| 101 |
|
| 102 |
- name: Install role at specific path |
| 103 |
community.general.ansible_galaxy_install: |
| 104 |
type: role |
| 105 |
name: ansistrano.deploy |
| 106 |
dest: /ansible/roles |
| 107 |
|
| 108 |
- name: Install collections and roles together |
| 109 |
community.general.ansible_galaxy_install: |
| 110 |
type: both |
| 111 |
requirements_file: requirements.yml |
| 112 |
|
| 113 |
- name: Force-install collection community.network at specific version |
| 114 |
community.general.ansible_galaxy_install: |
| 115 |
type: collection |
| 116 |
name: community.network:3.0.2 |
| 117 |
force: true |
| 118 |
""" |
| 119 |
|
| 120 |
RETURN = r""" |
| 121 |
type: |
| 122 |
description: The value of the O(type) parameter. |
| 123 |
type: str |
| 124 |
returned: always |
| 125 |
name: |
| 126 |
description: The value of the O(name) parameter. |
| 127 |
type: str |
| 128 |
returned: always |
| 129 |
dest: |
| 130 |
description: The value of the O(dest) parameter. |
| 131 |
type: str |
| 132 |
returned: always |
| 133 |
requirements_file: |
| 134 |
description: The value of the O(requirements_file) parameter. |
| 135 |
type: str |
| 136 |
returned: always |
| 137 |
force: |
| 138 |
description: The value of the O(force) parameter. |
| 139 |
type: bool |
| 140 |
returned: always |
| 141 |
installed_roles: |
| 142 |
description: |
| 143 |
- If O(requirements_file) is specified instead, returns dictionary with all the roles installed per path. |
| 144 |
- If O(name) is specified, returns that role name and the version installed per path. |
| 145 |
type: dict |
| 146 |
returned: always when installing roles |
| 147 |
contains: |
| 148 |
"<path>": |
| 149 |
description: Roles and versions for that path. |
| 150 |
type: dict |
| 151 |
sample: |
| 152 |
/home/user42/.ansible/roles: |
| 153 |
ansistrano.deploy: 3.9.0 |
| 154 |
baztian.xfce: v0.0.3 |
| 155 |
/custom/ansible/roles: |
| 156 |
ansistrano.deploy: 3.8.0 |
| 157 |
installed_collections: |
| 158 |
description: |
| 159 |
- If O(requirements_file) is specified instead, returns dictionary with all the collections installed per path. |
| 160 |
- If O(name) is specified, returns that collection name and the version installed per path. |
| 161 |
type: dict |
| 162 |
returned: always when installing collections |
| 163 |
contains: |
| 164 |
"<path>": |
| 165 |
description: Collections and versions for that path. |
| 166 |
type: dict |
| 167 |
sample: |
| 168 |
/home/az/.ansible/collections/ansible_collections: |
| 169 |
community.docker: 1.6.0 |
| 170 |
community.general: 3.0.2 |
| 171 |
/custom/ansible/ansible_collections: |
| 172 |
community.general: 3.1.0 |
| 173 |
new_collections: |
| 174 |
description: New collections installed by this module. |
| 175 |
returned: success |
| 176 |
type: dict |
| 177 |
sample: |
| 178 |
community.general: 3.1.0 |
| 179 |
community.docker: 1.6.1 |
| 180 |
new_roles: |
| 181 |
description: New roles installed by this module. |
| 182 |
returned: success |
| 183 |
type: dict |
| 184 |
sample: |
| 185 |
ansistrano.deploy: 3.8.0 |
| 186 |
baztian.xfce: v0.0.3 |
| 187 |
version: |
| 188 |
description: Version of ansible-core for ansible-galaxy. |
| 189 |
type: str |
| 190 |
returned: always |
| 191 |
sample: 2.17.4 |
| 192 |
version_added: 10.0.0 |
| 193 |
""" |
| 194 |
|
| 195 |
import re |
| 196 |
|
| 197 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt |
| 198 |
from ansible_collections.community.general.plugins.module_utils._module_helper import ( |
| 199 |
ModuleHelper, |
| 200 |
ModuleHelperException, |
| 201 |
) |
| 202 |
|
| 203 |
|
| 204 |
class AnsibleGalaxyInstall(ModuleHelper): |
| 205 |
_RE_GALAXY_VERSION = re.compile(r"^ansible-galaxy(?: \[core)? (?P<version>\d+\.\d+\.\d+)(?:\.\w+)?(?:\])?") |
| 206 |
_RE_LIST_PATH = re.compile(r"^# (?P<path>.*)$") |
| 207 |
_RE_LIST_COLL = re.compile(r"^(?P<elem>\w+\.\w+)\s+(?P<version>[\d\.]+)\s*$") |
| 208 |
_RE_LIST_ROLE = re.compile(r"^- (?P<elem>\w+\.\w+),\s+(?P<version>[\d\.]+)\s*$") |
| 209 |
_RE_INSTALL_OUTPUT = re.compile( |
| 210 |
r"^(?:(?P<collection>\w+\.\w+):(?P<cversion>[\d\.]+)|- (?P<role>\w+\.\w+) \((?P<rversion>[\d\.]+)\)) was installed successfully$" |
| 211 |
) |
| 212 |
ansible_version = None |
| 213 |
|
| 214 |
output_params = ("type", "name", "dest", "requirements_file", "executable", "force", "no_deps") |
| 215 |
module = dict( |
| 216 |
argument_spec=dict( |
| 217 |
state=dict(type="str", choices=["present", "latest"], default="present"), |
| 218 |
type=dict(type="str", choices=("collection", "role", "both"), required=True), |
| 219 |
name=dict(type="str"), |
| 220 |
requirements_file=dict(type="path"), |
| 221 |
dest=dict(type="path"), |
| 222 |
executable=dict(type="path"), |
| 223 |
force=dict(type="bool", default=False), |
| 224 |
no_deps=dict(type="bool", default=False), |
| 225 |
), |
| 226 |
mutually_exclusive=[("name", "requirements_file")], |
| 227 |
required_one_of=[("name", "requirements_file")], |
| 228 |
required_if=[("type", "both", ["requirements_file"])], |
| 229 |
supports_check_mode=False, |
| 230 |
) |
| 231 |
|
| 232 |
command = "ansible-galaxy" |
| 233 |
command_args_formats = dict( |
| 234 |
type=cmd_runner_fmt.as_func(lambda v: [] if v == "both" else [v]), |
| 235 |
galaxy_cmd=cmd_runner_fmt.as_list(), |
| 236 |
upgrade=cmd_runner_fmt.as_bool("--upgrade"), |
| 237 |
requirements_file=cmd_runner_fmt.as_opt_val("-r"), |
| 238 |
dest=cmd_runner_fmt.as_opt_val("-p"), |
| 239 |
force=cmd_runner_fmt.as_bool("--force"), |
| 240 |
no_deps=cmd_runner_fmt.as_bool("--no-deps"), |
| 241 |
version=cmd_runner_fmt.as_fixed("--version"), |
| 242 |
name=cmd_runner_fmt.as_list(), |
| 243 |
) |
| 244 |
|
| 245 |
def _make_runner(self, lang): |
| 246 |
return CmdRunner( |
| 247 |
self.module, |
| 248 |
command=self.vars.executable or self.command, |
| 249 |
arg_formats=self.command_args_formats, |
| 250 |
force_lang=lang, |
| 251 |
check_rc=True, |
| 252 |
) |
| 253 |
|
| 254 |
def _get_ansible_galaxy_version(self): |
| 255 |
class UnsupportedLocale(ModuleHelperException): |
| 256 |
pass |
| 257 |
|
| 258 |
def process(rc, out, err): |
| 259 |
if (rc != 0 and "unsupported locale setting" in err) or (rc == 0 and "cannot change locale" in err): |
| 260 |
raise UnsupportedLocale(msg=err) |
| 261 |
line = out.splitlines()[0] |
| 262 |
match = self._RE_GALAXY_VERSION.match(line) |
| 263 |
if not match: |
| 264 |
self.do_raise(f"Unable to determine ansible-galaxy version from: {line}") |
| 265 |
version = match.group("version") |
| 266 |
return version |
| 267 |
|
| 268 |
try: |
| 269 |
runner = self._make_runner("C.UTF-8") |
| 270 |
with runner("version", check_rc=False, output_process=process) as ctx: |
| 271 |
return runner, ctx.run() |
| 272 |
except UnsupportedLocale: |
| 273 |
runner = self._make_runner("en_US.UTF-8") |
| 274 |
with runner("version", check_rc=True, output_process=process) as ctx: |
| 275 |
return runner, ctx.run() |
| 276 |
|
| 277 |
def __init_module__(self): |
| 278 |
self.runner, self.vars.version = self._get_ansible_galaxy_version() |
| 279 |
self.ansible_version = tuple(int(x) for x in self.vars.version.split(".")[:3]) |
| 280 |
if self.ansible_version < (2, 11): |
| 281 |
self.do_raise(msg="Support for Ansible 2.9 and ansible-base 2.10 has been removed.") |
| 282 |
self.vars.set("new_collections", {}, change=True) |
| 283 |
self.vars.set("new_roles", {}, change=True) |
| 284 |
if self.vars.type != "collection": |
| 285 |
self.vars.installed_roles = self._list_roles() |
| 286 |
if self.vars.type != "roles": |
| 287 |
self.vars.installed_collections = self._list_collections() |
| 288 |
|
| 289 |
def _list_element(self, _type, path_re, elem_re): |
| 290 |
def process(rc, out, err): |
| 291 |
return [] if "None of the provided paths were usable" in out else out.splitlines() |
| 292 |
|
| 293 |
with self.runner("type galaxy_cmd dest", output_process=process, check_rc=False) as ctx: |
| 294 |
elems = ctx.run(type=_type, galaxy_cmd="list") |
| 295 |
|
| 296 |
elems_dict = {} |
| 297 |
current_path = None |
| 298 |
for line in elems: |
| 299 |
if line.startswith("#"): |
| 300 |
match = path_re.match(line) |
| 301 |
if not match: |
| 302 |
continue |
| 303 |
if self.vars.dest is not None and match.group("path") != self.vars.dest: |
| 304 |
current_path = None |
| 305 |
continue |
| 306 |
current_path = match.group("path") if match else None |
| 307 |
elems_dict[current_path] = {} |
| 308 |
|
| 309 |
elif current_path is not None: |
| 310 |
match = elem_re.match(line) |
| 311 |
if not match or (self.vars.name is not None and match.group("elem") != self.vars.name): |
| 312 |
continue |
| 313 |
elems_dict[current_path][match.group("elem")] = match.group("version") |
| 314 |
return elems_dict |
| 315 |
|
| 316 |
def _list_collections(self): |
| 317 |
return self._list_element("collection", self._RE_LIST_PATH, self._RE_LIST_COLL) |
| 318 |
|
| 319 |
def _list_roles(self): |
| 320 |
return self._list_element("role", self._RE_LIST_PATH, self._RE_LIST_ROLE) |
| 321 |
|
| 322 |
def __run__(self): |
| 323 |
def process(rc, out, err): |
| 324 |
for line in out.splitlines(): |
| 325 |
match = self._RE_INSTALL_OUTPUT.match(line) |
| 326 |
if not match: |
| 327 |
continue |
| 328 |
if match.group("collection"): |
| 329 |
self.vars.new_collections[match.group("collection")] = match.group("cversion") |
| 330 |
elif match.group("role"): |
| 331 |
self.vars.new_roles[match.group("role")] = match.group("rversion") |
| 332 |
|
| 333 |
upgrade = self.vars.type == "collection" and self.vars.state == "latest" |
| 334 |
with self.runner( |
| 335 |
"type galaxy_cmd upgrade force no_deps dest requirements_file name", output_process=process |
| 336 |
) as ctx: |
| 337 |
ctx.run(galaxy_cmd="install", upgrade=upgrade) |
| 338 |
if self.verbosity > 2: |
| 339 |
self.vars.set("run_info", ctx.run_info) |
| 340 |
|
| 341 |
|
| 342 |
def main(): |
| 343 |
AnsibleGalaxyInstall.execute() |
| 344 |
|
| 345 |
|
| 346 |
if __name__ == "__main__": |
| 347 |
main() |