I am currently trying to loop over a subset of k8s_facts. My fact looks something like:
{
"resources": [
{
"metadata": {
"annotations": {
"com.foo.bar/name": "foo",
"com.foo.bar/foo-name": "baz"
},
"creationTimestamp": "2018-12-20T02:29:50Z",
"name": "foo-bar"
}
},
...
I want to filter on a specific value of the com.foo.bar/foo-name
key. Because the key has .
, -
and /
, it doesn't play well with the Jinja2 selectattr
function. I tried to do something like that, but in vain:
- debug:
msg: "{{ item }}"
loop: "{{ my_fact.resources | selectattr('metadata.annotations[\\'com.foo.bar/foo-name\\']', 'defined') | selectattr('metadata.annotations[\\'com.foo.bar/foo-name\\']', 'match', 'baz') | list }}"
loop_control:
label: "{{ item.metadata.name }}"
When executing the previous, I get this error:
fatal: [<redacted>]: FAILED! => {"msg": "template error while templating string: expected token ',', got 'com'. String: {{ my_fact.resources | selectattr('metadata.annotations[\\\\'com.foo.bar/foo-name\\\\']', 'defined') | selectattr('metadata.annotations[\\\\'com.foo.bar/foo-name\\\\']', 'match', 'baz') | list }}"}
My question is, how can I escape complex strings containings dots in Jinja2?