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.


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
kubernetes.core.k8s
- Manage Kubernetes (K8s) objects
ansible.builtin.fileglob
- list files matching a pattern
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.
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
k8s module parameters
- name string - object name
- namespace string - namespace
- state string - present/absent/patched
- definition string - YAML definition
- src path - path for YAML definition
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.
Links
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
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
- multiple_yaml.yml
---
- 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"
- defs/mynamespace.yaml
---
kind: Namespace
apiVersion: v1
metadata:
name: example
labels:
name: example
- defs/mypod.yaml
---
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
- defs/myservice.yaml
---
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 $
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 $
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
Donate
Want to keep this project going? Please donate