Ansible Role as Operator

Make sure you have ansible and python-ansible-runner installed and the following pip modules:

  • ansible-runner-http

  • kubernetes

  • openshift

Scaffold the new operator

create a new directory on your machine, for instance : $HOME/vis-ansible

operator-sdk init --domain com.redhat --plugins ansible

Generate an API

Le’s create an API for our operator :

operator-sdk create api --group operators --version v1 --kind VisitorSite

Copy Ansible roles

In your new project you have a folder : roles create a folder visitorsite , and copy the content from $TUTORIAL_HOME/apps/ansible

Edit the watches

For running the oeprator locally you first need to update the watches.yml file :

Open the file and replace :

# FIXME: Specify the role or playbook for this resource.

with the complete path to your roles/visitorsite folder, i.e /home/sebastienblanc/deepdives/ansible-vis/roles/visitorsite

- version: v1
  group: operators.com.redhat
  kind: VisitorSite
  role: /home/sebastienblanc/deepdives/ansible-vis/roles/visitorsite

Apply a Custom Resource

You can now apply a custom resource

apiVersion: operators.com.redhat/v1
kind: VisitorSite
metadata:
  name: visitorsite-sample
spec:
  size: 1
  title: Hello Ansible
kubectl apply -f $TUTORIAL_HOME/apps/cr/visitorsite-ansible.yml

Check the pods getting created :

kubectl get pods

NAME                                 READY   STATUS    RESTARTS   AGE

mysql-86c559bb7f-kjjvt               1/1     Running   0          28h

visitors-backend-7489bb97dd-wggkt    1/1     Running   0          28h

visitors-frontend-86df47fffc-d2bgl   1/1     Running   0          28h