Helm Chart for Ansible

Akurathi Sri Krishna Sagar
8 min readAug 2, 2021

--

Quick Guide

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.

Kubernetes is an open-source container-orchestration system for automating computer application deployment, scaling, and management.

Helm is the package manager for Kubernetes. Helm helps you manage Kubernetes applications. Helm Charts help you define, install, and upgrade even the most complex Kubernetes application. Charts are easy to create, version, share, and publish.

Helm is a 3rd party tool and not available directly in Kubernetes.

In this article, we are going to launch an Ansible Pod with deployment using the Helm Chart.

Creating Docker image configured with Ansible

We need a docker image installed and configured with Ansible. For this, I’ve created a Dockerfile, which uses “centos” as the base image :

FROM    centos:latest
RUN yum install httpd -y
CMD /usr/sbin/httpd -DFOREGROUND
EXPOSE 80
RUN yum install python3 -y
RUN pip3 install setuptools_rust
RUN pip3 install --upgrade pip
RUN pip3 install ansible
RUN mkdir /etc/ansible/
RUN touch /inventory.txt
RUN yum install net-tools -y
RUN yum install vim -y
RUN dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
RUN yum install sshpass -y
RUN yum install openssh-clients -y
COPY ./ansible.cfg /etc/ansible/ansible.cfg

The following is the ansible.cfg (Configuration file for Ansible) file, which we are going to copy into the docker image :

[defaults]
inventory = /inventory.txt
host_key_checking = false
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

Now, go to the directory containing the above files and build the docker image with the command :

docker build -t asks1012/ansible-centos:latest .
#Change "asks1012" to your docker hub username if you want to upload it to hub.docker.com
#ansible-centos will be the name of resulting docker image.
#latest is the version name.
#"." indicates that the Dockerfile is in current directory.

After building the image, we get our new docker image. Check the image with the command :

docker image ls

If you want to push the image to docker hub, login to the docker hub account from CLI with the command :

docker login

Provide the username and password. After that, run :

docker push IMAGE_NAME

After the push is complete, you can view your docker image in the docker hub.

I’ve already pushed my image to the docker hub :

asks1012/ansible-centos — Docker Image | Docker Hub

You can pull my image with the command :

docker pull asks1012/ansible-centos

Creating Helm Chart

we move on to Helm. Helm needs a K8s cluster. For this practical, I’m using a local Minikube Cluster, running inside Virtual Box in Windows.

You can install helm in windows by downloading it from https://get.helm.sh/helm-v3.6.3-windows-amd64.zip , extract it and add it’s location to PATH.

Run the following command to check the installation :

helm version

Helm Chart contains the following :

  • Chart.yaml — For storing versions or metadata.
  • values.yaml — For storing variables and their values.
  • templates/ — Folder for storing the K8s yaml files.

I’m creating a new directory with name “ansible” for storing the above files, which will be the package name for helm.

Create a file with name “Chart.yaml” and write the following :

apiVersion: v1
name: ansible
version: 0.1
appVersion: 1.1
description: Helm Chart for Ansible.

In the above file, “version” is any version number, which you have to change each time you update the templates. You can write any “description”.

Now, create a directory with name “templates” under the directory “ansible”.

Run the following command inside the Kubernetes cluster to get the yaml file for deployment for launching the above created ansible docker image :

kubectl create deployment ansible --image=asks1012/ansible-centos --dry-run -o yaml > deployment.yaml

You will get the following deployment.yaml file :

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: ansible
name: ansible
spec:
replicas: 1
selector:
matchLabels:
app: ansible
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: ansible
spec:
containers:
- image: asks1012/ansible-centos
name: ansible-centos
resources: {}
status: {}

You can edit the above yaml file to match your requirements. Don’t forget to change the above yaml file to UTF-8, otherwise helm will throw errors. You can easily do it in vscode.

You can write “values.yaml” file for storing the variables if any.

Installing Helm Chart

Now go outside the “ansible” directory and run the following to launch the K8s deployment for ansible docker image through Helm Chart :

helm install ansible ansible/

I got the following output :

PS C:\Users\ASKS\Desktop> helm install ansible ansible/
NAME: ansible
LAST DEPLOYED: Sat Jul 31 17:41:19 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
PS C:\Users\ASKS\Desktop>

Output of “helm list” command :

PS C:\Users\ASKS\Desktop> helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
ansible default 1 2021-07-31 17:41:19.6995258 +0530 IST deployed ansible-0.1 1.1
PS C:\Users\ASKS\Desktop>

You can see, a new pod is launched :

PS C:\Users\ASKS\Desktop> kubectl get po
NAME READY STATUS RESTARTS AGE
ansible-68df788fb9-mr589 1/1 Running 0 9s
PS C:\Users\ASKS\Desktop>

You can go inside the above ansible pod and do configuration management on other pods!

