jinja2 default fiter set to None

5 views
Skip to first unread message

Peter Hudec

unread,
Oct 19, 2017, 6:28:24 PM10/19/17
to Ansible Project
Hi,

is there any way how to set undefined variable to None /python/, null
/yaml/ value ?

The filter default sets it always to empty string ... Consider this in
defaults and emplate file ...


``` defaults
sshd_config_test_config: "{{ __sshd_config_test_config|default(None)}} "
```

``` template
{% macro print_scalar(item_name, item_value=None, ident=0,
comment=False) -%}
{% if item_value is not none %}
{{"\t" * ident}}{{ item_name }} {{ item_value }}
{%- elif comment %}
{{"\t" * ident}}# {{ item_name }}
{%- endif %}
{%- endmacro %}


{{ print_scalar('test_config', sshd_config_test_config, 0, True) }}
```

This will print config value with an empty string, the jinja2 template
does is when __sshd_config_test_config is not defined.

if __sshd_config_test_config is set to value or null, it works as
expected. This varialble is OS based and included in os-{{
ansible-os-family}}.yml.

I'm trying to avoid type all variables into all os specific files.

thanks
Peter
--
*Peter Hudec*
Infraštruktúrny architekt
phu...@cnc.sk <mailto:phu...@cnc.sk>

*CNC, a.s.*
Borská 6, 841 04 Bratislava
Recepcia: +421 2  35 000 100

Mobil:+421 905 997 203
*www.cnc.sk* <http:///www.cnc.sk>

Reply all
Reply to author
Forward
0 new messages