Creating Ansible-based Operators

This guide outlines Ansible support in the Operator SDK and walks Operator authors through examples building and running Ansible-based Operators with the operator-sdk CLI tool that use Ansible playbooks and modules.

Ansible support in the Operator SDK

The Operator Framework is an open source toolkit to manage Kubernetes native applications, called Operators, in an effective, automated, and scalable way. This framework includes the Operator SDK, which assists developers in bootstrapping and building an Operator based on their expertise without requiring knowledge of Kubernetes API complexities.

One of the Operator SDK options for generating an Operator project includes leveraging existing Ansible playbooks and modules to deploy Kubernetes resources as a unified application, without having to write any Go code.

Custom resource files

Operators use the Kubernetes extension mechanism, custom resource definitions (CRDs), so your custom resource (CR) looks and acts just like the built-in, native Kubernetes objects.

The CR file format is a Kubernetes resource file. The object has mandatory and optional fields:

Table 1. Custom resource fields
FieldDescription

apiVersion

Version of the CR to be created.

kind

Kind of the CR to be created.

metadata

Kubernetes-specific metadata to be created.

spec (optional)

Key-value list of variables which are passed to Ansible. This field is empty by default.

status

Summarizes the current state of the object. For Ansible-based Operators, the status subresource is enabled for CRDs and managed by the operator_sdk.util.k8s_status Ansible module by default, which includes condition information to the CR status.

annotations

Kubernetes-specific annotations to be appended to the CR.

The following list of CR annotations modify the behavior of the Operator:

Table 2. Ansible-based Operator annotations
AnnotationDescription

ansible.operator-sdk/reconcile-period

Specifies the reconciliation interval for the CR. This value is parsed using the standard Golang package time. Specifically, ParseDuration is used which applies the default suffix of s, giving the value in seconds.

Example Ansible-based Operator annotation

  1. apiVersion: "test1.example.com/v1alpha1"
  2. kind: "Test1"
  3. metadata:
  4. name: "example"
  5. annotations:
  6. ansible.operator-sdk/reconcile-period: "30s"

watches.yaml file

A group/version/kind (GVK) is a unique identifier for a Kubernetes API. The watches.yaml file contains a list of mappings from custom resources (CRs), identified by its GVK, to an Ansible role or playbook. The Operator expects this mapping file in a predefined location at /opt/ansible/watches.yaml.

Table 3. watches.yaml file mappings
FieldDescription

group

Group of CR to watch.

version

Version of CR to watch.

kind

Kind of CR to watch

role (default)

Path to the Ansible role added to the container. For example, if your roles directory is at /opt/ansible/roles/ and your role is named busybox, this value would be /opt/ansible/roles/busybox. This field is mutually exclusive with the playbook field.

playbook

Path to the Ansible playbook added to the container. This playbook is expected to be a way to call roles. This field is mutually exclusive with the role field.

reconcilePeriod (optional)

The reconciliation interval, how often the role or playbook is run, for a given CR.

manageStatus (optional)

When set to true (default), the Operator manages the status of the CR generically. When set to false, the status of the CR is managed elsewhere, by the specified role or playbook or in a separate controller.

Example watches.yaml file

  1. - version: v1alpha1 (1)
  2. group: test1.example.com
  3. kind: Test1
  4. role: /opt/ansible/roles/Test1
  5. - version: v1alpha1 (2)
  6. group: test2.example.com
  7. kind: Test2
  8. playbook: /opt/ansible/playbook.yml
  9. - version: v1alpha1 (3)
  10. group: test3.example.com
  11. kind: Test3
  12. playbook: /opt/ansible/test3.yml
  13. reconcilePeriod: 0
  14. manageStatus: false
1Simple example mapping Test1 to the test1 role.
2Simple example mapping Test2 to a playbook.
3More complex example for the Test3 kind. Disables re-queuing and managing the CR status in the playbook.

Advanced options

Advanced features can be enabled by adding them to your watches.yaml file per GVK. They can go below the group, version, kind and playbook or role fields.

Some features can be overridden per resource using an annotation on that CR. The options that can be overridden have the annotation specified below.

Table 4. Advanced watches.yaml file options
FeatureYAML keyDescriptionAnnotation for overrideDefault value

Reconcile period

reconcilePeriod

Time between reconcile runs for a particular CR.

ansbile.operator-sdk/reconcile-period

1m

Manage status

manageStatus

Allows the Operator to manage the conditions section of each CR status section.

true

Watch dependent resources

watchDependentResources

Allows the Operator to dynamically watch resources that are created by Ansible.

true

Watch cluster-scoped resources

watchClusterScopedResources

Allows the Operator to watch cluster-scoped resources that are created by Ansible.

false

Max runner artifacts

maxRunnerArtifacts

Manages the number of artifact directories that Ansible Runner keeps in the Operator container for each individual resource.

ansible.operator-sdk/max-runner-artifacts

20

Example watches.yml file with advanced options

  1. - version: v1alpha1
  2. group: app.example.com
  3. kind: AppService
  4. playbook: /opt/ansible/playbook.yml
  5. maxRunnerArtifacts: 30
  6. reconcilePeriod: 5s
  7. manageStatus: False
  8. watchDependentResources: False

Extra variables sent to Ansible

Extra variables can be sent to Ansible, which are then managed by the Operator. The spec section of the custom resource (CR) passes along the key-value pairs as extra variables. This is equivalent to extra variables passed in to the ansible-playbook command.

The Operator also passes along additional variables under the meta field for the name of the CR and the namespace of the CR.

For the following CR example:

  1. apiVersion: "app.example.com/v1alpha1"
  2. kind: "Database"
  3. metadata:
  4. name: "example"
  5. spec:
  6. message: "Hello world 2"
  7. newParameter: "newParam"

The structure passed to Ansible as extra variables is:

  1. { "meta": {
  2. "name": "<cr_name>",
  3. "namespace": "<cr_namespace>",
  4. },
  5. "message": "Hello world 2",
  6. "new_parameter": "newParam",
  7. "_app_example_com_database": {
  8. <full_crd>
  9. },
  10. }

The message and newParameter fields are set in the top level as extra variables, and meta provides the relevant metadata for the CR as defined in the Operator. The meta fields can be accessed using dot notation in Ansible, for example:

  1. - debug:
  2. msg: "name: {{ meta.name }}, {{ meta.namespace }}"

Ansible Runner directory

Ansible Runner keeps information about Ansible runs in the container. This is located at /tmp/ansible-operator/runner/<group>/<version>/<kind>/<namespace>/<name>.

Additional resources

Building an Ansible-based Operator using the Operator SDK

This procedure walks through an example of building a simple Memcached Operator powered by Ansible playbooks and modules using tools and libraries provided by the Operator SDK.

Prerequisites

  • Operator SDK v0.19.4 CLI installed on the development workstation

  • Access to a Kubernetes-based cluster v1.11.3+ (for example OKD 4.6) using an account with cluster-admin permissions

  • OpenShift CLI (oc) v4.6+ installed

  • ansible v2.9.0+

  • ansible-runner v1.1.0+

  • ansible-runner-http v1.0.0+