Time for Testing

For testing, I’m launching a pod, which is configured with SSH so that Ansible pod can connect to it via SSH. I’ve created a docker image for this at :

asks1012/centos-ssh-k8s — Docker Image | Docker Hub

Run the following command to launch the pod :

PS C:\Users\ASKS\Desktop> kubectl run myssh1 --image=asks1012/centos-ssh-k8s
pod/myssh1 created
PS C:\Users\ASKS\Desktop> kubectl get po
NAME READY STATUS RESTARTS AGE
ansible-68df788fb9-mr589 1/1 Running 0 163m
myssh1 1/1 Running 0 19s
PS C:\Users\ASKS\Desktop>

Get the IP of the pod “myssh1” with the following command :

kubectl describe po myssh1

Under the description, you can see the IP of the pod (for me, 172.17.0.4) :

PS C:\Users\ASKS\Desktop> kubectl describe po myssh1
Name: myssh1
Namespace: default
Priority: 0
Node: minikube/192.168.99.100
Start Time: Sun, 01 Aug 2021 20:39:20 +0530
Labels: run=myssh1
Annotations: <none>
Status: Running
IP: 172.17.0.4
...
...

We can also get the IP of the SSH pod by going inside it and running “ifconfig” command :

PS C:\Users\ASKS\Desktop> kubectl exec -it myssh1 -- bash
[root@myssh1 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
RX packets 100 bytes 100397 (98.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 101 bytes 12119 (11.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@myssh1 /]#

Now, go inside the ansible pod with the command :

kubectl exec -it ansible-68df788fb9-mr589 -- bash
# Replace the above name with your pod name.

Now you will be inside the ansible configured pod :

PS C:\Users\ASKS\Desktop> kubectl get po
NAME READY STATUS RESTARTS AGE
ansible-68df788fb9-mr589 1/1 Running 0 168m
myssh1 1/1 Running 0 5m4s
PS C:\Users\ASKS\Desktop> kubectl exec -it ansible-68df788fb9-mr589 -- bash
[root@ansible-68df788fb9-mr589 /]#

Add the IP, username, password of the SSH pod to the Ansible pod inventory file, which is at “/inventory.txt” :

172.17.0.4      ansible_user=root       ansible_ssh_pass=password
# Replace the above IP with the SSH configured Pod IP.

Save the inventory file and run the command : “ansible all -m ping” , which just checks the connectivity with all the IP’s in inventory file. For me, the output is :

172.17.0.4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}

Configure the Containers in K8s Cluster!

Now, I’ve installed “python38” in the SSH configured pod from the Ansible Pod :

You can check if python38 is installed or not by going inside the SSH configured Pod :

PS C:\Users\ASKS\Desktop> kubectl exec -it myssh1 -- bash
[root@myssh1 /]# rpm -q python38
python38-3.8.6-3.module_el8.4.0+665+abc3a503.x86_64
[root@myssh1 /]#

Publish your Work

If you are planning to upload to upload your helm chart to some public repository, follow the steps to upload it to artifacthub.io :

  1. Make a directory “charts”. In this directory, we will be storing our helm chart :
mkdir charts

2. Run the following command to archive the helm chart you created, as artifacthub.io accepts only archive files :

helm package ansible -d charts

You will get the following output :

PS C:\Users\ASKS\desktop> helm package ansible -d charts
Successfully packaged chart and saved it to: charts\ansible-0.1.tgz
PS C:\Users\ASKS\desktop>

3. Next, we have to create a file index.yaml, which contains the metadata of our chart. Run the following command to create index.yaml file :

helm repo index charts

You can view the index.yaml file inside charts directory :

apiVersion: v1
entries:
ansible:
- apiVersion: v1
appVersion: "1.1"
created: "2021-08-02T10:25:28.8208129+05:30"
description: Helm Chart for Ansible.
digest: 7f02792923971dbdbc0478bc3180ee7cd6ea3623bb1c5666d6a827c62ee6560d
name: ansible
urls:
- ansible-0.1.tgz
version: "0.1"
generated: "2021-08-02T10:25:28.7883963+05:30"

4. Now, go to github and create a new repository and upload the charts directory to it :

5. Now, go to the Settings of your github repository and go to the Pages Section, Select the main branch and click save :

Copy the URL which is being displayed :

The URL you copied is for the root directory. Add the charts after the copied URL which we will be using below.

5. Next, go to artifacthub , signin/signup. You can sign in with github account also.

6. After signin, click on Profile icon on top right corner and click on Control Panel :

7. Click on ADD REPOSITORY button and provide the details :

8. Click ADD button and your helm chart is published !

URL for my Helm Chart on Artifacthub :

ansible 0.1.0 · asks1012/ansible (artifacthub.io)

You can install my Helm Chart by Clicking on INSTALL Button :

Thanks for Reading 😊

--

--

No responses yet