Since you are interested in using Ansible for lifecycle management of applications on Kubernetes, it will be beneficial to learn how to use the Ansible k8s (Kubernetes) module.
The k8s module allows you to:
- Leverage your existing Kubernetes resource files (written in YAML)
- Express Kubernetes lifecycle management actions in native Ansible.
One of the biggest benefits of using Ansible in conjunction with existing Kubernetes resource files is the ability to use Ansible's built-in Jinja templating engine to customize deployments by simply setting Ansible variables.
By the end of this scenario, you'll be able to use the Ansible k8s module to:
- Create and remove Kubernetes resources
- Reuse existing Kubernetes manifest files with Ansible
Thank you for taking a closer look at the Ansible Operator. For more Information, check out the links below:
K8s Ansible Module
Git Hub
- Operator-Framework: https://github.com/operator-framework
- Operator-SDK: https://github.com/operator-framework/operator-sdk/
YouTube
- Memcached Ansible Operator Demo: https://www.youtube.com/watch?v=Smk9oQp7YMY
- OpenShift Commons Briefing Ansible Operator - Operator Framework SIG: https://www.youtube.com/watch?v=YFTQJI6foGc
Samples
- Sample Operators using Ansible: https://github.com/operator-framework/operator-sdk-samples/tree/master/ansible
Chat
- Kubernetes Slack Chat (upstream): #kubernetes-operators at https://kubernetes.slack.com/
- Operator-Framework on Google Groups: https://groups.google.com/forum/#!forum/operator-framework
- OpenShift Operators Special Interest Group (SIG): https://commons.openshift.org/sig/OpenshiftOperators.html

Steps
Ansible Kubernetes Module
Running the k8s Ansible modules locally
For this example we will create and delete a namespace with the switch of an Ansible variable.
a. Modify tasks file example-role/tasks/main.yml
to contain the Ansible shown below.
--- - name: set test namespace to {{ state }} k8s: api_version: v1 kind: Namespace name: test state: "{{ state }}" ignore_errors: true
You can easily update this file by running the following command:
wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/tasksmain1.yml -O /root/tutorial/example-role/tasks/main.yml
b. Modify vars file example-role/defaults/main.yml
, setting state: present
by default.
--- state: present
You can easily update this file by running the following command:
wget -q https://raw.githubusercontent.com/openshift-labs/learn-katacoda/master/ansibleop/ansible-k8s-modules/assets/defaultsmain1.yml -O /root/tutorial/example-role/defaults/main.yml
c. Run playbook.yml, which will execute 'example-role'.
ansible-playbook -i myhosts playbook.yml
d. Check that the namespace test
was created.
$ oc get projects | grep test
NAME DISPLAY NAME STATUS
test Active