Procedure

  1. Create a new Operator project. A namespace-scoped Operator watches and manages resources in a single namespace. Namespace-scoped Operators are preferred because of their flexibility. They enable decoupled upgrades, namespace isolation for failures and monitoring, and differing API definitions.

    To create a new Ansible-based, namespace-scoped memcached-operator project and change to the new directory, use the following commands:

    1. $ operator-sdk new memcached-operator \
    2. --api-version=cache.example.com/v1alpha1 \
    3. --kind=Memcached \
    4. --type=ansible
    1. $ cd memcached-operator

    This creates the memcached-operator project specifically for watching the Memcached resource with API version example.com/v1apha1 and kind Memcached.

  2. Customize the Operator logic.

    For this example, the memcached-operator executes the following reconciliation logic for each Memcached custom resource (CR):

    • Create a memcached deployment if it does not exist.

    • Ensure that the deployment size is the same as specified by the Memcached CR.

    By default, the memcached-operator watches Memcached resource events as shown in the watches.yaml file and executes the Ansible role Memcached:

    1. - version: v1alpha1
    2. group: cache.example.com
    3. kind: Memcached

    You can optionally customize the following logic in the watches.yaml file:

    1. Specifying a role option configures the Operator to use this specified path when launching ansible-runner with an Ansible role. By default, the operator-sdk new command fills in an absolute path to where your role should go:

      1. - version: v1alpha1
      2. group: cache.example.com
      3. kind: Memcached
      4. role: /opt/ansible/roles/memcached
    2. Specifying a playbook option in the watches.yaml file configures the Operator to use this specified path when launching ansible-runner with an Ansible playbook:

      1. - version: v1alpha1
      2. group: cache.example.com
      3. kind: Memcached
      4. playbook: /opt/ansible/playbook.yaml
  3. Build the Memcached Ansible role.

    Modify the generated Ansible role under the roles/memcached/ directory. This Ansible role controls the logic that is executed when a resource is modified.

    1. Define the Memcached spec.

      Defining the spec for an Ansible-based Operator can be done entirely in Ansible. The Ansible Operator passes all key-value pairs listed in the CR spec field along to Ansible as variables. The names of all variables in the spec field are converted to snake case (lowercase with an underscore) by the Operator before running Ansible. For example, serviceAccount in the spec becomes service_account in Ansible.

      You should perform some type validation in Ansible on the variables to ensure that your application is receiving expected input.

      In case the user does not set the spec field, set a default by modifying the roles/memcached/defaults/main.yml file:

      1. size: 1
    2. Define the Memcached deployment.

      With the Memcached spec now defined, you can define what Ansible is actually executed on resource changes. Because this is an Ansible role, the default behavior executes the tasks in the roles/memcached/tasks/main.yml file.

      The goal is for Ansible to create a deployment if it does not exist, which runs the memcached:1.4.36-alpine image. Ansible 2.7+ supports the k8s Ansible module, which this example leverages to control the deployment definition.

      Modify the roles/memcached/tasks/main.yml to match the following:

      1. - name: start memcached
      2. k8s:
      3. definition:
      4. kind: Deployment
      5. apiVersion: apps/v1
      6. metadata:
      7. name: '{{ meta.name }}-memcached'
      8. namespace: '{{ meta.namespace }}'
      9. spec:
      10. replicas: "{{size}}"
      11. selector:
      12. matchLabels:
      13. app: memcached
      14. template:
      15. metadata:
      16. labels:
      17. app: memcached
      18. spec:
      19. containers:
      20. - name: memcached
      21. command:
      22. - memcached
      23. - -m=64
      24. - -o
      25. - modern
      26. - -v
      27. image: "docker.io/memcached:1.4.36-alpine"
      28. ports:
      29. - containerPort: 11211

      This example used the size variable to control the number of replicas of the Memcached deployment. This example sets the default to 1, but any user can create a CR that overwrites the default.

  4. Deploy the CRD.

    Before running the Operator, Kubernetes needs to know about the new custom resource definition (CRD) that the Operator will be watching. Deploy the Memcached CRD:

    1. $ oc create -f deploy/crds/cache.example.com_memcacheds_crd.yaml
  5. Build and run the Operator.

    There are two ways to build and run the Operator:

    • As a pod inside a Kubernetes cluster.

    • As a Go program outside the cluster using the operator-sdk up command.

    Choose one of the following methods:

    1. Run as a pod inside a Kubernetes cluster. This is the preferred method for production use.

      1. Build the memcached-operator image and push it to a registry:

        1. $ operator-sdk build quay.io/example/memcached-operator:v0.0.1
        1. $ podman push quay.io/example/memcached-operator:v0.0.1
      2. Deployment manifests are generated in the deploy/operator.yaml file. The deployment image in this file needs to be modified from the placeholder REPLACE_IMAGE to the previous built image. To do this, run:

        1. $ sed -i 's|REPLACE_IMAGE|quay.io/example/memcached-operator:v0.0.1|g' deploy/operator.yaml
      3. Deploy the memcached-operator manifests:

        1. $ oc create -f deploy/service_account.yaml
        1. $ oc create -f deploy/role.yaml
        1. $ oc create -f deploy/role_binding.yaml
        1. $ oc create -f deploy/operator.yaml
      4. Verify that the memcached-operator deployment is up and running:

        1. $ oc get deployment
        1. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
        2. memcached-operator 1 1 1 1 1m
    2. Run outside the cluster. This method is preferred during the development cycle to speed up deployment and testing.

      Ensure that Ansible Runner and Ansible Runner HTTP Plug-in are installed or else you will see unexpected errors from Ansible Runner when a CR is created.

      It is also important that the role path referenced in the watches.yaml file exists on your machine. Because normally a container is used where the role is put on disk, the role must be manually copied to the configured Ansible roles path (for example /etc/ansible/roles).

      1. To run the Operator locally with the default Kubernetes configuration file present at $HOME/.kube/config:

        1. $ operator-sdk run --local

        To run the Operator locally with a provided Kubernetes configuration file:

        1. $ operator-sdk run --local --kubeconfig=config
  1. Create a Memcached CR.

    1. Modify the deploy/crds/cache_v1alpha1_memcached_cr.yaml file as shown and create a Memcached CR:

      1. $ cat deploy/crds/cache_v1alpha1_memcached_cr.yaml

      Example output

      1. apiVersion: "cache.example.com/v1alpha1"
      2. kind: "Memcached"
      3. metadata:
      4. name: "example-memcached"
      5. spec:
      6. size: 3
      1. $ oc apply -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
    2. Ensure that the memcached-operator creates the deployment for the CR:

      1. $ oc get deployment

      Example output

      1. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
      2. memcached-operator 1 1 1 1 2m
      3. example-memcached 3 3 3 3 1m
    3. Check the pods to confirm three replicas were created:

      1. $ oc get pods
      1. NAME READY STATUS RESTARTS AGE
      2. example-memcached-6fd7c98d8-7dqdr 1/1 Running 0 1m
      3. example-memcached-6fd7c98d8-g5k7v 1/1 Running 0 1m
      4. example-memcached-6fd7c98d8-m7vn7 1/1 Running 0 1m
      5. memcached-operator-7cc7cfdf86-vvjqk 1/1 Running 0 2m
  2. Update the size.

    1. Change the spec.size field in the memcached CR from 3 to 4 and apply the change:

      1. $ cat deploy/crds/cache_v1alpha1_memcached_cr.yaml

      Example output

      1. apiVersion: "cache.example.com/v1alpha1"
      2. kind: "Memcached"
      3. metadata:
      4. name: "example-memcached"
      5. spec:
      6. size: 4
      1. $ oc apply -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
    2. Confirm that the Operator changes the deployment size:

      1. $ oc get deployment

      Example output

      1. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
      2. example-memcached 4 4 4 4 5m
  3. Clean up the resources:

    1. $ oc delete -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
    1. $ oc delete -f deploy/operator.yaml
    1. $ oc delete -f deploy/role_binding.yaml
    1. $ oc delete -f deploy/role.yaml
    1. $ oc delete -f deploy/service_account.yaml
    1. $ oc delete -f deploy/crds/cache_v1alpha1_memcached_crd.yaml

