Brooke
Brooke

Reputation: 435

Ansible - How to Escape Nested Jinja within Filter

I want to use nested jinja expression within filter, but am failing it to do.

I have a variable file that looks like this:

NAME: "test"
VAR: ["a","b","c-test","d", "t-test"]

and now, I want to use ansible filters in the same variable file to extract a specific string from the VAR list.

I have tried:

NAME: "test"
VAR: ["a","b","c-test","d","test-t"]

testc: "{{ VAR | select('match', 'c-{{ NAME }}') | list }}"
testt: "{{ VAR | select('match', '{{ NAME }}-t') | list }}"

and

NAME: "test"
VAR: ["a","b","c-test","d","test-t"]

testc: "{{ VAR | select('match', 'c-\'{{ NAME }}\'') | list }}" 
testt: "{{ VAR | select('match', '{{ NAME }}-t') | list }}"

with no success.

I have the same issue to use nested Jinja within filters other the 'select'.

How do you use or escape the nested jinja expression within ansible filter?

Upvotes: 1

Views: 1302

Answers (1)

blhsing
blhsing

Reputation: 106445

You can't nest double curly brackets. Names inside double curly brackets are evaluated as variables already so you don't need to enclose them in another pair of double curly brackets to evaluate them. You can simply use the concatenation operator ~ to concatenate the string literal 'c-' with the variable NAME directly:

test: "{{ VAR | select('match', 'c-' ~ NAME) | list }}"
test: "{{ VAR | select('match', NAME ~ '-t') | list }}"

Upvotes: 2

Related Questions