Automating Helm using Ansible
You can automate your Kubernetes infrastructure using Ansible. All Kubernetes modules are now located in the Kubernetes Collection called kubernetes.core. This Collection also contains modules to automate Helm and its related functionalities.
Another Approach
- name: Install a Helm Chart
hosts: all
become: yes
tasks:
- name: Add Helm repository
command: helm repo add bitnami https://charts.bitnami.com/bitnami
register: add_repo_result
- name: Update Helm repositories
command: helm repo update
register: update_repo_result
- name: Install NGINX Helm Chart
command: helm install nginx-server bitnami/nginx -n default
register: helm_install_result
- debug:
msg: "Helm repository add result: {{ add_repo_result.stdout }}"
- debug:
msg: "Helm repo update result: {{ update_repo_result.stdout }}"
- debug:
msg: "Helm chart install result: {{ helm_install_result.stdout }}"
Scenario 1 - Adding new Helm Repository
add a Helm Repository using helm_repository module:
---
- hosts: <hostname/group>
vars:
helm_chart_url: "https://charts.bitnami.com/bitnami"
tasks:
- name: <Add helm repo>
kubernetes.core.helm_repository:
name: bitnami
repo_url: "{{ helm_chart_url }}"
Scenario 2 - Installing a Helm Chart
Now, we have the Helm repository configured. Let us now install nginx charts from the Bitnami repository.
---
- hosts: <hostname/group>
tasks:
- name: <Install Nginx Chart>
kubernetes.core.helm:
name: nginx-server
namespace: testing
chart_ref: bitnami/nginx
Scenario 3 - Gather information about Helm Chart installed
Gathering information about the Helm Chart is also easy using the helm_info module.
---
- hosts: <hostname/group>
tasks:
- name: <Gather information about nginx-server>
kubernetes.core.helm_info:
name: nginx-server
release_namespace: testing
Scenario 4 - Install Helm Plugin
Helm allows you to enhance its functionality by providing pluggable architecture. That means users can write plugins to enhance Helm functionality. There is a large number of Helm plugins available. Users can install those plugins depending on their use case and requirements.
Let us now try to install the Helm plugin called helm env. This helm plugin allows users to view the environment variables available to a helm plugin.
---
- hosts: <hostname/group>
tasks:
- name: <Install Helm Plugin>
kubernetes.core.helm_plugin:
plugin_path: https://github.com/adamreese/helm-env
state: present
release_namespace: <testing>
Scenario 5 - Gather information about Helm plugins
Users can gather information about installed Helm plugins from the given Kubernetes cluster.
---
- hosts: <hostname/group>
tasks:
- name: Gather Helm plugin info
kubernetes.core.helm_plugin_info:
release_namespace: testing
register: r
- name: Print plugin version
debug:
msg: "{{ ( r.plugin_list | selectattr('name', 'equalto', plugin_name) | list )[0].version }}"
vars:
plugin_name: "env"