Create Kubernetes K8s or OpenShift OCP namespace project - Ansible module k8s — Video Tutorial
How to automate the "myapp" namespace project created using the Ansible module k8s for Kubernetes K8s or OpenShift OCP.
Watch Video
Watch "Create Kubernetes K8s or OpenShift OCP namespace project - Ansible module k8s" on YouTube
What You'll Learn
- How to create Kubernetes K8s or OpenShift OCP namespace project with Ansible?
- Ansible creates Kubernetes or OpenShift namespace project
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
How to create Kubernetes K8s or OpenShift OCP namespace project 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.
Ansible creates Kubernetes or OpenShift namespace project
- `kubernetes.core.k8s`
- Manage Kubernetes (K8s) objects
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
- name _string_ /namespace _string_ - object name / namespace
- api_version _string_ - "v1"
- kind _string_ - object model
- state _string_ - present/absent/patched
- definition _string_ - YAML definition
- src _path_ - path for YAML definition
- template _raw_ - YAML template definition
- validate _dictionary_ - validate resource 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/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 dictionary) 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.
Links
- [kubernetes.core.k8s - Manage Kubernetes (K8s) objects - Ansible Documentation](https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_module.html)
## Playbook
How to create Kubernetes namespace project with Ansible Playbook.
This Playbook uses Red Hat CodeReady Containers OpenShift 4 Cluster.
See also: [Install Red Hat CodeReady Containers to run OpenShift 4 in macOS](/articles/install-red-hat-codeready-containers-to-run-openshift-4-in-macos)
code
```yaml
---
- name: k8s Playbook
hosts: localhost
gather_facts: false
connection: local
vars:
project_name: "myapp"
tasks:
- name: create {{ project_name }} namespace
kubernetes.core.k8s:
api_version: v1
kind: Namespace
name: "{{ project_name }}"
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 5 min
- Category: installation
Read the full written article: Create Kubernetes K8s or OpenShift OCP namespace project - Ansible module k8s
Topics Covered
Related Video Tutorials
- Ansible troubleshooting - Kubernetes K8s or OpenShift OCP 401 Unauthorized — Explore troubleshooting steps for Kubernetes 401 Unauthorized errors in Ansible when interacting with Kubernetes or OpenShift clusters.
- Deploy Kubernetes Resources with Ansible Playbook — Learn how to deploy Kubernetes resources using Ansible. Follow this guide to create namespaces, pods, and services with an Ansible playbook.
- Optimize Kubernetes CPU Resources with Ansible Playbooks — Learn to assign CPU resources to Kubernetes and OpenShift pods using Ansible. Streamline your container management with effective resource configuration.
- Assign Memory to Kubernetes Pods with Ansible — Learn how to assign memory resources to Kubernetes or OpenShift containers and pods using Ansible. Follow our live Playbook and simple code examples.
- Configure a Pod to Use a Volume for Storage on Kubernetes or OpenShift with Ansible — Learn how to configure a Kubernetes or OpenShift Pod to use a volume for persistent storage with Ansible. This guide includes a live Playbook example, Ansible module details, and execution steps for managing storage efficiently.
- 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.