Last active
July 16, 2025 12:13
-
-
Save leshikus/1a728fa85ee3150ae796345e5463e2ad to your computer and use it in GitHub Desktop.
Setup Grafana via Helm CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Setup local Kubernetes cluster with Grafana | |
hosts: localhost | |
vars: | |
arch: amd64 | |
cluster_name: kind-grafana | |
kubectl_url: "https://dl.k8s.io/release/{{ lookup('url', 'https://dl.k8s.io/release/stable.txt') }}/bin/linux/{{ arch }}/kubectl" | |
kind_url: "https://kind.sigs.k8s.io/dl/latest/kind-linux-{{ arch }}" | |
helm_script_url: "https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3" | |
do_restart: false | |
tasks: | |
- name: Install packages | |
become: true | |
package: | |
name: | |
- curl | |
- gnupg | |
- software-properties-common | |
state: present | |
- name: Download and install kubectl | |
get_url: | |
url: "{{ kubectl_url }}" | |
dest: /tmp/kubectl | |
mode: '0755' | |
register: kubectl_download | |
- name: Move kubectl to /usr/local/bin | |
become: true | |
copy: | |
src: /tmp/kubectl | |
dest: /usr/local/bin/kubectl | |
remote_src: yes | |
mode: '0755' | |
when: kubectl_download.changed | |
- name: Download and install kind | |
get_url: | |
url: "{{ kind_url }}" | |
dest: /tmp/kind | |
mode: '0755' | |
register: kind_download | |
- name: Move kind to /usr/local/bin | |
become: true | |
copy: | |
src: /tmp/kind | |
dest: /usr/local/bin/kind | |
remote_src: yes | |
mode: '0755' | |
when: kind_download.changed | |
- name: Install Helm if not present | |
shell: | | |
curl -fsSL -o get_helm.sh {{ helm_script_url }} | |
chmod u+x get_helm.sh | |
./get_helm.sh | |
args: | |
creates: /usr/local/bin/helm | |
- name: Write kind cluster config | |
copy: | |
dest: ./kind-config.yaml | |
content: | | |
kind: Cluster | |
apiVersion: kind.x-k8s.io/v1alpha4 | |
nodes: | |
- role: control-plane | |
- role: worker | |
- role: worker | |
- name: Get the cluster list | |
shell: > | |
kind get clusters | |
register: cluster_list | |
failed_when: false | |
changed_when: false | |
- set_fact: | |
cluster_exists: "{{ cluster_name in cluster_list.stdout }}" | |
- name: Delete cluster | |
shell: | | |
kind delete cluster --name {{ cluster_name }} | |
rm -f .kube/config | |
when: cluster_exists and do_restart | |
- name: Create kind cluster | |
shell: kind create cluster --name {{ cluster_name }} --config kind-config.yaml | |
args: | |
creates: ~/.kube/config | |
register: stdout | |
when: not cluster_exists or do_restart | |
- name: Wait for nodes to be ready | |
shell: kubectl get nodes | |
register: nodes_out | |
retries: 10 | |
delay: 5 | |
until: '"Ready" in nodes_out.stdout' | |
- name: Add Grafana Helm repo | |
shell: helm repo add grafana https://grafana.github.io/helm-charts | |
args: | |
creates: ~/.cache/helm/repository/grafana-index.yaml | |
- name: Update Helm repos | |
shell: helm repo update | |
- name: Save default Grafana values.yaml | |
shell: helm show values grafana/grafana > values.yaml | |
args: | |
creates: values.yaml | |
- name: Deploy Grafana | |
shell: > | |
helm upgrade --cleanup-on-fail --install grafana | |
grafana/grafana | |
--namespace monitoring | |
--create-namespace | |
--values values.yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment