When using an Ansible-based Operator, the Operator automatically passes variables from your Custom Resource (CR) spec to Ansible as extra-vars. This behavior is equivalent to passing variables via the --extra-vars flag in ansible-playbook.
In addition to the keys defined in your spec, the Operator injects a special ansible_operator_meta object containing the CR's name and namespace. It also provides the full CR and the CR's spec under keys prefixed with the API group (e.g., _app_example_com_database).
To access the CR metadata in your Ansible tasks, use dot notation on the ansible_operator_meta object.
# Example Custom Resource
apiVersion: "cache.example.com/v1alpha1"
kind: "Memcached"
metadata:
name: "memcached-sample"
spec:
message: "Hello world 2"
newParameter: "newParam"
# --- Resulting structure passed to Ansible ---
{
"ansible_operator_meta": {
"name": "memcached-sample",
"namespace": "<cr-namespace>"
},
"message": "Hello world 2",
"new_parameter": "newParam",
"_cache_example_com_memcached": { <Full CR> },
"_cache_example_com_memcached_spec": { <Full CR .spec> }
}
# --- Accessing metadata in an Ansible task ---
- debug:
msg: "name: {{ ansible_operator_meta.name }}, {{ ansible_operator_meta.namespace }}"