Ansible support in Operator SDK

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

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

    Example Ansible-based Operator annotation

    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.

    Example watches.yaml file

    1. - version: v1alpha1 (1)
    2. group: test1.example.com
    3. kind: Test1
    4. role: /opt/ansible/roles/Test1
    5. group: test2.example.com
    6. kind: Test2
    7. playbook: /opt/ansible/playbook.yml
    8. - version: v1alpha1 (3)
    9. group: test3.example.com
    10. kind: Test3
    11. playbook: /opt/ansible/test3.yml
    12. reconcilePeriod: 0
    13. manageStatus: false

    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.

    Example watches.yml file with advanced options

    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. kind: "Database"
    2. metadata:
    3. name: "example"
    4. spec:
    5. message: "Hello world 2"
    6. newParameter: "newParam"

    The structure passed to Ansible as extra variables is:

    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. ---
    2. - debug:
    3. msg: "name: {{ ansible_operator_meta.name }}, {{ ansible_operator_meta.namespace }}"

    Ansible Runner keeps information about Ansible runs in the container. This is located at .

    Additional resources