Ansible Operator

Ansible K8s Modules

Ensure you are in the tutorial directory:

cd $HOME/tutorial/

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 ~/tutorial/example-role/tasks/main.yml

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 ~/tutorial/example-role/defaults/main.yml

Run playbook.yml, which will execute ‘example-role’.

ansible-playbook -i myhosts playbook.yml

Check that the namespace test was created.

oc get projects | grep test

You should see:

test  Active

Next, we’ll use the Ansible k8s module to leverage existing Kubernetes and OpenShift Resource files. Let’s take a look at an nginx deployment example.

Note: We’ve modified the resource file slightly as we will be deploying on OpenShift.

Copy the nginx deployment definition nginx-deployment.yml into example-role/templates, adding a .j2 extension

cp nginx-deployment.yml ./example-role/templates/nginx-deployment.yml.j2
cat ./example-role/templates/nginx-deployment.yml.j2

Update tasks file example-role/tasks/main.yml to create the nginx deployment using the k8s module

---
- name: set test namespace to {{ state }}
  k8s:
   api_version: v1
   kind: Namespace
   name: test
   state: "{{ state }}"

- name: set nginx deployment to {{ state }}
  k8s:
   state: "{{ state }}"
   definition: "{{ lookup('template', 'nginx-deployment.yml.j2') }}"
   namespace: test

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/tasksmain2.yml -O ~/tutorial/example-role/tasks/main.yml

Running the Playbook with the command below will read the state variable defined in example-role/defaults/main.yml

ansible-playbook -i myhosts playbook.yml

You can see the test namespace created and the nginx deployment created in the new namespace.

oc get all -n test

Next, let’s make it possible to customize the replica count for our nginx deployment by adding an nginx_replicas variable to the deployment template and filling the variable value dynamically with Ansible.

Modify vars file example-role/defaults/main.yml, setting nginx_replicas: 2

---
state: present
nginx_replicas: 2

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/defaultsmain2.yml -O ~/tutorial/example-role/defaults/main.yml

Modify nginx deployment definition nginx-deployment.yml.j2 to read replicas from the nginx_replicas variable

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-deployment
spec:
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.15.4
          ports:
          - containerPort: 80
  replicas: {{ nginx_replicas }}
  selector:
    name: nginx

You can easily update this file by running the following command:

wget -q https://exe-workshops.s3.amazonaws.com/nginx-deployment-updated.yml.j2 -O ~/tutorial/example-role/templates/nginx-deployment.yml.j2

Running the Playbook again will read the variable nginx_replicas and use the provided value to customize the nginx Deployment.

ansible-playbook -i myhosts playbook.yml

After running the Playbook, the cluster will scale down one of the nginx pods to meet the new requested replica count of 2.

oc get pods -n test

To remove the nginx deployment, we’ll override the state variable to contain state=absent using the -e / --extra-vars flag.

ansible-playbook -i myhosts playbook.yml --extra-vars state=absent

You should now see no resources in the test namespace

oc get all -n test