Skip to content

Ansible Operator

Ansible Operator example

Resources:

Create first ansible operator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
---
# 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

1
make undeploy

Last update: September 18, 2021
Back to top