ansible-collections / kubernetes.core

The collection includes a variety of Ansible content to help automate the management of applications in Kubernetes and OpenShift clusters, as well as the provisioning and maintenance of clusters themselves.
Other
212 stars 133 forks source link

kubernetes.core.helm_repository to be stateful #480

Open edgan opened 2 years ago

edgan commented 2 years ago
SUMMARY

kubernetes.core.helm_repository, or a new function, should be stateful, as in state: present. Currently it installs a repository, but can't verify if it is already installed. If it is already installed it will just throw an error saying it is already installed.

ISSUE TYPE
COMPONENT NAME

kubernetes.core.helm_repository

ADDITIONAL INFORMATION

Workaround code:

- name: "helm_repo_check_{{ nexus.helm.repo.name }}"
  changed_when: false
  shell:
    cmd: "helm repo list | grep {{ nexus.helm.repo.name }}"

- name: nexus_helm_repo
  kubernetes.core.helm_repository:
    name: "{{ nexus.helm.repo.name }}"
    repo_url: "nexus.helm.repo.url }}"
  when: helm_repo_nexus.rc == 1
`
gravesm commented 2 years ago

@edgan The module already supports this:

- hosts: localhost
  gather_facts: no
  tasks:
    - kubernetes.core.helm_repository:
        repo_url: https://charts.bitnami.com/bitnami
        name: bitnami

    - kubernetes.core.helm_repository:
        repo_url: https://charts.bitnami.com/bitnami
        name: bitnami
PLAY [localhost] ****************************************************************************************************************************

TASK [kubernetes.core.helm_repository] ******************************************************************************************************
changed: [localhost]

TASK [kubernetes.core.helm_repository] ******************************************************************************************************
ok: [localhost]

PLAY RECAP **********************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Could you provide more information about what version of the collection and what version of ansible you are using? Also, could you provide the output from running the playbook using -vvv?

em- commented 1 year ago

I got here because I got the Repository already have a repository named $foo error, but it happened to me because I already had it listed with a different URL.

I can reproduce the exact error with:

- hosts: localhost
  gather_facts: no
  tasks:
    - kubernetes.core.helm_repository:
        repo_url: https://charts.bitnami.com/bitnami
        name: bitnami

    - kubernetes.core.helm_repository:
        repo_url: https://charts.bitnami.com/bitnami/ # note the trailing "/"
        name: bitnami
$ ansible-playbook ansible-helm-repo.yaml 
TASK [kubernetes.core.helm_repository] 
changed: [localhost]

TASK [kubernetes.core.helm_repository] 
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Repository already have a repository named bitnami"}

Maybe this was issue for the original reporter as well?

Note that helm gives a different message, but still does not point out that the issue is the different URL. I know it should be obvious, but my brain sometimes goes on vacation. :)

$helm repo add bitnami https://charts.gitlab.io/
Error: repository name (bitnami) already exists, please specify a different name