-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab_env.py
48 lines (40 loc) · 1.39 KB
/
gitlab_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# (c) 2017, Ron Arts <[email protected]>
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = """
lookup: gitlab_env
author: Ron Arts <[email protected]>
version_added: "0.1"
short_description: Read all GitLab CI variables, and the ones prefixed with a given value
description:
- Allows you to query the environment variables available during GitLab CI builds
options:
_terms:
description: Environment variable or list of them to lookup the values for
required: True
"""
EXAMPLES = """
- environment: "{{ lookup('gitlab_env','ENV', 'COMMON') }}"
"""
RETURN = """
_list:
description:
- all CI environment variables unchanged, and some with prefixes removed
type: list
"""
import os
from ansible.plugins.lookup import LookupBase
from ansible.module_utils._text import to_text
class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
env = {}
for var in os.environ:
if (var.startswith('CI_')):
env[var] = to_text(os.getenv(var))
for prefix_var in terms:
prefix = to_text(os.getenv(prefix_var))
env[prefix_var] = prefix
for var in os.environ:
if (var.startswith(prefix + '_')):
env[var[len(prefix)+1:]] = to_text(os.getenv(var))
return [env]