BacklogZero®

Ansible in a Minute · Certified Collections ·

Create a Kubernetes namespace

Short answer

Use kubernetes.core to create a Kubernetes namespace with a clear name.

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

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: prod

How 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.

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today →

Related guides

← Back to Ansible in a Minute

Create a Kubernetes namespace · Level Up Labs