Skip to content

Ansible Operator

Ansible Operator example

Resources:

Create first ansible operator

mkdir sample-operator
cd sample-operator

operator-sdk init \
  --plugins=ansible.sdk.operatorframework.io/v1 \
  --domain=example.com \
  --group=app --version=v1alpha1 --kind=AppService \
  --generate-playbook \
  --generate-role

make docker-build docker-push \
  IMG="quay.io/openshift-examples/ansible-example-operator:latest"

make deploy \
  IMG="quay.io/openshift-examples/ansible-example-operator:latest"


kubectl get pods -n sample-operator-system --watch

kubectl apply -f config/samples/app_v1alpha1_appservice.yaml

kubectl logs -n sample-operator-system \
    -l control-plane=controller-manager -c manager --tail=-1

Adjust roles/appservice/tasks/main.yml

---
# tasks
- name: Print some debug information
  vars:
    msg: |
        Module Variables ("vars"):
        --------------------------------
        {{ vars | to_nice_json }}

        Environment Variables ("environment"):
        --------------------------------
        {{ environment | to_nice_json }}

        GROUP NAMES Variables ("group_names"):
        --------------------------------
        {{ group_names | to_nice_json }}

        GROUPS Variables ("groups"):
        --------------------------------
        {{ groups | to_nice_json }}

        HOST Variables ("hostvars"):
        --------------------------------
        {{ hostvars | to_nice_json }}

  debug:
    msg: "{{ msg.split('\n') }}"

Rebuild and redeploy

1
2
3
4
5
make docker-build docker-push \
  IMG="quay.io/openshift-examples/ansible-example-operator:latest"

make deploy \
  IMG="quay.io/openshift-examples/ansible-example-operator:latest"

Cleanup

make undeploy

2021-09-18 2019-10-08 Contributors: Robert Bohne