Browse Source

ansible module draft so setting gitlab ci/cd variables

master
Markus Bergholz 5 months ago
parent
commit
23c93b45f5
1 changed files with 134 additions and 0 deletions
  1. 134
    0
      ansible/library/gitlab_project_variables.py

+ 134
- 0
ansible/library/gitlab_project_variables.py View File

@@ -0,0 +1,134 @@
#!/usr/bin/python

from ansible.module_utils.basic import *
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}


DOCUMENTATION = '''
'''


EXAMPLES = '''
- name: Set or update some CI/CD variables
gitlab_project_variables:
host: gitlab.com
token: secret_access_token
name: markuman/dotfiles
purge_vars: False
vars:
- ACCESS_KEY_ID: abc123
- SECRET_ACCESS_KEY: 321cba

'''

RETURN = '''# '''


try:
import gitlab
HAS_GITLAB_PACKAGE = True
except:
HAS_GITLAB_PACKAGE = False

try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()


class gitlab_project_variables(object):

def __init__(self, token, project_name, host):
self.repo = gitlab.Gitlab(
'https://{host}'.format(host=host), private_token=token)
self.repo.auth()
self.project = self.get_project(project_name)

def get_project(self, project_name):
return self.repo.projects.get(project_name)

def list_all_project_variables(self):
raw_variable_list = self.project.variables.list()
retval = []
if len(raw_variable_list) > 0:
for item in raw_variable_list:
retval.append(item.get_id())
return retval

def create_variable(self, key, value):
return self.project.variables.create({"key": key, "value": value})

def update_variable(self, key, value):
var = self.project.variables.get(key)
if var.value == value:
return False
var.save()
return True

def delete_variable(self, key):
return self.project.variables.delete(key)


def native_python_main(host, token, project_name, purge_vars, var_list):

change = False
this_gitlab = gitlab_project_variables(
token=token, project_name=project_name, host=host)

existing_variables = this_gitlab.list_all_project_variables()

for idx in range(len(var_list)):
key = list(var_list[idx].keys())[0]
if key not in existing_variables:
display.vvvv("create {key}".format(key=key))
this_gitlab.create_variable(key, var_list[idx][key])
change = True
else:
display.vvvv("update {key}".format(key=key))
change = this_gitlab.update_variable(
key, var_list[idx][key]) or change
pop_index = existing_variables.index(key)
existing_variables.pop(pop_index)
if len(existing_variables) > 0 and purge_vars:
for item in existing_variables:
display.vvvv("delete {key}".format(key=item))
this_gitlab.delete_variable(item)
change = True

existing_variables = this_gitlab.list_all_project_variables()
display.vvvv(existing_variables)
return change


def main():
module = AnsibleModule(
argument_spec=dict(
host=dict(required=True, type='str'),
token=dict(required=True, type='str'),
name=dict(required=True, type='str'),
purge_vars=dict(required=False, default=False, type='bool'),
vars=dict(required=False, default=list(), type='list')
)
)

if not HAS_GITLAB_PACKAGE:
module.fail_json(
msg="Missing required gitlab module (check docs or install with: pip install python-gitlab")

host = module.params['host']
token = module.params['token']
purge_vars = module.params['purge_vars']
var_list = module.params['vars']
project_name = module.params['name']

change = native_python_main(
host, token, project_name, purge_vars, var_list)

module.exit_json(changed=change, gitlab_project_variables=None)


if __name__ == '__main__':
main()

Loading…
Cancel
Save