I am trying to use the default filter in Jinja2 to replace missing values with default values.
It turns out that my data source has some NaNs, not just missing values.
For my use case, I want to replace NaNs with a default value.
But the default filter just passes NaN through without substitution.
Are there any filters in Jinja2 which would allow me to replace NaN values with something else, easily? (Of course I could write a custom filter, I'm just wondering if there's a simpler solution.)
For context, I'm doing this from Ansible, which has a few more filters available than plain Jinja2.
MWE
playbook.yaml
---
- hosts: localhost
connection: local
tasks:
- name: generate cloudformation template
template:
src: input.j2
dest: output.txt
vars:
data:
- value: 123
comment: this is an integer
- value: None
comment: This is None
- value: NaN
comment: This is NaN
- comment: this is missing
- value: ''
comment: empty string
input.j2
{% for row in data %}
{{ row['comment'] }}
{{ row['value'] | default('default') }}
{{ row['value'] | default('default', boolean=True) }}
{% endfor %}
Run with:
ansible-playbook playbook.yaml
cat output.txt
Actual behavior
this is an integer
123
123
123
This is None
None
None
default
This is NaN
NaN
default
default
this is missing
default
default
default
empty string
default
default
Desired behavior
...
This is NaN
NaN
default
default
...
(It would be good to also have a solution that replaces Nones. I'm not sure why None | default('x', boolean=True) doesn't do that, since None is falsey.)