Last active
August 20, 2017 13:14
-
-
Save kpcyrd/1bde93b2bb9c77e0b6c14a2b3f75b026 to your computer and use it in GitHub Desktop.
ansible-gitlab_settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | |
from __future__ import absolute_import, division, print_function | |
__metaclass__ = type | |
ANSIBLE_METADATA = {'metadata_version': '0.0', | |
'status': ['preview'], | |
'supported_by': 'community'} | |
''' | |
DOCUMENTATION = | |
--- | |
module: gitlab_settings | |
short_description: Updates Gitlab Settings | |
''' | |
EXAMPLES = ''' | |
- name: Update Gitlab Setting | |
gitlab_settings: | |
server_url: http://gitlab.example.com | |
login_token: XyKpCyRd1 | |
settings: | |
domain_blacklist_enabled: true | |
foo: bar | |
abc: | |
- xyz | |
''' | |
RETURN = '''# ''' | |
try: | |
# python2 | |
from urlparse import urljoin | |
except ImportError: | |
# python3 | |
from urllib.parse import urljoin | |
try: | |
import requests | |
HAS_REQUESTS_PACKAGE = True | |
except ImportError: | |
HAS_REQUESTS_PACKAGE = False | |
from ansible.module_utils.basic import AnsibleModule | |
from ansible.module_utils._text import to_native | |
def get_settings(server_url, login_token): | |
url = urljoin(server_url, '/api/v4/application/settings') | |
r = requests.get(url, headers={ | |
'PRIVATE-TOKEN': login_token | |
}) | |
r.raise_for_status() | |
return r.json() | |
def check_if_changes_needed(wanted_settings, current_settings): | |
for key, value in wanted_settings.items(): | |
if current_settings[key] != value: | |
return True | |
else: | |
return False | |
def transform_settings(settings): | |
return {'%s[]' % k if type(v) is list else k: v for k, v in settings.items()} | |
def put_settings(server_url, login_token, settings): | |
url = urljoin(server_url, '/api/v4/application/settings') | |
r = requests.put(url, params=transform_settings(settings), headers={ | |
'PRIVATE-TOKEN': login_token | |
}) | |
r.raise_for_status() | |
return r.json() | |
def main(): | |
module = AnsibleModule( | |
argument_spec=dict( | |
server_url=dict(required=True), | |
login_token=dict(required=True), | |
settings=dict(required=True, type='dict'), | |
), | |
supports_check_mode=True, | |
) | |
if not HAS_REQUESTS_PACKAGE: | |
module.fail_json(msg='Missing required requests module (check docs or install with pip install requests') | |
server_url = module.params['server_url'] | |
login_token = module.params['login_token'] | |
wanted_settings = module.params['settings'] | |
current_settings = get_settings(server_url, login_token) | |
changes_needed = check_if_changes_needed(wanted_settings, current_settings) | |
if module.check_mode: | |
module.exit_json(changed=changes_needed) | |
elif changes_needed: | |
put_settings(server_url, login_token, wanted_settings) | |
module.exit_json(changed=True) | |
else: | |
module.exit_json(changed=False) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment