def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(auth_argument_spec()) argument_spec.update(project=dict(type='str', required=True), purge=dict(type='bool', required=False, default=False), vars=dict(type='dict', required=False, default=dict(), no_log=True), state=dict(type='str', default="present", choices=["absent", "present"])) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_username', 'api_oauth_token'], ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], ], required_together=[ ['api_username', 'api_password'], ], required_one_of=[[ 'api_username', 'api_token', 'api_oauth_token', 'api_job_token' ]], supports_check_mode=True) purge = module.params['purge'] var_list = module.params['vars'] state = module.params['state'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_instance = gitlab_authentication(module) this_gitlab = GitlabProjectVariables(module=module, gitlab_instance=gitlab_instance) change, return_value = native_python_main(this_gitlab, purge, var_list, state, module) module.exit_json(changed=change, project_variable=return_value)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(auth_argument_spec()) argument_spec.update( project=dict(type='str', required=True), name=dict(type='str', required=True), merge_access_levels=dict(type='str', default="maintainer", choices=["maintainer", "developer", "nobody"]), push_access_level=dict(type='str', default="maintainer", choices=["maintainer", "developer", "nobody"]), state=dict(type='str', default="present", choices=["absent", "present"]), ) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_username', 'api_oauth_token'], ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], ], required_together=[ ['api_username', 'api_password'], ], required_one_of=[[ 'api_username', 'api_token', 'api_oauth_token', 'api_job_token' ]], supports_check_mode=True) project = module.params['project'] name = module.params['name'] merge_access_levels = module.params['merge_access_levels'] push_access_level = module.params['push_access_level'] state = module.params['state'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_version = gitlab.__version__ if LooseVersion(gitlab_version) < LooseVersion('2.3.0'): module.fail_json( msg= "community.general.gitlab_proteched_branch requires python-gitlab Python module >= 2.3.0 (installed version: [%s])." " Please upgrade python-gitlab to version 2.3.0 or above." % gitlab_version) gitlab_instance = gitlab_authentication(module) this_gitlab = GitlabProtectedBranch(module=module, project=project, gitlab_instance=gitlab_instance) p_branch = this_gitlab.protected_branch_exist(name=name) if not p_branch and state == "present": this_gitlab.create_protected_branch( name=name, merge_access_levels=merge_access_levels, push_access_level=push_access_level) module.exit_json(changed=True, msg="Created the proteched branch.") elif p_branch and state == "present": if not this_gitlab.compare_protected_branch(name, merge_access_levels, push_access_level): this_gitlab.delete_protected_branch(name=name) this_gitlab.create_protected_branch( name=name, merge_access_levels=merge_access_levels, push_access_level=push_access_level) module.exit_json(changed=True, msg="Recreated the proteched branch.") elif p_branch and state == "absent": this_gitlab.delete_protected_branch(name=name) module.exit_json(changed=True, msg="Deleted the proteched branch.") module.exit_json(changed=False, msg="No changes are needed.")
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(auth_argument_spec()) argument_spec.update( project=dict(type='str', required=True), branch=dict(type='str', required=True), ref_branch=dict(type='str', required=False), state=dict(type='str', default="present", choices=["absent", "present"]), ) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_username', 'api_oauth_token'], ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], ], required_together=[ ['api_username', 'api_password'], ], required_one_of=[[ 'api_username', 'api_token', 'api_oauth_token', 'api_job_token' ]], required_if=[ ['state', 'present', ['ref_branch'], True], ], supports_check_mode=False) project = module.params['project'] branch = module.params['branch'] ref_branch = module.params['ref_branch'] state = module.params['state'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_version = gitlab.__version__ if LooseVersion(gitlab_version) < LooseVersion('2.3.0'): module.fail_json( msg= "community.general.gitlab_proteched_branch requires python-gitlab Python module >= 2.3.0 (installed version: [%s])." " Please upgrade python-gitlab to version 2.3.0 or above." % gitlab_version) gitlab_instance = gitlab_authentication(module) this_gitlab = GitlabBranch(module=module, project=project, gitlab_instance=gitlab_instance) this_branch = this_gitlab.get_branch(branch) if not this_branch and state == "present": r_branch = this_gitlab.get_branch(ref_branch) if not r_branch: module.fail_json(msg="Ref branch {b} not exist.".format( b=ref_branch)) this_gitlab.create_branch(branch, ref_branch) module.exit_json(changed=True, msg="Created the branch {b}.".format(b=branch)) elif this_branch and state == "present": module.exit_json(changed=False, msg="Branch {b} already exist".format(b=branch)) elif this_branch and state == "absent": try: this_gitlab.delete_branch(this_branch) module.exit_json(changed=True, msg="Branch {b} deleted.".format(b=branch)) except Exception as e: module.fail_json(msg="Error delete branch.", exception=traceback.format_exc()) else: module.exit_json(changed=False, msg="No changes are needed.")
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update( dict( api_token=dict(type='str', no_log=True), name=dict(type='str', required=True), path=dict(type='str'), description=dict(type='str'), state=dict(type='str', default="present", choices=["absent", "present"]), parent=dict(type='str'), visibility=dict(type='str', default="private", choices=["internal", "private", "public"]), project_creation_level=dict( type='str', choices=['developer', 'maintainer', 'noone']), auto_devops_enabled=dict(type='bool'), subgroup_creation_level=dict(type='str', choices=['maintainer', 'owner']), require_two_factor_authentication=dict(type='bool'), avatar_path=dict(type='path'), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_password', 'api_token'], ], required_together=[ ['api_username', 'api_password'], ], required_one_of=[['api_username', 'api_token']], supports_check_mode=True, ) group_name = module.params['name'] group_path = module.params['path'] description = module.params['description'] state = module.params['state'] parent_identifier = module.params['parent'] group_visibility = module.params['visibility'] project_creation_level = module.params['project_creation_level'] auto_devops_enabled = module.params['auto_devops_enabled'] subgroup_creation_level = module.params['subgroup_creation_level'] require_two_factor_authentication = module.params[ 'require_two_factor_authentication'] avatar_path = module.params['avatar_path'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_instance = gitlab_authentication(module) # Define default group_path based on group_name if group_path is None: group_path = group_name.replace(" ", "_") gitlab_group = GitLabGroup(module, gitlab_instance) parent_group = None if parent_identifier: parent_group = find_group(gitlab_instance, parent_identifier) if not parent_group: module.fail_json( msg="Failed create GitLab group: Parent group doesn't exists") group_exists = gitlab_group.exists_group(parent_group.full_path + '/' + group_path) else: group_exists = gitlab_group.exists_group(group_path) if state == 'absent': if group_exists: gitlab_group.delete_group() module.exit_json(changed=True, msg="Successfully deleted group %s" % group_name) else: module.exit_json(changed=False, msg="Group deleted or does not exists") if state == 'present': if gitlab_group.create_or_update_group( group_name, parent_group, { "path": group_path, "description": description, "visibility": group_visibility, "project_creation_level": project_creation_level, "auto_devops_enabled": auto_devops_enabled, "subgroup_creation_level": subgroup_creation_level, "require_two_factor_authentication": require_two_factor_authentication, "avatar_path": avatar_path, }): module.exit_json( changed=True, msg="Successfully created or updated the group %s" % group_name, group=gitlab_group.group_object._attrs) else: module.exit_json(changed=False, msg="No need to update the group %s" % group_name, group=gitlab_group.group_object._attrs)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(auth_argument_spec()) argument_spec.update( dict( name=dict(type='str'), state=dict(type='str', default="present", choices=["absent", "present", "blocked", "unblocked"]), username=dict(type='str', required=True), password=dict(type='str', no_log=True), reset_password=dict(type='bool', default=False, no_log=False), email=dict(type='str'), sshkey_name=dict(type='str'), sshkey_file=dict(type='str', no_log=False), sshkey_expires_at=dict(type='str', no_log=False), group=dict(type='str'), access_level=dict(type='str', default="guest", choices=[ "developer", "guest", "maintainer", "master", "owner", "reporter" ]), confirm=dict(type='bool', default=True), isadmin=dict(type='bool', default=False), external=dict(type='bool', default=False), identities=dict(type='list', elements='dict'), overwrite_identities=dict(type='bool', default=False), )) module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_username', 'api_oauth_token'], ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], ], required_together=[ ['api_username', 'api_password'], ], required_one_of=[[ 'api_username', 'api_token', 'api_oauth_token', 'api_job_token' ]], supports_check_mode=True, required_if=(('state', 'present', ['name', 'email']), )) user_name = module.params['name'] state = module.params['state'] user_username = module.params['username'].lower() user_password = module.params['password'] user_reset_password = module.params['reset_password'] user_email = module.params['email'] user_sshkey_name = module.params['sshkey_name'] user_sshkey_file = module.params['sshkey_file'] user_sshkey_expires_at = module.params['sshkey_expires_at'] group_path = module.params['group'] access_level = module.params['access_level'] confirm = module.params['confirm'] user_isadmin = module.params['isadmin'] user_external = module.params['external'] user_identities = module.params['identities'] overwrite_identities = module.params['overwrite_identities'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_instance = gitlab_authentication(module) gitlab_user = GitLabUser(module, gitlab_instance) user_exists = gitlab_user.exists_user(user_username) if user_exists: user_is_active = gitlab_user.is_active(user_username) else: user_is_active = False if state == 'absent': if user_exists: gitlab_user.delete_user() module.exit_json(changed=True, msg="Successfully deleted user %s" % user_username) else: module.exit_json(changed=False, msg="User deleted or does not exists") if state == 'blocked': if user_exists and user_is_active: gitlab_user.block_user() module.exit_json(changed=True, msg="Successfully blocked user %s" % user_username) else: module.exit_json(changed=False, msg="User already blocked or does not exists") if state == 'unblocked': if user_exists and not user_is_active: gitlab_user.unblock_user() module.exit_json(changed=True, msg="Successfully unblocked user %s" % user_username) else: module.exit_json(changed=False, msg="User is not blocked or does not exists") if state == 'present': if gitlab_user.create_or_update_user( user_username, { "name": user_name, "password": user_password, "reset_password": user_reset_password, "email": user_email, "sshkey_name": user_sshkey_name, "sshkey_file": user_sshkey_file, "sshkey_expires_at": user_sshkey_expires_at, "group_path": group_path, "access_level": access_level, "confirm": confirm, "isadmin": user_isadmin, "external": user_external, "identities": user_identities, "overwrite_identities": overwrite_identities, }): module.exit_json( changed=True, msg="Successfully created or updated the user %s" % user_username, user=gitlab_user.user_object._attrs) else: module.exit_json(changed=False, msg="No need to update the user %s" % user_username, user=gitlab_user.user_object._attrs)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(dict( api_token=dict(type='str', no_log=True), state=dict(type='str', default="present", choices=["absent", "present"]), project=dict(type='str', required=True), key=dict(type='str', required=True, no_log=False), can_push=dict(type='bool', default=False), title=dict(type='str', required=True) )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_password', 'api_token'] ], required_together=[ ['api_username', 'api_password'] ], required_one_of=[ ['api_username', 'api_token'] ], supports_check_mode=True, ) state = module.params['state'] project_identifier = module.params['project'] key_title = module.params['title'] key_keyfile = module.params['key'] key_can_push = module.params['can_push'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_instance = gitlab_authentication(module) gitlab_deploy_key = GitLabDeployKey(module, gitlab_instance) project = find_project(gitlab_instance, project_identifier) if project is None: module.fail_json(msg="Failed to create deploy key: project %s doesn't exists" % project_identifier) deploy_key_exists = gitlab_deploy_key.exists_deploy_key(project, key_title) if state == 'absent': if deploy_key_exists: gitlab_deploy_key.delete_deploy_key() module.exit_json(changed=True, msg="Successfully deleted deploy key %s" % key_title) else: module.exit_json(changed=False, msg="Deploy key deleted or does not exists") if state == 'present': if gitlab_deploy_key.create_or_update_deploy_key(project, key_title, key_keyfile, {'can_push': key_can_push}): module.exit_json(changed=True, msg="Successfully created or updated the deploy key %s" % key_title, deploy_key=gitlab_deploy_key.deploy_key_object._attrs) else: module.exit_json(changed=False, msg="No need to update the deploy key %s" % key_title, deploy_key=gitlab_deploy_key.deploy_key_object._attrs)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(auth_argument_spec()) argument_spec.update(dict( state=dict(type='str', default="present", choices=["absent", "present"]), project=dict(type='str', required=True), hook_url=dict(type='str', required=True), push_events=dict(type='bool', default=True), push_events_branch_filter=dict(type='str', default=''), issues_events=dict(type='bool', default=False), merge_requests_events=dict(type='bool', default=False), tag_push_events=dict(type='bool', default=False), note_events=dict(type='bool', default=False), job_events=dict(type='bool', default=False), pipeline_events=dict(type='bool', default=False), wiki_page_events=dict(type='bool', default=False), hook_validate_certs=dict(type='bool', default=False, aliases=['enable_ssl_verification']), token=dict(type='str', no_log=True), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_username', 'api_oauth_token'], ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], ], required_together=[ ['api_username', 'api_password'] ], required_one_of=[ ['api_username', 'api_token', 'api_oauth_token', 'api_job_token'] ], supports_check_mode=True, ) state = module.params['state'] project_identifier = module.params['project'] hook_url = module.params['hook_url'] push_events = module.params['push_events'] push_events_branch_filter = module.params['push_events_branch_filter'] issues_events = module.params['issues_events'] merge_requests_events = module.params['merge_requests_events'] tag_push_events = module.params['tag_push_events'] note_events = module.params['note_events'] job_events = module.params['job_events'] pipeline_events = module.params['pipeline_events'] wiki_page_events = module.params['wiki_page_events'] enable_ssl_verification = module.params['hook_validate_certs'] hook_token = module.params['token'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_instance = gitlab_authentication(module) gitlab_hook = GitLabHook(module, gitlab_instance) project = find_project(gitlab_instance, project_identifier) if project is None: module.fail_json(msg="Failed to create hook: project %s doesn't exists" % project_identifier) hook_exists = gitlab_hook.exists_hook(project, hook_url) if state == 'absent': if hook_exists: gitlab_hook.delete_hook() module.exit_json(changed=True, msg="Successfully deleted hook %s" % hook_url) else: module.exit_json(changed=False, msg="Hook deleted or does not exists") if state == 'present': if gitlab_hook.create_or_update_hook(project, hook_url, { "push_events": push_events, "push_events_branch_filter": push_events_branch_filter, "issues_events": issues_events, "merge_requests_events": merge_requests_events, "tag_push_events": tag_push_events, "note_events": note_events, "job_events": job_events, "pipeline_events": pipeline_events, "wiki_page_events": wiki_page_events, "enable_ssl_verification": enable_ssl_verification, "token": hook_token, }): module.exit_json(changed=True, msg="Successfully created or updated the hook %s" % hook_url, hook=gitlab_hook.hook_object._attrs) else: module.exit_json(changed=False, msg="No need to update the hook %s" % hook_url, hook=gitlab_hook.hook_object._attrs)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(auth_argument_spec()) argument_spec.update( dict( project=dict(type='str', required=True), gitlab_user=dict(type='list', elements='str'), state=dict(type='str', default='present', choices=['present', 'absent']), access_level=dict( type='str', choices=['guest', 'reporter', 'developer', 'maintainer']), purge_users=dict( type='list', elements='str', choices=['guest', 'reporter', 'developer', 'maintainer']), gitlab_users_access=dict(type='list', elements='dict', options=dict( name=dict(type='str', required=True), access_level=dict(type='str', choices=[ 'guest', 'reporter', 'developer', 'maintainer' ], required=True), )), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_username', 'api_oauth_token'], ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], ['gitlab_user', 'gitlab_users_access'], ['access_level', 'gitlab_users_access'], ], required_together=[ ['api_username', 'api_password'], ['gitlab_user', 'access_level'], ], required_one_of=[ ['api_username', 'api_token', 'api_oauth_token', 'api_job_token'], ['gitlab_user', 'gitlab_users_access'], ], required_if=[ [ 'state', 'present', ['access_level', 'gitlab_users_access'], True ], ], supports_check_mode=True, ) if not HAS_PY_GITLAB: module.fail_json(msg=missing_required_lib( 'python-gitlab', url='https://python-gitlab.readthedocs.io/en/stable/'), exception=GITLAB_IMP_ERR) access_level_int = { 'guest': gitlab.GUEST_ACCESS, 'reporter': gitlab.REPORTER_ACCESS, 'developer': gitlab.DEVELOPER_ACCESS, 'maintainer': gitlab.MAINTAINER_ACCESS, } gitlab_project = module.params['project'] state = module.params['state'] access_level = module.params['access_level'] purge_users = module.params['purge_users'] if purge_users: purge_users = [access_level_int[level] for level in purge_users] # connect to gitlab server gl = gitlab_authentication(module) project = GitLabProjectMembers(module, gl) gitlab_project_id = project.get_project(gitlab_project) # project doesn't exist if not gitlab_project_id: module.fail_json(msg="project '%s' not found." % gitlab_project) members = [] if module.params['gitlab_user'] is not None: gitlab_users_access = [] gitlab_users = module.params['gitlab_user'] for gl_user in gitlab_users: gitlab_users_access.append({ 'name': gl_user, 'access_level': access_level_int[access_level] if access_level else None }) elif module.params['gitlab_users_access'] is not None: gitlab_users_access = module.params['gitlab_users_access'] for user_level in gitlab_users_access: user_level['access_level'] = access_level_int[ user_level['access_level']] if len(gitlab_users_access) == 1 and not purge_users: # only single user given members = [ project.get_member_in_a_project( gitlab_project_id, project.get_user_id(gitlab_users_access[0]['name'])) ] if members[0] is None: members = [] elif len(gitlab_users_access) > 1 or purge_users: # list of users given members = project.get_members_in_a_project(gitlab_project_id) else: module.exit_json( changed='OK', result= "Nothing to do, please give at least one user or set purge_users true.", result_data=[]) changed = False error = False changed_users = [] changed_data = [] for gitlab_user in gitlab_users_access: gitlab_user_id = project.get_user_id(gitlab_user['name']) # user doesn't exist if not gitlab_user_id: if state == 'absent': changed_users.append( "user '%s' not found, and thus also not part of the project" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'OK', 'msg': "user '%s' not found, and thus also not part of the project" % gitlab_user['name'] }) else: error = True changed_users.append("user '%s' not found." % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'FAILED', 'msg': "user '%s' not found." % gitlab_user['name'] }) continue is_user_a_member = project.is_user_a_member(members, gitlab_user_id) # check if the user is a member in the project if not is_user_a_member: if state == 'present': # add user to the project try: if not module.check_mode: project.add_member_to_project( gitlab_user_id, gitlab_project_id, gitlab_user['access_level']) changed = True changed_users.append( "Successfully added user '%s' to project" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'CHANGED', 'msg': "Successfully added user '%s' to project" % gitlab_user['name'] }) except (gitlab.exceptions.GitlabCreateError) as e: error = True changed_users.append( "Failed to updated the access level for the user, '%s'" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'FAILED', 'msg': "Not allowed to add the access level for the member, %s: %s" % (gitlab_user['name'], e) }) # state as absent else: changed_users.append( "User, '%s', is not a member in the project. No change to report" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'OK', 'msg': "User, '%s', is not a member in the project. No change to report" % gitlab_user['name'] }) # in case that a user is a member else: if state == 'present': # compare the access level user_access_level = project.get_user_access_level( members, gitlab_user_id) if user_access_level == gitlab_user['access_level']: changed_users.append( "User, '%s', is already a member in the project. No change to report" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'OK', 'msg': "User, '%s', is already a member in the project. No change to report" % gitlab_user['name'] }) else: # update the access level for the user try: if not module.check_mode: project.update_user_access_level( members, gitlab_user_id, gitlab_user['access_level']) changed = True changed_users.append( "Successfully updated the access level for the user, '%s'" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'CHANGED', 'msg': "Successfully updated the access level for the user, '%s'" % gitlab_user['name'] }) except (gitlab.exceptions.GitlabUpdateError) as e: error = True changed_users.append( "Failed to updated the access level for the user, '%s'" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'FAILED', 'msg': "Not allowed to update the access level for the member, %s: %s" % (gitlab_user['name'], e) }) else: # remove the user from the project try: if not module.check_mode: project.remove_user_from_project( gitlab_user_id, gitlab_project_id) changed = True changed_users.append( "Successfully removed user, '%s', from the project" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'CHANGED', 'msg': "Successfully removed user, '%s', from the project" % gitlab_user['name'] }) except (gitlab.exceptions.GitlabDeleteError) as e: error = True changed_users.append( "Failed to removed user, '%s', from the project" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'FAILED', 'msg': "Failed to remove user, '%s' from the project: %s" % (gitlab_user['name'], e) }) # if state = present and purge_users set delete users which are in members having give access level but not in gitlab_users if state == 'present' and purge_users: uppercase_names_in_gitlab_users_access = [] for name in gitlab_users_access: uppercase_names_in_gitlab_users_access.append(name['name'].upper()) for member in members: if member.access_level in purge_users and member.username.upper( ) not in uppercase_names_in_gitlab_users_access: try: if not module.check_mode: project.remove_user_from_project( member.id, gitlab_project_id) changed = True changed_users.append( "Successfully removed user '%s', from project. Was not in given list" % member.username) changed_data.append({ 'gitlab_user': member.username, 'result': 'CHANGED', 'msg': "Successfully removed user '%s', from project. Was not in given list" % member.username }) except (gitlab.exceptions.GitlabDeleteError) as e: error = True changed_users.append( "Failed to removed user, '%s', from the project" % gitlab_user['name']) changed_data.append({ 'gitlab_user': gitlab_user['name'], 'result': 'FAILED', 'msg': "Failed to remove user, '%s' from the project: %s" % (gitlab_user['name'], e) }) if len(gitlab_users_access) == 1 and error: # if single user given and an error occurred return error for list errors will be per user module.fail_json(msg="FAILED: '%s '" % changed_users[0], result_data=changed_data) elif error: module.fail_json( msg='FAILED: At least one given user/permission could not be set', result_data=changed_data) module.exit_json(changed=changed, msg='Successfully set memberships', result="\n".join(changed_users), result_data=changed_data)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update( dict( api_token=dict(type='str', no_log=True), description=dict(type='str', required=True, aliases=["name"]), active=dict(type='bool', default=True), owned=dict(type='bool', default=False), tag_list=dict(type='list', elements='str', default=[]), run_untagged=dict(type='bool', default=True), locked=dict(type='bool', default=False), access_level=dict(type='str', default='ref_protected', choices=["not_protected", "ref_protected"]), maximum_timeout=dict(type='int', default=3600), registration_token=dict(type='str', no_log=True), project=dict(type='str'), state=dict(type='str', default="present", choices=["absent", "present"]), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_password', 'api_token'], ], required_together=[ ['api_username', 'api_password'], ], required_one_of=[ ['api_username', 'api_token'], ], required_if=[ ('state', 'present', ['registration_token']), ], supports_check_mode=True, ) state = module.params['state'] owned = module.params['owned'] runner_description = module.params['description'] runner_active = module.params['active'] tag_list = module.params['tag_list'] run_untagged = module.params['run_untagged'] runner_locked = module.params['locked'] access_level = module.params['access_level'] maximum_timeout = module.params['maximum_timeout'] registration_token = module.params['registration_token'] project = module.params['project'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_instance = gitlab_authentication(module) gitlab_project = None if project: try: gitlab_project = gitlab_instance.projects.get(project) except gitlab.exceptions.GitlabGetError as e: module.fail_json(msg='No such a project %s' % project, exception=to_native(e)) gitlab_runner = GitLabRunner(module, gitlab_instance, gitlab_project) runner_exists = gitlab_runner.exists_runner(runner_description, owned) if state == 'absent': if runner_exists: gitlab_runner.delete_runner() module.exit_json(changed=True, msg="Successfully deleted runner %s" % runner_description) else: module.exit_json(changed=False, msg="Runner deleted or does not exists") if state == 'present': if gitlab_runner.create_or_update_runner( runner_description, { "active": runner_active, "tag_list": tag_list, "run_untagged": run_untagged, "locked": runner_locked, "access_level": access_level, "maximum_timeout": maximum_timeout, "registration_token": registration_token, }): module.exit_json( changed=True, runner=gitlab_runner.runner_object._attrs, msg="Successfully created or updated the runner %s" % runner_description) else: module.exit_json(changed=False, runner=gitlab_runner.runner_object._attrs, msg="No need to update the runner %s" % runner_description)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(auth_argument_spec()) argument_spec.update( dict( group=dict(type='str'), name=dict(type='str', required=True), path=dict(type='str'), description=dict(type='str'), initialize_with_readme=dict(type='bool', default=False), default_branch=dict(type='str'), issues_enabled=dict(type='bool', default=True), merge_requests_enabled=dict(type='bool', default=True), merge_method=dict(type='str', default='merge', choices=["merge", "rebase_merge", "ff"]), wiki_enabled=dict(type='bool', default=True), snippets_enabled=dict(default=True, type='bool'), visibility=dict(type='str', default="private", choices=["internal", "private", "public"], aliases=["visibility_level"]), import_url=dict(type='str'), state=dict(type='str', default="present", choices=["absent", "present"]), lfs_enabled=dict(default=False, type='bool'), username=dict(type='str'), allow_merge_on_skipped_pipeline=dict(type='bool'), only_allow_merge_if_all_discussions_are_resolved=dict(type='bool'), only_allow_merge_if_pipeline_succeeds=dict(type='bool'), packages_enabled=dict(type='bool'), remove_source_branch_after_merge=dict(type='bool'), squash_option=dict( type='str', choices=['never', 'always', 'default_off', 'default_on']), ci_config_path=dict(type='str'), shared_runners_enabled=dict(type='bool'), avatar_path=dict(type='path'), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_username', 'api_oauth_token'], ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], ['group', 'username'], ], required_together=[ ['api_username', 'api_password'], ], required_one_of=[[ 'api_username', 'api_token', 'api_oauth_token', 'api_job_token' ]], supports_check_mode=True, ) group_identifier = module.params['group'] project_name = module.params['name'] project_path = module.params['path'] project_description = module.params['description'] initialize_with_readme = module.params['initialize_with_readme'] issues_enabled = module.params['issues_enabled'] merge_requests_enabled = module.params['merge_requests_enabled'] merge_method = module.params['merge_method'] wiki_enabled = module.params['wiki_enabled'] snippets_enabled = module.params['snippets_enabled'] visibility = module.params['visibility'] import_url = module.params['import_url'] state = module.params['state'] lfs_enabled = module.params['lfs_enabled'] username = module.params['username'] allow_merge_on_skipped_pipeline = module.params[ 'allow_merge_on_skipped_pipeline'] only_allow_merge_if_all_discussions_are_resolved = module.params[ 'only_allow_merge_if_all_discussions_are_resolved'] only_allow_merge_if_pipeline_succeeds = module.params[ 'only_allow_merge_if_pipeline_succeeds'] packages_enabled = module.params['packages_enabled'] remove_source_branch_after_merge = module.params[ 'remove_source_branch_after_merge'] squash_option = module.params['squash_option'] ci_config_path = module.params['ci_config_path'] shared_runners_enabled = module.params['shared_runners_enabled'] avatar_path = module.params['avatar_path'] default_branch = module.params['default_branch'] if default_branch and not initialize_with_readme: module.fail_json( msg= "Param default_branch need param initialize_with_readme set to true" ) if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) gitlab_instance = gitlab_authentication(module) # Set project_path to project_name if it is empty. if project_path is None: project_path = project_name.replace(" ", "_") gitlab_project = GitLabProject(module, gitlab_instance) namespace = None namespace_id = None if group_identifier: group = find_group(gitlab_instance, group_identifier) if group is None: module.fail_json( msg="Failed to create project: group %s doesn't exists" % group_identifier) namespace_id = group.id else: if username: namespace = gitlab_instance.namespaces.list(search=username)[0] else: namespace = gitlab_instance.namespaces.list( search=gitlab_instance.user.username)[0] namespace_id = namespace.id if not namespace_id: module.fail_json( msg= "Failed to find the namespace or group ID which is required to look up the namespace" ) try: namespace = gitlab_instance.namespaces.get(namespace_id) except gitlab.exceptions.GitlabGetError as e: module.fail_json( msg="Failed to find the namespace for the given user: %s" % to_native(e)) if not namespace: module.fail_json(msg="Failed to find the namespace for the project") project_exists = gitlab_project.exists_project(namespace, project_path) if state == 'absent': if project_exists: gitlab_project.delete_project() module.exit_json(changed=True, msg="Successfully deleted project %s" % project_name) module.exit_json(changed=False, msg="Project deleted or does not exists") if state == 'present': if gitlab_project.create_or_update_project( project_name, namespace, { "path": project_path, "description": project_description, "initialize_with_readme": initialize_with_readme, "default_branch": default_branch, "issues_enabled": issues_enabled, "merge_requests_enabled": merge_requests_enabled, "merge_method": merge_method, "wiki_enabled": wiki_enabled, "snippets_enabled": snippets_enabled, "visibility": visibility, "import_url": import_url, "lfs_enabled": lfs_enabled, "allow_merge_on_skipped_pipeline": allow_merge_on_skipped_pipeline, "only_allow_merge_if_all_discussions_are_resolved": only_allow_merge_if_all_discussions_are_resolved, "only_allow_merge_if_pipeline_succeeds": only_allow_merge_if_pipeline_succeeds, "packages_enabled": packages_enabled, "remove_source_branch_after_merge": remove_source_branch_after_merge, "squash_option": squash_option, "ci_config_path": ci_config_path, "shared_runners_enabled": shared_runners_enabled, "avatar_path": avatar_path, }): module.exit_json( changed=True, msg="Successfully created or updated the project %s" % project_name, project=gitlab_project.project_object._attrs) module.exit_json(changed=False, msg="No need to update the project %s" % project_name, project=gitlab_project.project_object._attrs)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(auth_argument_spec()) argument_spec.update( group=dict(type='str', required=True), purge=dict(type='bool', required=False, default=False), vars=dict(type='dict', required=False, default=dict(), no_log=True), variables=dict(type='list', elements='dict', required=False, default=list(), options=dict( name=dict(type='str', required=True), value=dict(type='str', no_log=True), masked=dict(type='bool', default=False), protected=dict(type='bool', default=False), environment_scope=dict(type='str', default='*'), variable_type=dict(type='str', default='env_var', choices=["env_var", "file"]) )), state=dict(type='str', default="present", choices=["absent", "present"]), ) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_username', 'api_token'], ['api_username', 'api_oauth_token'], ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], ['vars', 'variables'], ], required_together=[ ['api_username', 'api_password'], ], required_one_of=[ ['api_username', 'api_token', 'api_oauth_token', 'api_job_token'] ], supports_check_mode=True ) if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) purge = module.params['purge'] var_list = module.params['vars'] state = module.params['state'] if var_list: variables = vars_to_variables(var_list, module) else: variables = module.params['variables'] if state == 'present': if any(x['value'] is None for x in variables): module.fail_json(msg='value parameter is required in state present') gitlab_instance = gitlab_authentication(module) this_gitlab = GitlabGroupVariables(module=module, gitlab_instance=gitlab_instance) changed, raw_return_value, before, after = native_python_main(this_gitlab, purge, variables, state, module) # postprocessing for item in after: item.pop('group_id') item['name'] = item.pop('key') for item in before: item.pop('group_id') item['name'] = item.pop('key') untouched_key_name = 'key' if not module.check_mode: untouched_key_name = 'name' raw_return_value['untouched'] = [x for x in before if x in after] added = [x.get('key') for x in raw_return_value['added']] updated = [x.get('key') for x in raw_return_value['updated']] removed = [x.get('key') for x in raw_return_value['removed']] untouched = [x.get(untouched_key_name) for x in raw_return_value['untouched']] return_value = dict(added=added, updated=updated, removed=removed, untouched=untouched) module.exit_json(changed=changed, group_variable=return_value)