When using an Ansible Operator on Kubernetes or OpenShift, how do I get the values of nested spec items?

9/23/2019

I have a custom resource in my OpenShift cluster (kubernetes cluster) that looks something like below. I want my Ansible operator, made using Operator-SDK, to consume the value of spec.storage.size and spec.storage.type. How do I do that?

apiVersion: my.domain.com/v1alpha1                                                                                                                                                                                                                                      
kind: MyApp                                                                                                                                                                                                                                                  
metadata:                                                                                                                                                                                                                                                                         
  name: my-myapp                                                                                                                                                                                                                                                                
spec:                                                                                                                                                                                                                                                 
  storage:
    size: 1Gi
    type: persistent
-- Josiah
ansible
kubernetes

1 Answer

9/23/2019

The storage yaml block is passed as a hash to Ansible. You can use either bracket or dot notation to find the value. In the lines below, I'm using dot notation to get the size and bracket notation to determine if the type of storage is persistent.

- debug:
    msg: "Requested size is {{ storage.size }}."
  when: storage['type'] == "persistent"
-- Josiah
Source: StackOverflow