Deploy Kubernetes Resources with Ansible Playbook
By Luca Berton · Published 2024-01-01 · Category: installation
Learn how to deploy Kubernetes resources using Ansible. Follow this guide to create namespaces, pods, and services with an Ansible playbook.

How to Apply Multiple Yaml Files at Once on Kubernetes K8s or OpenShift OCP with Ansible?
I'm going to show you a live Playbook and some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.See also: Optimize Kubernetes CPU Resources with Ansible Playbooks
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.
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.
See also: Assign Memory to Kubernetes Pods with Ansible
Links
• kubernetes.core.k8s • ansible.builtin.fileglob • Kubernetes best practices: Organizing with Namespaces## Playbook
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 Playbook
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 Playbook] ***********************************************************************************
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 Playbook] ***********************************************************************************
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 $
Conclusion
Now you know how to Apply Multiple YAML Files at Once on Kubernetes K8s or OpenShift OCP with Ansible.
See also: Configure a Pod to Use a Volume for Storage on Kubernetes or OpenShift with Ansible
Related Articles
• Jinja2 templating in Ansible • Nginx deployment with Ansible • Ansible Inventory Guide • file ownership and modes via ansible.builtin.fileCategory: installation
Watch the video: Deploy Kubernetes Resources with Ansible Playbook — Video Tutorial