diff --git a/changelogs/fragments/4150-gitlab-project-variable-absent-fix.yml b/changelogs/fragments/4150-gitlab-project-variable-absent-fix.yml new file mode 100644 index 0000000000..778ee40ce1 --- /dev/null +++ b/changelogs/fragments/4150-gitlab-project-variable-absent-fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - gitlab_project_variable - ``value`` is not necessary when deleting variables (https://github.com/ansible-collections/community.general/pull/4150). diff --git a/plugins/modules/source_control/gitlab/gitlab_project_variable.py b/plugins/modules/source_control/gitlab/gitlab_project_variable.py index 9bab6f169c..f9b8d7b6e1 100644 --- a/plugins/modules/source_control/gitlab/gitlab_project_variable.py +++ b/plugins/modules/source_control/gitlab/gitlab_project_variable.py @@ -74,8 +74,8 @@ options: value: description: - The variable value. + - Required when I(state=present). type: str - required: true masked: description: - Wether variable value is masked or not. @@ -403,7 +403,7 @@ def main(): 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', required=True, no_log=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='*'), @@ -431,18 +431,21 @@ def main(): 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'] - state = module.params['state'] - - if not HAS_GITLAB_PACKAGE: - module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) + 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) diff --git a/tests/integration/targets/gitlab_project_variable/tasks/main.yml b/tests/integration/targets/gitlab_project_variable/tasks/main.yml index 7b745526a2..18dbf7eccd 100644 --- a/tests/integration/targets/gitlab_project_variable/tasks/main.yml +++ b/tests/integration/targets/gitlab_project_variable/tasks/main.yml @@ -227,6 +227,7 @@ vars: ACCESS_KEY_ID: environment_scope: testing + value: checkmode register: gitlab_project_variable_state - name: state must be changed @@ -242,6 +243,7 @@ vars: ACCESS_KEY_ID: environment_scope: testing + value: checkmode register: gitlab_project_variable_state - name: state must not be changed @@ -644,3 +646,51 @@ - gitlab_project_variable_state.project_variable.untouched|length == 0 - gitlab_project_variable_state.project_variable.removed|length == 0 - gitlab_project_variable_state.project_variable.updated|length == 0 + +- name: throw error when state is present but no value is given + gitlab_project_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + project: "{{ gitlab_project_name }}" + variables: + - name: delete_me + register: gitlab_project_variable_state + ignore_errors: yes + +- name: verify fail + assert: + that: + - gitlab_project_variable_state.failed + - gitlab_project_variable_state is not changed + +- name: set a new variable to delete it later + gitlab_project_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + project: "{{ gitlab_project_name }}" + purge: True + variables: + - name: delete_me + value: ansible + register: gitlab_project_variable_state + +- name: verify the change + assert: + that: + - gitlab_project_variable_state.changed + +- name: delete variable without referencing its value + gitlab_project_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + project: "{{ gitlab_project_name }}" + state: absent + variables: + - name: delete_me + register: gitlab_project_variable_state + +- name: verify deletion + assert: + that: + - gitlab_project_variable_state.changed + - gitlab_project_variable_state.project_variable.removed|length == 1