Difficulty: beginner
Estimated Time: 15 minutes

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

YouTube

Samples

Chat

Ansible Kubernetes Module

Step 1 of 3

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