Ansible Pilot

Apply Multiple Yaml Files at Once on Kubernetes K8s or OpenShift OCP - Ansible module k8s and lookup plugin fileglob

How to automate the application of multiple Kubernetes YAML definitions (Namespace, Pod, Service, etc.) using Ansible Playbook on Kubernetes K8s or OpenShift OCP cluster using module k8s & lookup plugin fileglob.

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

How to Apply Multiple Yaml Files at Once on Kubernetes K8s or OpenShift OCP 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 Apply Multiple YAML Files at Once on K8s or OCP

Let’s talk about the Ansible module k8s and the Ansible lookup plugin fileglob. 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. Plugins are a way to expand the Ansible functionality. With lookup plugins specifically, you can load variables or templates with information from external sources. The full name is ansible.builtin.fileglob, it’s part of ansible-core and is included in all Ansible installations. The purpose of the lookup plugin is to list files matching a pattern.

k8s module 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 the object namespace. The api_version parameter specifies the Kubernetes API version, the default is “v1” for version 1. The state like for other modules determines if an object should be created - present option, update - 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.

The Best Resources For Ansible

Video Course

Books

demo

How to Apply Multiple Yaml Files at Once on Kubernetes K8s or OpenShift OCP with Ansible Playbook. I’m going to combine the k8s module with the fileglob lookup plugin to be able to process multiple Yaml files.

code

---
- name: k8s demo
  hosts: localhost
  gather_facts: false
  connection: local
  tasks:
    - name: Apply K8s resources
      kubernetes.core.k8s:
        definition: "{{ lookup('template', '{{ item }}') | from_yaml }}"
      with_fileglob:
        - "./defs/mynamespace.yaml"
        - "./defs/mypod.yaml"
        - "./defs/myservice.yaml"
---
kind: Namespace
apiVersion: v1
metadata:
  name: example
  labels:
    name: example
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: example
  labels:
    app.kubernetes.io/name: proxy
spec:
  containers:
    - name: nginx
      image: nginx:1.21.6
      ports:
        - containerPort: 80
          name: http-web-svc
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: example
spec:
  selector:
    app.kubernetes.io/name: proxy
  ports:
    - name: service-port
      protocol: TCP
      port: 80
      targetPort: http-web-svc

execution

ansible-pilot $ ansible-playbook kubernetes/multiple_yaml.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 [Apply K8s resources] ************************************************************************
changed: [localhost] => (item=/Users/lberton/prj/github/ansible-pilot/kubernetes/./defs/mynamespace.yaml)
changed: [localhost] => (item=/Users/lberton/prj/github/ansible-pilot/kubernetes/./defs/mypod.yaml)
changed: [localhost] => (item=/Users/lberton/prj/github/ansible-pilot/kubernetes/./defs/myservice.yaml)
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/multiple_yaml.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 [Apply K8s resources] ************************************************************************
ok: [localhost] => (item=/Users/lberton/prj/github/ansible-pilot/kubernetes/./defs/mynamespace.yaml)
ok: [localhost] => (item=/Users/lberton/prj/github/ansible-pilot/kubernetes/./defs/mypod.yaml)
ok: [localhost] => (item=/Users/lberton/prj/github/ansible-pilot/kubernetes/./defs/myservice.yaml)
PLAY RECAP ****************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

ansible-pilot $ oc project example
error: A project named "example" does not exist on "https://api.crc.testing:6443".
Your projects are:
* default
[...]
ansible-pilot $ oc get pod --namespace=example 
No resources found in example namespace.
ansible-pilot $ oc get service --namespace=example
No resources found in example namespace.
ansible-pilot $

ansible module k8s before execution

after execution

ansible-pilot $ oc project example
Now using project "example" on server "https://api.crc.testing:6443".
ansible-pilot $ oc get pod --namespace=example
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          38s
ansible-pilot $ oc get service --namespace=example
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
nginx-service   ClusterIP   10.217.4.144   <none>        80/TCP    50s
ansible-pilot $

ansible module k8s after execution

Recap

Now you know how to Apply Multiple YAML Files at Once on Kubernetes K8s or OpenShift OCP with Ansible.

Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack 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 200+ Ansible code

Want to keep this project going? Please donate

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

Subscribe not to miss any new releases

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress