Ansible in a Minute · Certified Collections ·
Create a Kubernetes namespace
Short answer
Use kubernetes.core to create a Kubernetes namespace with a clear name.
Request
create kubernetes namespace app-prod with kubernetes.core.k8s
Example
A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.
YAML
---
- name: Create a Kubernetes namespace
hosts: localhost
gather_facts: false
connection: local
tasks:
- name: Ensure namespace exists
kubernetes.core.k8s:
state: present
definition:
apiVersion: v1
kind: Namespace
metadata:
name: app-prod
labels:
env: prodHow it works
kubernetes.core.k8s applies a resource definition through the cluster API. state present creates the namespace when it is missing and leaves it unchanged when it already exists.
When to use it
- Provisioning an environment namespace before deploying workloads
- Keeping namespace labels in source control
Requirements
- Collection: kubernetes.core
- Technology: Kubernetes
- kubernetes.core collection installed
- kubeconfig or equivalent cluster credentials available to Ansible
Validation
- Syntax validated
- Not execution-tested
- Last verified 2026-09-09
Notes
Confirm the namespace name against cluster naming policy before you apply it in shared clusters.
Try it with BacklogZero
Ask BacklogZero to generate this automation for your environment.