Skip to Content
Practical Ansible 2
book

Practical Ansible 2

by Daniel Oh, James Freeman, Fabio Alessandro Locati
June 2020
Intermediate to advanced
410 pages
9h 52m
English
Packt Publishing
Content preview from Practical Ansible 2

Creating a Kubernetes namespace with Ansible

So far, we have learned how to show existing namespaces, but usually, Ansible is used in a declarative way to achieve a desired state. So, let's create a new playbook called k8s-ns.yaml with the following content:

---- hosts: localhost  tasks:    - name: Ensure the myns namespace exists      k8s:        api_version: v1        kind: Namespace        name: myns        state: present

Before running it, we can execute kubectl get ns so that we can ensure myns is not present. In my case, the output is as follows:

$ kubectl get nsNAME STATUS AGEdefault Active 69mkube-node-lease Active 69mkube-public Active 69mkube-system Active 69m

We can now run the playbook with the following command:

$ ansible-playbook k8s-ns.yaml

The output should resemble ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Ansible - Third Edition

Mastering Ansible - Third Edition

James Freeman, Jesse Keating
Security Automation with Ansible 2

Security Automation with Ansible 2

Akash Mahajan, MADHU AKULA

Publisher Resources

ISBN: 9781789807462Supplemental Content