Managing application lifecycle using the k8s Ansible module

To manage the lifecycle of your application on Kubernetes using Ansible, you can use the k8s Ansible module. This Ansible module allows a developer to either leverage their existing Kubernetes resource files (written in YAML) or express the lifecycle management in native Ansible.

One of the biggest benefits of using Ansible in conjunction with existing Kubernetes resource files is the ability to use Jinja templating so that you can customize resources with the simplicity of a few variables in Ansible.

This section goes into detail on usage of the k8s Ansible module. To get started, install the module on your local workstation and test it using a playbook before moving on to using it within an Operator.

Installing the k8s Ansible module

To install the k8s Ansible module on your local workstation:

Procedure

  1. Install Ansible 2.9+:

    1. $ sudo yum install ansible
  2. Install the OpenShift python client package using pip:

    1. $ sudo pip install openshift
    1. $ sudo pip install kubernetes

Testing the k8s Ansible module locally

Sometimes, it is beneficial for a developer to run the Ansible code from their local machine as opposed to running and rebuilding the Operator each time.

Procedure

  1. Install the community.kubernetes collection:

    1. $ ansible-galaxy collection install community.kubernetes
  2. Initialize a new Ansible-based Operator project:

    1. $ operator-sdk new --type ansible \
    2. --kind Test1 \
    3. --api-version test1.example.com/v1alpha1 test1-operator

    Example output

    1. Create test1-operator/tmp/init/galaxy-init.sh
    2. Create test1-operator/tmp/build/Dockerfile
    3. Create test1-operator/tmp/build/test-framework/Dockerfile
    4. Create test1-operator/tmp/build/go-test.sh
    5. Rendering Ansible Galaxy role [test1-operator/roles/test1]...
    6. Cleaning up test1-operator/tmp/init
    7. Create test1-operator/watches.yaml
    8. Create test1-operator/deploy/rbac.yaml
    9. Create test1-operator/deploy/crd.yaml
    10. Create test1-operator/deploy/cr.yaml
    11. Create test1-operator/deploy/operator.yaml
    12. Run git init ...
    13. Initialized empty Git repository in /home/user/go/src/github.com/user/opsdk/test1-operator/.git/
    14. Run git init done
    1. $ cd test1-operator
  3. Modify the roles/test1/tasks/main.yml file with the Ansible logic that you want. This example creates and deletes a namespace with the switch of a variable.

    1. - name: set test namespace to "{{ state }}"
    2. community.kubernetes.k8s:
    3. api_version: v1
    4. kind: Namespace
    5. state: "{{ state }}"
    6. name: test
    7. ignore_errors: true (1)
    1Setting ignore_errors: true ensures that deleting a nonexistent project does not fail.
  4. Modify the roles/test1/defaults/main.yml file to set state to present by default:

    1. state: present
  5. Create an Ansible playbook playbook.yml in the top-level directory, which includes the test1 role:

    1. - hosts: localhost
    2. roles:
    3. - test1
  6. Run the playbook:

    1. $ ansible-playbook playbook.yml

    Example output

    1. [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
    2. PLAY [localhost] ***************************************************************************
    3. PROCEDURE [Gathering Facts] *********************************************************************
    4. ok: [localhost]
    5. Task [test1 : set test namespace to present]
    6. changed: [localhost]
    7. PLAY RECAP *********************************************************************************
    8. localhost : ok=2 changed=1 unreachable=0 failed=0
  7. Check that the namespace was created:

    1. $ oc get namespace

    Example output

    1. NAME STATUS AGE
    2. default Active 28d
    3. kube-public Active 28d
    4. kube-system Active 28d
    5. test Active 3s
  8. Rerun the playbook setting state to absent:

    1. $ ansible-playbook playbook.yml --extra-vars state=absent

    Example output

    1. [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
    2. PLAY [localhost] ***************************************************************************
    3. PROCEDURE [Gathering Facts] *********************************************************************
    4. ok: [localhost]
    5. Task [test1 : set test namespace to absent]
    6. changed: [localhost]
    7. PLAY RECAP *********************************************************************************
    8. localhost : ok=2 changed=1 unreachable=0 failed=0
  9. Check that the namespace was deleted:

    1. $ oc get namespace

    Example output

    1. NAME STATUS AGE
    2. default Active 28d
    3. kube-public Active 28d
    4. kube-system Active 28d

Testing the k8s Ansible module inside an Operator

After you are familiar with using the k8s Ansible module locally, you can trigger the same Ansible logic inside of an Operator when a custom resource (CR) changes. This example maps an Ansible role to a specific Kubernetes resource that the Operator watches. This mapping is done in the watches.yaml file.

Testing an Ansible-based Operator locally

After getting comfortable testing Ansible workflows locally, you can test the logic inside of an Ansible-based Operator running locally.

To do so, use the operator-sdk run --local command from the top-level directory of your Operator project. This command reads from the watches.yaml file and uses the ~/.kube/config file to communicate with a Kubernetes cluster just as the k8s Ansible module does.

Procedure

  1. Because the run --local command reads from the watches.yaml file, there are options available to the Operator author. If role is left alone (by default, /opt/ansible/roles/<name>) you must copy the role over to the /opt/ansible/roles/ directory from the Operator directly.

    This is cumbersome because changes are not reflected from the current directory. Instead, change the role field to point to the current directory and comment out the existing line:

    1. - version: v1alpha1
    2. group: test1.example.com
    3. kind: Test1
    4. # role: /opt/ansible/roles/Test1
    5. role: /home/user/test1-operator/Test1
  2. Create a custom resource definition (CRD) and proper role-based access control (RBAC) definitions for the custom resource (CR) Test1. The operator-sdk command autogenerates these files inside of the deploy/ directory:

    1. $ oc create -f deploy/crds/test1_v1alpha1_test1_crd.yaml
    1. $ oc create -f deploy/service_account.yaml
    1. $ oc create -f deploy/role.yaml
    1. $ oc create -f deploy/role_binding.yaml
  3. Run the run --local command:

    1. $ operator-sdk run --local

    Example output

    1. [...]
    2. INFO[0000] Starting to serve on 127.0.0.1:8888
    3. INFO[0000] Watching test1.example.com/v1alpha1, Test1, default
  4. Now that the Operator is watching the resource Test1 for events, the creation of a CR triggers your Ansible role to execute. View the deploy/cr.yaml file:

    1. apiVersion: "test1.example.com/v1alpha1"
    2. kind: "Test1"
    3. metadata:
    4. name: "example"

    Because the spec field is not set, Ansible is invoked with no extra variables. The next section covers how extra variables are passed from a CR to Ansible. This is why it is important to set reasonable defaults for the Operator.

  5. Create a CR instance of Test1 with the default variable state set to present:

    1. $ oc create -f deploy/cr.yaml
  6. Check that the namespace test was created:

    1. $ oc get namespace

    Example output

    1. NAME STATUS AGE
    2. default Active 28d
    3. kube-public Active 28d
    4. kube-system Active 28d
    5. test Active 3s
  7. Modify the deploy/cr.yaml file to set the state field to absent:

    1. apiVersion: "test1.example.com/v1alpha1"
    2. kind: "Test1"
    3. metadata:
    4. name: "example"
    5. spec:
    6. state: "absent"
  8. Apply the changes and confirm that the namespace is deleted:

    1. $ oc apply -f deploy/cr.yaml
    1. $ oc get namespace

    Example output

    1. NAME STATUS AGE
    2. default Active 28d
    3. kube-public Active 28d
    4. kube-system Active 28d

Testing an Ansible-based Operator on a cluster

After getting familiar running Ansible logic inside of an Ansible-based Operator locally, you can test the Operator inside of a pod on a Kubernetes cluster, such as OKD. Running as a pod on a cluster is preferred for production use.

Procedure

  1. Build the test1-operator image and push it to a registry:

    1. $ operator-sdk build quay.io/example/test1-operator:v0.0.1
    1. $ podman push quay.io/example/test1-operator:v0.0.1
  2. Deployment manifests are generated in the deploy/operator.yaml file. The deployment image in this file must be modified from the placeholder REPLACE_IMAGE to the previously-built image. To do so, run the following command:

    1. $ sed -i 's|REPLACE_IMAGE|quay.io/example/test1-operator:v0.0.1|g' deploy/operator.yaml

    If you are performing these steps on macOS, use the following command instead:

    1. $ sed -i "" 's|REPLACE_IMAGE|quay.io/example/test1-operator:v0.0.1|g' deploy/operator.yaml
  3. Deploy the test1-operator:

    1. $ oc create -f deploy/crds/test1_v1alpha1_test1_crd.yaml (1)
    1Only required if the CRD does not exist already.
    1. $ oc create -f deploy/service_account.yaml
    1. $ oc create -f deploy/role.yaml
    1. $ oc create -f deploy/role_binding.yaml
    1. $ oc create -f deploy/operator.yaml
  4. Verify that the test1-operator is up and running:

    1. $ oc get deployment

    Example output

    1. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
    2. test1-operator 1 1 1 1 1m
  5. You can now view the Ansible logs for the test1-operator:

    1. $ oc logs deployment/test1-operator

Managing custom resource status using the operator_sdk.util Ansible collection

Ansible-based Operators automatically update custom resource (CR) status subresources with generic information about the previous Ansible run. This includes the number of successful and failed tasks and relevant error messages as shown:

  1. status:
  2. conditions:
  3. - ansibleResult:
  4. changed: 3
  5. completion: 2018-12-03T13:45:57.13329
  6. failures: 1
  7. ok: 6
  8. skipped: 0
  9. lastTransitionTime: 2018-12-03T13:45:57Z
  10. message: 'Status code was -1 and not [200]: Request failed: <urlopen error [Errno
  11. 113] No route to host>'
  12. reason: Failed
  13. status: "True"
  14. type: Failure
  15. - lastTransitionTime: 2018-12-03T13:46:13Z
  16. message: Running reconciliation
  17. reason: Running
  18. status: "True"
  19. type: Running

Ansible-based Operators also allow Operator authors to supply custom status values with the k8s_status Ansible module, which is included in the operator_sdk.util collection. This allows the author to update the status from within Ansible with any key-value pair as desired.

By default, Ansible-based Operators always include the generic Ansible run output as shown above. If you would prefer your application did not update the status with Ansible output, you can track the status manually from your application.

Procedure

  1. To track CR status manually from your application, update the watches.yaml file with a manageStatus field set to false:

    1. - version: v1
    2. group: api.example.com
    3. kind: Test1
    4. role: Test1
    5. manageStatus: false
  2. Use the operator_sdk.util.k8s_status Ansible module to update the subresource. For example, to update with key test1 and value test2, operator_sdk.util can be used as shown:

    1. - operator_sdk.util.k8s_status:
    2. api_version: app.example.com/v1
    3. kind: Test1
    4. name: "{{ meta.name }}"
    5. namespace: "{{ meta.namespace }}"
    6. status:
    7. test1: test2

    Collections can also be declared in the meta/main.yml for the role, which is included for new scaffolded Ansible Operators:

    1. collections:
    2. - operator_sdk.util

    Declaring collections in the role meta allows you to invoke the k8s_status module directly:

    1. k8s_status:
    2. <snip>
    3. status:
    4. test1: test2

Additional resources

Additional resources