| 1 |
# Copyright (c) 2018, Stefan Heitmueller <[email protected]> |
| 2 |
# Copyright (c) 2018 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
name: gitlab_runners |
| 10 |
author: |
| 11 |
- Stefan Heitmüller (@morph027) <[email protected]> |
| 12 |
short_description: Ansible dynamic inventory plugin for GitLab runners |
| 13 |
requirements: |
| 14 |
- python-gitlab > 1.8.0 |
| 15 |
extends_documentation_fragment: |
| 16 |
- ansible.builtin.constructed |
| 17 |
description: |
| 18 |
- Reads inventories from the GitLab API. |
| 19 |
- Uses a YAML configuration file gitlab_runners.[yml|yaml]. |
| 20 |
options: |
| 21 |
plugin: |
| 22 |
description: The name of this plugin, it should always be set to V(gitlab_runners) for this plugin to recognize it as its own. |
| 23 |
type: str |
| 24 |
required: true |
| 25 |
choices: |
| 26 |
- gitlab_runners |
| 27 |
- community.general.gitlab_runners |
| 28 |
server_url: |
| 29 |
description: The URL of the GitLab server, with protocol (i.e. http or https). |
| 30 |
env: |
| 31 |
- name: GITLAB_SERVER_URL |
| 32 |
version_added: 1.0.0 |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
api_token: |
| 36 |
description: GitLab token for logging in. |
| 37 |
env: |
| 38 |
- name: GITLAB_API_TOKEN |
| 39 |
version_added: 1.0.0 |
| 40 |
type: str |
| 41 |
aliases: |
| 42 |
- private_token |
| 43 |
- access_token |
| 44 |
filter: |
| 45 |
description: Filter runners from GitLab API. |
| 46 |
env: |
| 47 |
- name: GITLAB_FILTER |
| 48 |
version_added: 1.0.0 |
| 49 |
type: str |
| 50 |
choices: ['active', 'paused', 'online', 'specific', 'shared'] |
| 51 |
verbose_output: |
| 52 |
description: Toggle to (not) include all available nodes metadata. |
| 53 |
type: bool |
| 54 |
default: true |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
--- |
| 59 |
# gitlab_runners.yml |
| 60 |
plugin: community.general.gitlab_runners |
| 61 |
host: https://gitlab.com |
| 62 |
|
| 63 |
--- |
| 64 |
# Example using constructed features to create groups and set ansible_host |
| 65 |
plugin: community.general.gitlab_runners |
| 66 |
host: https://gitlab.com |
| 67 |
strict: false |
| 68 |
keyed_groups: |
| 69 |
# add e.g. amd64 hosts to an arch_amd64 group |
| 70 |
- prefix: arch |
| 71 |
key: 'architecture' |
| 72 |
# add e.g. linux hosts to an os_linux group |
| 73 |
- prefix: os |
| 74 |
key: 'platform' |
| 75 |
# create a group per runner tag |
| 76 |
# e.g. a runner tagged w/ "production" ends up in group "label_production" |
| 77 |
# hint: labels containing special characters will be converted to safe names |
| 78 |
- key: 'tag_list' |
| 79 |
prefix: tag |
| 80 |
""" |
| 81 |
|
| 82 |
from ansible.errors import AnsibleError, AnsibleParserError |
| 83 |
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable |
| 84 |
|
| 85 |
from ansible_collections.community.general.plugins.plugin_utils._unsafe import make_unsafe |
| 86 |
|
| 87 |
try: |
| 88 |
import gitlab |
| 89 |
|
| 90 |
HAS_GITLAB = True |
| 91 |
except ImportError: |
| 92 |
HAS_GITLAB = False |
| 93 |
|
| 94 |
|
| 95 |
class InventoryModule(BaseInventoryPlugin, Constructable): |
| 96 |
"""Host inventory parser for ansible using GitLab API as source.""" |
| 97 |
|
| 98 |
NAME = "community.general.gitlab_runners" |
| 99 |
|
| 100 |
def _populate(self): |
| 101 |
gl = gitlab.Gitlab(self.get_option("server_url"), private_token=self.get_option("api_token")) |
| 102 |
self.inventory.add_group("gitlab_runners") |
| 103 |
try: |
| 104 |
if self.get_option("filter"): |
| 105 |
runners = gl.runners.all(scope=self.get_option("filter")) |
| 106 |
else: |
| 107 |
runners = gl.runners.all() |
| 108 |
for runner in runners: |
| 109 |
host = make_unsafe(str(runner["id"])) |
| 110 |
ip_address = runner["ip_address"] |
| 111 |
host_attrs = make_unsafe(vars(gl.runners.get(runner["id"]))["_attrs"]) |
| 112 |
self.inventory.add_host(host, group="gitlab_runners") |
| 113 |
self.inventory.set_variable(host, "ansible_host", make_unsafe(ip_address)) |
| 114 |
if self.get_option("verbose_output", True): |
| 115 |
self.inventory.set_variable(host, "gitlab_runner_attributes", host_attrs) |
| 116 |
|
| 117 |
# Use constructed if applicable |
| 118 |
strict = self.get_option("strict") |
| 119 |
# Composed variables |
| 120 |
self._set_composite_vars(self.get_option("compose"), host_attrs, host, strict=strict) |
| 121 |
# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group |
| 122 |
self._add_host_to_composed_groups(self.get_option("groups"), host_attrs, host, strict=strict) |
| 123 |
# Create groups based on variable values and add the corresponding hosts to it |
| 124 |
self._add_host_to_keyed_groups(self.get_option("keyed_groups"), host_attrs, host, strict=strict) |
| 125 |
except Exception as e: |
| 126 |
raise AnsibleParserError( |
| 127 |
f"Unable to fetch hosts from GitLab API, this was the original exception: {e}" |
| 128 |
) from e |
| 129 |
|
| 130 |
def verify_file(self, path): |
| 131 |
"""Return the possibly of a file being consumable by this plugin.""" |
| 132 |
return super().verify_file(path) and path.endswith(("gitlab_runners.yaml", "gitlab_runners.yml")) |
| 133 |
|
| 134 |
def parse(self, inventory, loader, path, cache=True): |
| 135 |
if not HAS_GITLAB: |
| 136 |
raise AnsibleError( |
| 137 |
"The GitLab runners dynamic inventory plugin requires python-gitlab: https://python-gitlab.readthedocs.io/en/stable/" |
| 138 |
) |
| 139 |
super().parse(inventory, loader, path, cache) |
| 140 |
self._read_config_data(path) |
| 141 |
self._populate() |