June 2020
Intermediate to advanced
410 pages
9h 52m
English
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 ...