diff --git a/changelogs/fragments/509-helm-repo-add-force_update-argument.yaml b/changelogs/fragments/509-helm-repo-add-force_update-argument.yaml new file mode 100644 index 0000000000..ba7588210f --- /dev/null +++ b/changelogs/fragments/509-helm-repo-add-force_update-argument.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - helm_repository - Ability to replace (overwrite) the repo if it already exists by forcing (https://github.com/ansible-collections/kubernetes.core/issues/491). diff --git a/plugins/modules/helm_repository.py b/plugins/modules/helm_repository.py index 03a0e21dee..cb232a5f69 100644 --- a/plugins/modules/helm_repository.py +++ b/plugins/modules/helm_repository.py @@ -112,6 +112,13 @@ type: raw aliases: [ kubeconfig_path ] version_added: "2.4.0" + force_update: + description: + - Whether or not to replace (overwrite) the repo if it already exists. + type: bool + aliases: [ force ] + default: False + version_added: "2.4.0" """ EXAMPLES = r""" @@ -218,6 +225,7 @@ def install_repository( repository_username, repository_password, pass_credentials, + force_update, ): install_command = command + " repo add " + repository_name + " " + repository_url @@ -228,6 +236,9 @@ def install_repository( if pass_credentials: install_command += " --pass-credentials" + if force_update: + install_command += " --force-update" + return install_command @@ -250,6 +261,7 @@ def argument_spec(): default="present", choices=["present", "absent"], aliases=["state"] ), pass_credentials=dict(type="bool", default=False, no_log=True), + force_update=dict(type="bool", default=False, aliases=["force"]), ) ) return arg_spec @@ -277,6 +289,7 @@ def main(): repo_password = module.params.get("repo_password") repo_state = module.params.get("repo_state") pass_credentials = module.params.get("pass_credentials") + force_update = module.params.get("force_update") helm_cmd = get_helm_binary(module) @@ -286,7 +299,7 @@ def main(): helm_cmd = delete_repository(helm_cmd, repo_name) changed = True elif repo_state == "present": - if repository_status is None: + if repository_status is None or force_update: helm_cmd = install_repository( helm_cmd, repo_name, @@ -294,6 +307,7 @@ def main(): repo_username, repo_password, pass_credentials, + force_update, ) changed = True elif repository_status["url"] != repo_url: diff --git a/tests/integration/targets/helm/tasks/main.yml b/tests/integration/targets/helm/tasks/main.yml index e86d33df22..9d8dda3e7b 100644 --- a/tests/integration/targets/helm/tasks/main.yml +++ b/tests/integration/targets/helm/tasks/main.yml @@ -4,4 +4,4 @@ loop_control: loop_var: helm_version with_items: - - "v3.2.4" + - "v3.7.0" diff --git a/tests/integration/targets/helm/tasks/tests_repository.yml b/tests/integration/targets/helm/tasks/tests_repository.yml index eedea01a73..dfd649fe84 100644 --- a/tests/integration/targets/helm/tasks/tests_repository.yml +++ b/tests/integration/targets/helm/tasks/tests_repository.yml @@ -42,6 +42,19 @@ that: - repository_errors is failed +- name: Succesfully add repository with the same name when forcing + helm_repository: + binary_path: "{{ helm_binary }}" + name: test_helm_repo + repo_url: "{{ chart_test_repo }}" + force: true + register: repository + +- name: Assert that test_helm_repo repository is changed + assert: + that: + - repository is changed + - name: Remove test_helm_repo chart repository helm_repository: binary_path: "{{ helm_binary }}"