Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ignore an element from a list when looping through with Jinja?

Tags:

ansible

jinja2

I have an output as follows:

[{ 
   'stderr': 'error: cannot open file',
},
{ 
   'stderr': '',
}]

Jinja snipper:

{{ php_command_result.results | map(attribute='stderr') | sort | join('\r - ') }}"

Returns a trailing - at the end because stderr is empty. How can I ignore empty values?

like image 916
sdot257 Avatar asked Aug 11 '15 18:08

sdot257


People also ask

How do you run a loop in Jinja?

Jinja2 being a templating language has no need for wide choice of loop types so we only get for loop. For loops start with {% for my_item in my_collection %} and end with {% endfor %} . This is very similar to how you'd loop over an iterable in Python.


1 Answers

Did you try rejectattr?

{{ php_command_result.results | rejectattr('stderr', 'equalto', '') | map(attribute='stderr') | sort | join('\r - ') }}"
like image 192
Emilio Garçia Avatar answered Oct 13 '22 21:10

Emilio Garçia