Ansible Pilot

Create Kubernetes K8s or OpenShift OCP Pod - nginx - Ansible module k8s

How to automate the creation of "nginx" Pod in namespace "example" of Kubernetes K8s or OpenShift OCP with Ansible module k8s.

April 12, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to create Kubernetes K8s or OpenShift OCP Pod with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible create Kubernetes or OpenShift Pod

Let’s talk about the Ansible module k8s. The full name is kubernetes.core.k8s, which means that is part of the collection of modules of Ansible to interact with Kubernetes and Red Hat OpenShift clusters. It manages Kubernetes (K8s) objects.

Parameters

There is a long list of parameters of the k8s module. Let me summarize the most used. Most of the parameters are very generic and allow you to combine them for many use-cases. The name and namespace specify object name and/or the object namespace. They are useful to create, delete, or discover an object without providing a full resource definition. The api_version parameter specifies the Kubernetes API version, the default is “v1” for version 1. The kind parameter specifies an object model. The state like for other modules determines if an object should be created - present option, patched - patched option, or deleted - absent option. The definition parameter allows you to provide a valid YAML definition (string, list, or dict) for an object when creating or updating. If you prefer to specify a file for the YAML definition, the src parameter provides a path to a file containing a valid YAML definition of an object or objects to be created or updated. You could also specify a YAML definition template with the template parameter. You might find useful also the validate parameter in order to define how to validate the resource definition against the Kubernetes schema. Please note that requires the kubernetes-validate python module.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

demo

How to create Kubernetes Pod with Ansible Playbook using the module k8s . Specifically, the following example is going to create the “nginx” Pod in namespace “example” of Kubernetes K8s or OpenShift OCP with Ansible.

code

---
- name: k8s demo
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    myproject: "example"
  tasks:
    - name: create k8s pod
      kubernetes.core.k8s:
        src: mypod.yaml
        namespace: "{{ myproject }}"
        state: present
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.21.6
    ports:
    - containerPort: 80

execution

ansible-pilot $ ansible-playbook kubernetes/pod.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [k8s demo] ***********************************************************************************
TASK [create k8s pod] *****************************************************************************
changed: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

idempotency

ansible-pilot $ ansible-playbook kubernetes/pod.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [k8s demo] ***********************************************************************************
TASK [create k8s pod] *****************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

ansible-pilot $ kubectl project example
Already on project "example" on server "https://api.crc.testing:6443".
ansible-pilot $ kubectl get pods
No resources found in example namespace.
ansible-pilot $
ansible-pilot $ oc project example
Already on project "example" on server "https://api.crc.testing:6443".
ansible-pilot $ oc get pods
No resources found in example namespace.
ansible-pilot $

ansible module k8s before execution

after execution

ansible-pilot $ kubectl project example
Already on project "example" on server "https://api.crc.testing:6443".
ansible-pilot $ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          5m31s
ansible-pilot $
ansible-pilot $ oc project example
Already on project "example" on server "https://api.crc.testing:6443".
ansible-pilot $ oc get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          5m31s
ansible-pilot $
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/04/12 10:59:16 [notice] 1#1: using the "epoll" event method
2022/04/12 10:59:16 [notice] 1#1: nginx/1.21.6
2022/04/12 10:59:16 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/04/12 10:59:16 [notice] 1#1: OS: Linux 4.18.0-305.30.1.el8_4.x86_64
2022/04/12 10:59:16 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/04/12 10:59:16 [notice] 1#1: start worker processes
2022/04/12 10:59:16 [notice] 1#1: start worker process 33
2022/04/12 10:59:16 [notice] 1#1: start worker process 34
2022/04/12 10:59:16 [notice] 1#1: start worker process 35
2022/04/12 10:59:16 [notice] 1#1: start worker process 36

ansible module k8s after execution

Recap

Now you know how to create nginx Kubernetes or OpenShift Pod with Ansible.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases