Getting started with Operator SDK for Ansible-based Operators

    To demonstrate the basics of setting up and running an Ansible-based Operator using tools and libraries provided by the Operator SDK, Operator developers can build an example Ansible-based Operator for Memcached, a distributed key-value store, and deploy it to a cluster.

    • Operator SDK CLI installed

    • OpenShift CLI () v4.13+ installed

    • Ansible v2.9.0

    • v2.0.2+

    • Ansible Runner HTTP Event Emitter plugin v1.0.0+

    • 3.8.6+

    • OpenShift Python client v0.12.0+

    • Logged into an OKD 4.13 cluster with oc with an account that has cluster-admin permissions

    • To allow the cluster to pull the image, the repository where you push your image must be set as public, or you must configure an image pull secret

    You can build and deploy a simple Ansible-based Operator for Memcached by using the Operator SDK.

    Procedure

    1. Create a project.

      1. Create your project directory:

      2. Run the operator-sdk init command with the ansible plugin to initialize the project:

        1. $ operator-sdk init \
        2. --plugins=ansible \
        3. --domain=example.com
    2. Create an API.

      Create a simple Memcached API:

      1. --group cache \
      2. --version v1 \
      3. --kind Memcached \
      4. --generate-role (1)
    3. Use the default Makefile targets to build and push your Operator. Set IMG with a pull spec for your image that uses a registry you can push to:

    4. Run the Operator.

      1. Install the CRD:

        1. $ make install
      2. Deploy the project to the cluster. Set IMG to the image that you pushed:

      3. Create a sample custom resource (CR).

        1. Create a sample CR:

          1. $ oc apply -f config/samples/cache_v1_memcached.yaml \
          2. -n memcached-operator-system
        2. Watch for the CR to reconcile the Operator:

          Example output

          1. ...
          2. I0205 17:48:45.881666 7 leaderelection.go:253] successfully acquired lease memcached-operator-system/memcached-operator
          3. {"level":"info","ts":1612547325.8819902,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting EventSource","source":"kind source: cache.example.com/v1, Kind=Memcached"}
          4. {"level":"info","ts":1612547325.98242,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting Controller"}
          5. {"level":"info","ts":1612547325.9824686,"logger":"controller-runtime.manager.controller.memcached-controller","msg":"Starting workers","worker count":4}
          6. {"level":"info","ts":1612547348.8311093,"logger":"runner","msg":"Ansible-runner exited successfully","job":"4037200794235010051","name":"memcached-sample","namespace":"memcached-operator-system"}
      4. Delete a CR

        Delete a CR by running the following command:

      5. Run the following command to clean up the resources that have been created as part of this procedure:

        1. $ make undeploy
      • See for a more in-depth walkthrough on building an Ansible-based Operator.