Setting Up a Fully Functional Ansible Environment in a Minikube Pod

This guide will show you how to set up a fully functional Ansible pod with a virtual environment inside a minikube environment and this will help you execute the regular ansible like commands than going via the awk.

Why Use a Virtual Environment?

  • Execute the regular ansible like commands
  • Direct pip install ansible fails in the system Python
  • A Python virtual environment (venv) isolates Ansible and its dependencies

Create a yaml file for the ee-shell-full for Ansible pod

awk@ansible:~/playbooks$ cat ee-shell-full.yaml
apiVersion: v1
kind: Pod
metadata:
  name: ee-shell-full
  namespace: default
spec:
  containers:
    - name: ee-shell-full
      image: ubuntu:24.04
      command: ["/bin/bash", "-c", "sleep infinity"]
      securityContext:
        runAsUser: 0        # Run as root
        runAsGroup: 0
      tty: true
      stdin: true

Start the pod

kubectl apply -f ee-shell-full.yaml

log in with:

kubectl exec -it ee-shell-full -- bash

You are root inside this pod, which allows installing packages and managing SSH keys.

Install Required Packages

Inside the pod:

apt update
apt install -y python3 python3-pip python3-venv iputils-ping openssh-client curl vim
  • python3 / python3-pip: required for Ansible
  • python3-venv: create isolated Python environment
  • iputils-ping / curl: network testing
  • openssh-client: for SSH to remote hosts

Create and Activate a Python Virtual Environment

python3 -m venv /root/ansible-venv
source /root/ansible-venv/bin/activate
  • Prompt changes to:
(ansible-venv) root@ee-shell-full:/#

Install Ansible inside the venv:

pip install --upgrade pip
pip install ansible

Check Ansible version

ansible --version
(ansible-venv) root@ee-shell-full:/# ansible --version
ansible [core 2.20.2]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /root/ansible-venv/lib/python3.12/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /root/ansible-venv/bin/ansible
  python version = 3.12.3 (main, Jan  8 2026, 11:30:50) [GCC 13.3.0] (/root/ansible-venv/bin/python3)
  jinja version = 3.1.6
  pyyaml version = 6.0.3 (with libyaml v0.2.5)

Ansible is now fully functional inside the venv

awk@ansible:~$ kubectl describe pod ee-shell
Name:             ee-shell-full
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Sat, 31 Jan 2026 11:05:30 +0000
Labels:           <none>
Annotations:      <none>
Status:           Running
IP:               10.244.0.65
IPs:
  IP:  10.244.0.65
Containers:
  ee-shell-full:
    Container ID:  docker://8881a93b237e33bf50318f386ec8cfe9a1565445b4e392cfb2d3cbbeeed36249
    Image:         ubuntu:24.04
    Image ID:      docker-pullable://ubuntu@sha256:cd1dba651b3080c3686ecf4e3c4220f026b521fb76978881737d24f200828b2b
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/bash
      -c
      sleep infinity
    State:          Running
      Started:      Sat, 31 Jan 2026 08:33:06 +0000
    Last State:     Terminated
      Reason:       Error
      Exit Code:    137
      Started:      Sat, 31 Jan 2026 11:05:31 +0000
      Finished:     Sat, 31 Jan 2026 15:31:24 +0000
    Ready:          True
    Restart Count:  1
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-slvqn (ro)
Conditions:
  Type                        Status
  PodReadyToStartContainers   True
  Initialized                 True
  Ready                       True
  ContainersReady             True
  PodScheduled                True
Volumes:
  kube-api-access-slvqn:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason          Age    From     Message
  ----    ------          ----   ----     -------
  Normal  SandboxChanged  7m12s  kubelet  Pod sandbox changed, it will be killed and re-created.
  Normal  Pulled          7m10s  kubelet  Container image "ubuntu:24.04" already present on machine and can be accessed by the pod
  Normal  Created         7m10s  kubelet  Container created
  Normal  Started         7m8s   kubelet  Container started

Setup Passwordless SSH

Copy SSH keys from host to pod and Fix permissions inside the pod:

kubectl cp ~/.ssh default/ee-shell-full:/root/.ssh
chmod 700 /root/.ssh
chmod 600 /root/.ssh/id_rsa
chmod 644 /root/.ssh/id_rsa.pub

Set host files



(ansible-venv) root@ee-shell-full:~# cat hosts
[test]
172.16.11.122

Run Ansible on a Remote Host

ansible-venv) root@ee-shell-full:~# ansible -i hosts test -m command -a "uptime" -u awk
[WARNING]: Host '172.16.11.122' is using the discovered Python interpreter at '/usr/bin/python3.12', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/2.20/reference_appendices/interpreter_discovery.html for more information.
172.16.11.122 | CHANGED | rc=0 >>
 14:32:30 up 20:48,  3 users,  load average: 0.00, 0.00, 0.00

Add this line to root’s .bashrc for Auto-Activate venv

echo "source /root/ansible-venv/bin/activate" >> ~/.bashrc

Next time you login, the venv is active automatically

Logging out of the pod shell

Inside the pod, simply rune exit to return you to your host VM shell

exit

Shutting down / deleting the pod

If you want to remove the pod completely:

kubectl delete pod ee-shell-full
(Visited 4 times, 1 visits today)

By Ash Thomas

Ash Thomas is a seasoned IT professional with extensive experience as a technical expert, complemented by a keen interest in blockchain technology.