ansible - J2 template

22 views
Skip to first unread message

dudu.c...@gmail.com

unread,
Jul 1, 2021, 2:17:21 AM7/1/21
to Ansible Project
Ansible question 
I have a variable, let call it value1 and a J2 template.

The J2 template includes the following line :
LINE=” This string mut be included      this string is optional= {{ value1}}”

What I need to implement 

If value1 is defined with a string – let assume that the string is OK  (Value1: OK)
, the file should be 
LINE=” This string must be included        this string is optional= OK”


If value1 is not defined (empty Value1: ) –the file should be 
LINE=” This string must be included”

So how the J2 should be configured?

Dick Visser

unread,
Jul 1, 2021, 3:25:54 AM7/1/21
to ansible...@googlegroups.com
LINE=” This string must be included{% if value1 is defined %}
this string is optional= {{ value1 }}{% endif %}”
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f9c56cbd-6356-4efd-b0d7-2e6abe9faf9dn%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Vladimir Botka

unread,
Jul 1, 2021, 4:55:22 AM7/1/21
to dudu.c...@gmail.com, ansible...@googlegroups.com
On Wed, 30 Jun 2021 23:17:21 -0700 (PDT)
"dudu.c...@gmail.com" <dudu.c...@gmail.com> wrote:

> If value1 is not defined (empty Value1: )

Test the variable is both defined and not empty,
e.g. (minimal version)

{% if value1 is defined and value1|d('')|length > 0 %}
{{ value1 }}
{% else %}
This string must be included
{% endif %}

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages