How to Create a POD using Ansible and Push it Via Jenkins

Hi All,

Please be informed that, i am tying to create a POD and Namespace in Kubernetes via Ansible Playbook and push it from Jenkins.

I have wrote the yaml playbook and it is working fine if i run it from the Ansible server, but when i push it via jenkins getting an error.

In Jenkins i have used both Freestyle project.

---
- name: Creating a Pod
  hosts: all
  vars_prompt:
   - name: pod_name
     prompt: "Enter the POD Name to Create"
     private: no
   - name: ns_name
     prompt: "Enter the Name Space to Create"
     private: no
  tasks:
   - name: Creating a POD using Ansible
     community.kubernetes.k8s:
       api_version: v1
       definition:
        kind: Pod
        metadata:
         name: "{{ pod_name }}"
         namespace: "{{ ns_name }}" 
        spec:
         containers:
          - name: "{{ pod_name }}"
            image: nginx
   - name: check the status of POD
     command: kubectl get pods "{{ pod_name }}" -n "{{ ns_name }}"
     register: pod
   - name: print the POD status
     debug: var=pod
   - name: check the name space
     command: kubectl get ns "{{ ns_name }}"
     register: names
   - name: Print the namespace status
     debug: var=names
  environment:
   KUBECONFIG: /home/ansible/.kube/config

This is my ansible playbook. Kindly let me know how to setup the same in Jenkins.

Thanks a lot in advance.

Ansible is really good at config management at an OS level. It’s not really suitable for managing your kubernetes cluster though. CD/CI pipelines are the way to go. I would recommend Argo CD, Argo Workflows, and Argo Events. Perhaps tekton. I don’t know… I like ansible but I’m very biased myself.