In contemporary scenarios, it is increasingly prevalent to have the kubectl and Helm Charts clients installed on the control or remote machine. This ensures the seamless establishment of connections with Kubernetes (K8s) and facilitates the deployment of Helm Charts without encountering any issues. In the following discussion, we will delve deeper into the process of installing kubectl and Helm within a Kubernetes environment, emphasizing the creation of ansible roles tailored specifically for managing kubectl and Helm Charts.

We will adhere to the template provided below for the installation of kubectl and Helm.

We organize the folder structure for kubectl roles as depicted below, and the structure for Helm follows the same arrangement.

roles/setup/kubectl/defaults/main.yaml

---
tmp_directory: "{{ lookup('env', 'TMPDIR') | default('/tmp',true) }}"

default:
version: "1.19.8"
os: "linux" # use "darwin" for MacOS X, "windows" for Windows
arch: "amd64" # other possible values: "386","arm64","arm","ppc64le","s390x"
bin_directory: "~/bin"

roles/setup/kubectl/tasks/main.yaml


---
- name: register temporary directory

- name: check kubectl

- name: Download kubectl binary

- name: Unarchive kubernetes-client

- name: create bin directory

- name: Copy kubectl binary to destination directory

- name: Test kubectl installation

lets deepdive into one by one task and fullfill it accordingly

name: register temporary directory



- name: register temporary directory
tempfile:
state: directory
register: tmp_directory
tags:
- molecule-idempotence-notest

name: check kubectl and register the result with a variable kubectl_stat_result

 - name: check kubectl
stat:
path: "{{ kubectl.bin_directory | default(default.bin_directory) }}/kubectl"
register: kubectl_stat_result
tags:
- kubectl

name: Download kubectl binary

  - name: Download kubectl binary
get_url:
url: "https://dl.k8s.io/v{{ kubectl.version | default(default.version) }}/kubernetes-client-{{ kubectl.os | default(default.os) }}-{{ kubectl.arch | default(default.arch)}}.tar.gz"
dest: "{{ tmp_directory.path }}"
when: not kubectl_stat_result.stat.exists
tags:
- kubectl

name: create bin directory

 - name: create bin directory
file:
path: "{{ kubectl.bin_directory | default(default.bin_directory) }}"
state: directory
when: not kubectl_stat_result.stat.exists
tags:
- kubectl

name: Copy kubectl binary to destination directory

 - name: Copy kubectl binary to destination directory
copy:
src: "{{ tmp_directory.path }}/kubernetes/client/bin/{{ bin_item }}"
dest: "{{ kubectl.bin_directory | default(default.bin_directory) }}/{{ bin_item }}"
mode: 0755
remote_src: yes
when: not kubectl_stat_result.stat.exists
with_items:
- kubectl
loop_control:
loop_var: bin_item
tags:
- kubectl

name: Test kubectl installation

  - name: Test kubectl installation
command: "kubectl version --client"
changed_when: false
tags:
- molecule-idempotence-notest

Define the below environment-setup.yaml to invoke the Custom Roles into this playbook

---

- hosts: ansible_provisioners
gather_facts: yes
no_log: "{{ no_ansible_log | default(false) }}"
tasks:


- include_role:
name: setup/kubectl
vars:
kubectl:
os: "{{ install_os }}"
arch: "{{ install_arch }}"
bin_directory: "{{ bin_install_dir }}"


- include_role:
name: setup/helm
vars:
helm:
os: "{{ install_os }}"
arch: "{{ install_arch }}"
bin_directory: "{{ bin_install_dir }}"



vars: #These variables can be overriden from the command line
privilege_escalate: false #Default to NOT escalate to root privledges
install_os: "linux" #Default to linux OS
install_arch: "amd64" #Default to amd64 architecture
bin_install_dir: "~/bin" #Default to ~/bin install directory for binaries

Define the Site.yaml and invoke the ‘environment-setup.yaml’ playbook inside the site.yaml


- hosts: all
gather_facts: true
no_log: "{{ no_ansible_log | default(false) }}"
- import_playbook: environment-setup.yaml

Result

Command to execute —

ansible-playbook /home/azureuser/site.yaml

Leave a Reply