Create an Ansible playbook to launch the EC2 instance and configure minikube in it

Nischal Vooda
3 min readAug 27, 2022

--

Prerequisites:

  • Ansible Installed
  • Inventory Configured
  • AWS CLIv2 installed and Configured

Step-1)Configuring Dynamic inventory

  • Here I am using dynamic inventory because we have to configure minikube in ec2 instance.
  • For the dynamic inventory, download ec2.py and ec2.ini from this given URL, and paste in the inventory folder:
https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory
  • Now add this path in ansible.cfg
  • After that, you also need to copy the key.pem for the ec2 instance launch.
  • After copying your key, make it executable by the following command:
chmod 600 Key_Name.pem
  • Here you also need privilege escalation because in aws we have to configure all the configurations done by the user root only.
  • After this edit your ec2.py
  • change your python path in my case it is /usr/bin/python3.
  • Save it.
  • After this make this file executable by the following command:
chmod +x ec2.py

Step-2)Launching ec2 instance

- hosts: localhost
tasks:
- name: Launching OS
ec2:
key_name: key
instance_type: t2.medium
image: ami-04b1ddd35fd71475a
wait: yes
count: 1
instance_tags:
Name: k8s_minikube
vpc_subnet_id: subnet-c08fce8c
assign_public_ip: yes
region: ap-south-1
state: present
group_id: sg-011aaeeafb6ee8fff
  • Here u need to add some info for launching aws ec2 instance like key_name, ami_id,vpc name, etc.

Step-3)Configuring minikube in ec2 instance

- hosts: tag_Name_k8s_minikube
tasks:
- name: conf repo for kubectl
yum_repository:
name: Kubernetes
description: Kubectl
baseurl: https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
gpgcheck: yes
gpgkey: https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
repo_gpgcheck: yes
  • The first step is to setup the repo.
- name: Installing kubectl
yum:
name: kubectl
state: present- name: Installing Docker
yum:
name: docker
state: present- name: Installing conntrack
yum:
name: conntrack
state: present
  • Here I am installing kubectl(we have already setup a repo), we need docker because minikube needs docker, conntrack because if u don’t install it’ll not start minikube.
- name: Downloading minikube
get_url:
url: https://storage.googleapis.com/minikube/releases/latest/minikube-latest.x86_64.rpm
dest: /root/minikube-latest.x86_64.rpm- name: Installing minkube
yum:
name: /root/minikube-latest.x86_64.rpm
state: present- name: Starting Minikube driver using none
command: minkube start --driver=none
  • The final setup is to installing minikube I use get_url to download minikube, and after yum for installing, at last, minikube start — — driver=none (driver=none because we are going to install minikube on the top of os without using any driver like(Virtualbox, etc).

Output

ec2 instance launched

minikube installed

--

--

No responses yet