| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2019, Markus Bergholz ([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 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: gitlab_project_variable |
| 10 |
short_description: Creates/updates/deletes GitLab Projects Variables |
| 11 |
description: |
| 12 |
- When a project variable does not exist, it is created. |
| 13 |
- When a project variable does exist and is not hidden, its value is updated when the values are different. |
| 14 |
When a project variable does exist and is hidden, its value is updated. In this case, the module is B(not idempotent). |
| 15 |
- Variables which are untouched in the playbook, but are not untouched in the GitLab project, they stay untouched (O(purge=false)) |
| 16 |
or are deleted (O(purge=true)). |
| 17 |
author: |
| 18 |
- "Markus Bergholz (@markuman)" |
| 19 |
requirements: |
| 20 |
- python-gitlab python module |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.general._auth_basic |
| 23 |
- community.general._gitlab |
| 24 |
- community.general._attributes |
| 25 |
|
| 26 |
attributes: |
| 27 |
check_mode: |
| 28 |
support: full |
| 29 |
diff_mode: |
| 30 |
support: none |
| 31 |
|
| 32 |
options: |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Create or delete project variable. |
| 36 |
- Possible values are present and absent. |
| 37 |
default: present |
| 38 |
type: str |
| 39 |
choices: ["present", "absent"] |
| 40 |
project: |
| 41 |
description: |
| 42 |
- The path and name of the project. |
| 43 |
required: true |
| 44 |
type: str |
| 45 |
purge: |
| 46 |
description: |
| 47 |
- When set to V(true), all variables which are not untouched in the task are deleted. |
| 48 |
default: false |
| 49 |
type: bool |
| 50 |
vars: |
| 51 |
description: |
| 52 |
- When the list element is a simple key-value pair, C(masked), C(hidden), C(raw), and C(protected) are set to V(false). |
| 53 |
- When the list element is a dict with the keys C(value), C(masked), C(hidden), C(raw), and C(protected), the user can have full |
| 54 |
control about whether a value should be masked, hidden, raw, protected, or a combination. |
| 55 |
- Support for protected values requires GitLab >= 9.3. |
| 56 |
- Support for masked values requires GitLab >= 11.10. |
| 57 |
- Support for hidden values requires GitLab >= 17.4, and was added in community.general 11.3.0. |
| 58 |
- Support for raw values requires GitLab >= 15.7. |
| 59 |
- Support for environment_scope requires GitLab Premium >= 13.11. |
| 60 |
- Support for variable_type requires GitLab >= 11.11. |
| 61 |
- A C(value) must be a string or a number. |
| 62 |
- Field C(variable_type) must be a string with either V(env_var), which is the default, or V(file). |
| 63 |
- Field C(environment_scope) must be a string defined by scope environment. |
| 64 |
- When a value is masked, it must be in Base64 and have a length of at least 8 characters. See GitLab documentation |
| 65 |
on acceptable values for a masked variable (https://docs.gitlab.com/ce/ci/variables/#masked-variables). |
| 66 |
default: {} |
| 67 |
type: dict |
| 68 |
variables: |
| 69 |
version_added: 4.4.0 |
| 70 |
description: |
| 71 |
- A list of dictionaries that represents CI/CD variables. |
| 72 |
- This module works internal with this structure, even if the older O(vars) parameter is used. |
| 73 |
default: [] |
| 74 |
type: list |
| 75 |
elements: dict |
| 76 |
suboptions: |
| 77 |
name: |
| 78 |
description: |
| 79 |
- The name of the variable. |
| 80 |
type: str |
| 81 |
required: true |
| 82 |
value: |
| 83 |
description: |
| 84 |
- The variable value. |
| 85 |
- Required when O(state=present). |
| 86 |
type: str |
| 87 |
description: |
| 88 |
description: |
| 89 |
- A description for the variable. |
| 90 |
- Support for descriptions requires GitLab >= 16.2. |
| 91 |
type: str |
| 92 |
version_added: '11.4.0' |
| 93 |
masked: |
| 94 |
description: |
| 95 |
- Whether variable value is masked or not. |
| 96 |
- Support for masked values requires GitLab >= 11.10. |
| 97 |
type: bool |
| 98 |
default: false |
| 99 |
hidden: |
| 100 |
description: |
| 101 |
- Whether variable value is hidden or not. |
| 102 |
- Implies C(masked). |
| 103 |
- Support for hidden values requires GitLab >= 17.4. |
| 104 |
type: bool |
| 105 |
default: false |
| 106 |
version_added: '11.3.0' |
| 107 |
protected: |
| 108 |
description: |
| 109 |
- Whether variable value is protected or not. |
| 110 |
- Support for protected values requires GitLab >= 9.3. |
| 111 |
type: bool |
| 112 |
default: false |
| 113 |
raw: |
| 114 |
description: |
| 115 |
- Whether variable value is raw or not. |
| 116 |
- Support for raw values requires GitLab >= 15.7. |
| 117 |
type: bool |
| 118 |
default: false |
| 119 |
version_added: '7.4.0' |
| 120 |
variable_type: |
| 121 |
description: |
| 122 |
- Whether a variable is an environment variable (V(env_var)) or a file (V(file)). |
| 123 |
- Support for O(variables[].variable_type) requires GitLab >= 11.11. |
| 124 |
type: str |
| 125 |
choices: ["env_var", "file"] |
| 126 |
default: env_var |
| 127 |
environment_scope: |
| 128 |
description: |
| 129 |
- The scope for the variable. |
| 130 |
- Support for O(variables[].environment_scope) requires GitLab Premium >= 13.11. |
| 131 |
type: str |
| 132 |
default: '*' |
| 133 |
""" |
| 134 |
|
| 135 |
|
| 136 |
EXAMPLES = r""" |
| 137 |
- name: Set or update some CI/CD variables |
| 138 |
community.general.gitlab_project_variable: |
| 139 |
api_url: https://gitlab.com |
| 140 |
api_token: secret_access_token |
| 141 |
project: markuman/dotfiles |
| 142 |
purge: false |
| 143 |
variables: |
| 144 |
- name: ACCESS_KEY_ID |
| 145 |
value: abc123 |
| 146 |
- name: SECRET_ACCESS_KEY |
| 147 |
value: dassgrfaeui8989 |
| 148 |
masked: true |
| 149 |
protected: true |
| 150 |
environment_scope: production |
| 151 |
|
| 152 |
- name: Set or update some CI/CD variables |
| 153 |
community.general.gitlab_project_variable: |
| 154 |
api_url: https://gitlab.com |
| 155 |
api_token: secret_access_token |
| 156 |
project: markuman/dotfiles |
| 157 |
purge: false |
| 158 |
vars: |
| 159 |
ACCESS_KEY_ID: abc123 |
| 160 |
SECRET_ACCESS_KEY: |
| 161 |
value: 3214cbad |
| 162 |
masked: true |
| 163 |
protected: true |
| 164 |
variable_type: env_var |
| 165 |
environment_scope: '*' |
| 166 |
|
| 167 |
- name: Set or update some CI/CD variables with raw value |
| 168 |
community.general.gitlab_project_variable: |
| 169 |
api_url: https://gitlab.com |
| 170 |
api_token: secret_access_token |
| 171 |
project: markuman/dotfiles |
| 172 |
purge: false |
| 173 |
vars: |
| 174 |
ACCESS_KEY_ID: abc123 |
| 175 |
SECRET_ACCESS_KEY: |
| 176 |
value: 3214cbad |
| 177 |
masked: true |
| 178 |
protected: true |
| 179 |
raw: true |
| 180 |
variable_type: env_var |
| 181 |
environment_scope: '*' |
| 182 |
|
| 183 |
- name: Set or update some CI/CD variables with expandable value |
| 184 |
community.general.gitlab_project_variable: |
| 185 |
api_url: https://gitlab.com |
| 186 |
api_token: secret_access_token |
| 187 |
project: markuman/dotfiles |
| 188 |
purge: false |
| 189 |
vars: |
| 190 |
ACCESS_KEY_ID: abc123 |
| 191 |
SECRET_ACCESS_KEY: |
| 192 |
value: '$MY_OTHER_VARIABLE' |
| 193 |
masked: true |
| 194 |
protected: true |
| 195 |
raw: false |
| 196 |
variable_type: env_var |
| 197 |
environment_scope: '*' |
| 198 |
|
| 199 |
- name: Delete one variable |
| 200 |
community.general.gitlab_project_variable: |
| 201 |
api_url: https://gitlab.com |
| 202 |
api_token: secret_access_token |
| 203 |
project: markuman/dotfiles |
| 204 |
state: absent |
| 205 |
vars: |
| 206 |
ACCESS_KEY_ID: abc123 |
| 207 |
""" |
| 208 |
|
| 209 |
RETURN = r""" |
| 210 |
project_variable: |
| 211 |
description: Four lists of the variablenames which were added, updated, removed or exist. |
| 212 |
returned: always |
| 213 |
type: dict |
| 214 |
contains: |
| 215 |
added: |
| 216 |
description: A list of variables which were created. |
| 217 |
returned: always |
| 218 |
type: list |
| 219 |
sample: ["ACCESS_KEY_ID", "SECRET_ACCESS_KEY"] |
| 220 |
untouched: |
| 221 |
description: A list of variables which exist. |
| 222 |
returned: always |
| 223 |
type: list |
| 224 |
sample: ["ACCESS_KEY_ID", "SECRET_ACCESS_KEY"] |
| 225 |
removed: |
| 226 |
description: A list of variables which were deleted. |
| 227 |
returned: always |
| 228 |
type: list |
| 229 |
sample: ["ACCESS_KEY_ID", "SECRET_ACCESS_KEY"] |
| 230 |
updated: |
| 231 |
description: A list of variables whose values were changed. |
| 232 |
returned: always |
| 233 |
type: list |
| 234 |
sample: ["ACCESS_KEY_ID", "SECRET_ACCESS_KEY"] |
| 235 |
""" |
| 236 |
|
| 237 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 238 |
from ansible.module_utils.basic import AnsibleModule |
| 239 |
|
| 240 |
from ansible_collections.community.general.plugins.module_utils._gitlab import ( |
| 241 |
auth_argument_spec, |
| 242 |
filter_returned_variables, |
| 243 |
find_project, |
| 244 |
gitlab_authentication, |
| 245 |
list_all_kwargs, |
| 246 |
vars_to_variables, |
| 247 |
) |
| 248 |
|
| 249 |
|
| 250 |
class GitlabProjectVariables: |
| 251 |
def __init__(self, module, gitlab_instance): |
| 252 |
self.repo = gitlab_instance |
| 253 |
self._module = module |
| 254 |
self.project = self.get_project(module.params["project"]) |
| 255 |
|
| 256 |
def get_project(self, project_name): |
| 257 |
project = find_project(self.repo, project_name) |
| 258 |
if project is None: |
| 259 |
self._module.fail_json(msg=f"Project {project_name} not found or insufficient permissions.") |
| 260 |
return project |
| 261 |
|
| 262 |
def list_all_project_variables(self): |
| 263 |
return list(self.project.variables.list(**list_all_kwargs)) |
| 264 |
|
| 265 |
def create_variable(self, var_obj): |
| 266 |
if self._module.check_mode: |
| 267 |
return True |
| 268 |
|
| 269 |
var = { |
| 270 |
"key": var_obj.get("key"), |
| 271 |
"value": var_obj.get("value"), |
| 272 |
"description": var_obj.get("description"), |
| 273 |
"masked": var_obj.get("masked"), |
| 274 |
"masked_and_hidden": var_obj.get("hidden"), |
| 275 |
"protected": var_obj.get("protected"), |
| 276 |
"raw": var_obj.get("raw"), |
| 277 |
"variable_type": var_obj.get("variable_type"), |
| 278 |
} |
| 279 |
|
| 280 |
if var_obj.get("environment_scope") is not None: |
| 281 |
var["environment_scope"] = var_obj.get("environment_scope") |
| 282 |
|
| 283 |
self.project.variables.create(var) |
| 284 |
return True |
| 285 |
|
| 286 |
def update_variable(self, var_obj): |
| 287 |
if self._module.check_mode: |
| 288 |
return True |
| 289 |
self.delete_variable(var_obj) |
| 290 |
self.create_variable(var_obj) |
| 291 |
return True |
| 292 |
|
| 293 |
def delete_variable(self, var_obj): |
| 294 |
if self._module.check_mode: |
| 295 |
return True |
| 296 |
self.project.variables.delete( |
| 297 |
var_obj.get("key"), filter={"environment_scope": var_obj.get("environment_scope")} |
| 298 |
) |
| 299 |
return True |
| 300 |
|
| 301 |
|
| 302 |
def compare(requested_variables, existing_variables, state): |
| 303 |
# we need to do this, because it was determined in a previous version - more or less buggy |
| 304 |
# basically it is not necessary and might results in more/other bugs! |
| 305 |
# but it is required and only relevant for check mode!! |
| 306 |
# logic represents state 'present' when not purge. all other can be derived from that |
| 307 |
# untouched => equal in both |
| 308 |
# updated => name and scope are equal |
| 309 |
# added => name and scope does not exist |
| 310 |
untouched = list() |
| 311 |
updated = list() |
| 312 |
added = list() |
| 313 |
|
| 314 |
if state == "present": |
| 315 |
existing_key_scope_vars = list() |
| 316 |
for item in existing_variables: |
| 317 |
existing_key_scope_vars.append({"key": item.get("key"), "environment_scope": item.get("environment_scope")}) |
| 318 |
|
| 319 |
for var in requested_variables: |
| 320 |
if var in existing_variables: |
| 321 |
untouched.append(var) |
| 322 |
else: |
| 323 |
compare_item = {"key": var.get("name"), "environment_scope": var.get("environment_scope")} |
| 324 |
if compare_item in existing_key_scope_vars: |
| 325 |
updated.append(var) |
| 326 |
else: |
| 327 |
added.append(var) |
| 328 |
|
| 329 |
return untouched, updated, added |
| 330 |
|
| 331 |
|
| 332 |
def native_python_main(this_gitlab, purge, requested_variables, state, module): |
| 333 |
change = False |
| 334 |
return_value = dict(added=[], updated=[], removed=[], untouched=[]) |
| 335 |
|
| 336 |
gitlab_keys = this_gitlab.list_all_project_variables() |
| 337 |
before = [x.attributes for x in gitlab_keys] |
| 338 |
|
| 339 |
gitlab_keys = this_gitlab.list_all_project_variables() |
| 340 |
existing_variables = filter_returned_variables(gitlab_keys) |
| 341 |
|
| 342 |
# filter out and enrich before compare |
| 343 |
for item in requested_variables: |
| 344 |
item["key"] = item.pop("name") |
| 345 |
item["value"] = str(item.get("value")) |
| 346 |
if item.get("protected") is None: |
| 347 |
item["protected"] = False |
| 348 |
if item.get("raw") is None: |
| 349 |
item["raw"] = False |
| 350 |
if item.get("masked") is None: |
| 351 |
item["masked"] = False |
| 352 |
if item.get("hidden") is None: |
| 353 |
item["hidden"] = False |
| 354 |
if item.get("environment_scope") is None: |
| 355 |
item["environment_scope"] = "*" |
| 356 |
if item.get("variable_type") is None: |
| 357 |
item["variable_type"] = "env_var" |
| 358 |
|
| 359 |
if module.check_mode: |
| 360 |
untouched, updated, added = compare(requested_variables, existing_variables, state) |
| 361 |
|
| 362 |
if state == "present": |
| 363 |
add_or_update = [x for x in requested_variables if x not in existing_variables] |
| 364 |
for item in add_or_update: |
| 365 |
try: |
| 366 |
if this_gitlab.create_variable(item): |
| 367 |
return_value["added"].append(item) |
| 368 |
|
| 369 |
except Exception: |
| 370 |
if this_gitlab.update_variable(item): |
| 371 |
return_value["updated"].append(item) |
| 372 |
|
| 373 |
if purge: |
| 374 |
# refetch and filter |
| 375 |
gitlab_keys = this_gitlab.list_all_project_variables() |
| 376 |
existing_variables = filter_returned_variables(gitlab_keys) |
| 377 |
|
| 378 |
remove = [x for x in existing_variables if x not in requested_variables] |
| 379 |
for item in remove: |
| 380 |
if this_gitlab.delete_variable(item): |
| 381 |
return_value["removed"].append(item) |
| 382 |
|
| 383 |
elif state == "absent": |
| 384 |
# value, type, and description do not matter on removing variables. |
| 385 |
keys_ignored_on_deletion = ["value", "variable_type", "description"] |
| 386 |
for key in keys_ignored_on_deletion: |
| 387 |
for item in existing_variables: |
| 388 |
item.pop(key) |
| 389 |
for item in requested_variables: |
| 390 |
item.pop(key) |
| 391 |
|
| 392 |
if not purge: |
| 393 |
remove_requested = [x for x in requested_variables if x in existing_variables] |
| 394 |
for item in remove_requested: |
| 395 |
if this_gitlab.delete_variable(item): |
| 396 |
return_value["removed"].append(item) |
| 397 |
|
| 398 |
else: |
| 399 |
for item in existing_variables: |
| 400 |
if this_gitlab.delete_variable(item): |
| 401 |
return_value["removed"].append(item) |
| 402 |
|
| 403 |
if module.check_mode: |
| 404 |
return_value = dict(added=added, updated=updated, removed=return_value["removed"], untouched=untouched) |
| 405 |
|
| 406 |
if any(return_value[x] for x in ["added", "removed", "updated"]): |
| 407 |
change = True |
| 408 |
|
| 409 |
gitlab_keys = this_gitlab.list_all_project_variables() |
| 410 |
after = [x.attributes for x in gitlab_keys] |
| 411 |
|
| 412 |
return change, return_value, before, after |
| 413 |
|
| 414 |
|
| 415 |
def main(): |
| 416 |
argument_spec = basic_auth_argument_spec() |
| 417 |
argument_spec.update(auth_argument_spec()) |
| 418 |
argument_spec.update( |
| 419 |
project=dict(type="str", required=True), |
| 420 |
purge=dict(type="bool", default=False), |
| 421 |
vars=dict(type="dict", default=dict(), no_log=True), |
| 422 |
# please mind whenever changing the variables dict to also change module_utils/gitlab.py's |
| 423 |
# KNOWN dict in filter_returned_variables or bad evil will happen |
| 424 |
variables=dict( |
| 425 |
type="list", |
| 426 |
elements="dict", |
| 427 |
default=list(), |
| 428 |
options=dict( |
| 429 |
name=dict(type="str", required=True), |
| 430 |
value=dict(type="str", no_log=True), |
| 431 |
description=dict(type="str"), |
| 432 |
masked=dict(type="bool", default=False), |
| 433 |
hidden=dict(type="bool", default=False), |
| 434 |
protected=dict(type="bool", default=False), |
| 435 |
raw=dict(type="bool", default=False), |
| 436 |
environment_scope=dict(type="str", default="*"), |
| 437 |
variable_type=dict(type="str", default="env_var", choices=["env_var", "file"]), |
| 438 |
), |
| 439 |
), |
| 440 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 441 |
) |
| 442 |
|
| 443 |
module = AnsibleModule( |
| 444 |
argument_spec=argument_spec, |
| 445 |
mutually_exclusive=[ |
| 446 |
["api_username", "api_token"], |
| 447 |
["api_username", "api_oauth_token"], |
| 448 |
["api_username", "api_job_token"], |
| 449 |
["api_token", "api_oauth_token"], |
| 450 |
["api_token", "api_job_token"], |
| 451 |
["vars", "variables"], |
| 452 |
], |
| 453 |
required_together=[ |
| 454 |
["api_username", "api_password"], |
| 455 |
], |
| 456 |
required_one_of=[["api_username", "api_token", "api_oauth_token", "api_job_token"]], |
| 457 |
supports_check_mode=True, |
| 458 |
) |
| 459 |
|
| 460 |
# check prerequisites and connect to gitlab server |
| 461 |
gitlab_instance = gitlab_authentication(module) |
| 462 |
|
| 463 |
purge = module.params["purge"] |
| 464 |
var_list = module.params["vars"] |
| 465 |
state = module.params["state"] |
| 466 |
|
| 467 |
if var_list: |
| 468 |
variables = vars_to_variables(var_list, module) |
| 469 |
else: |
| 470 |
variables = module.params["variables"] |
| 471 |
|
| 472 |
if state == "present": |
| 473 |
if any(x["value"] is None for x in variables): |
| 474 |
module.fail_json(msg="value parameter is required for all variables in state present") |
| 475 |
|
| 476 |
this_gitlab = GitlabProjectVariables(module=module, gitlab_instance=gitlab_instance) |
| 477 |
|
| 478 |
change, raw_return_value, before, after = native_python_main(this_gitlab, purge, variables, state, module) |
| 479 |
|
| 480 |
# postprocessing |
| 481 |
for item in after: |
| 482 |
item.pop("project_id") |
| 483 |
item["name"] = item.pop("key") |
| 484 |
for item in before: |
| 485 |
item.pop("project_id") |
| 486 |
item["name"] = item.pop("key") |
| 487 |
|
| 488 |
untouched_key_name = "key" |
| 489 |
if not module.check_mode: |
| 490 |
untouched_key_name = "name" |
| 491 |
raw_return_value["untouched"] = [x for x in before if x in after] |
| 492 |
|
| 493 |
added = [x.get("key") for x in raw_return_value["added"]] |
| 494 |
updated = [x.get("key") for x in raw_return_value["updated"]] |
| 495 |
removed = [x.get("key") for x in raw_return_value["removed"]] |
| 496 |
untouched = [x.get(untouched_key_name) for x in raw_return_value["untouched"]] |
| 497 |
return_value = dict(added=added, updated=updated, removed=removed, untouched=untouched) |
| 498 |
|
| 499 |
module.exit_json(changed=change, project_variable=return_value) |
| 500 |
|
| 501 |
|
| 502 |
if __name__ == "__main__": |
| 503 |
main() |