How to improve the readability of block scalars when used with jinja

82 views
Skip to first unread message

jean-christophe manciot

unread,
Mar 17, 2021, 2:56:39 PM3/17/21
to Ansible Project
Hello everyone.
Block scalars are described here.

The goal is to be improve the readability of the following block scalar example and obtain the intended value for the variable: 
```
- set_fact:
        variable: "{% if condition_1 is true %}{% if condition_2 is true %}'value'{% else %}'another value'{% endif %}{% else %}{{ another_variable }}{% endif %}"
```
I unsuccessfully tried:
```
- set_fact:
        variable: >-
                "{% if condition_1 is true %}
                        {% if condition_2 is true %}
                                'value'
                        {% else %}
                                'another value'
                        {% endif %}
                {% else %}
                        {{ another_variable }}
                {% endif %}"
```
Unfortunately, some spaces are added in the variable set value and there does not seem to be a way to completely "prune" them.

Am I missing something?

Matt Martz

unread,
Mar 17, 2021, 3:05:36 PM3/17/21
to ansible...@googlegroups.com

--
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/1b2e9cf5-923a-4891-878f-7d364573e2aan%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

jean-christophe manciot

unread,
Mar 18, 2021, 8:06:05 AM3/18/21
to Ansible Project
Thanks @sivel.
This is possible with:
  • #jinja2: lstrip_blocks: "true", trim_blocks: "true" (as first line of yml task file before ---)
  • all {% and %} replaced with {%- and -%} respectively
  • both double-quotes removed from my previous example (which is normal jinja actually)
  • all \\n replaced by \n in all regex_replace (n being a number)
  • maybe there are other pitfalls
Reply all
Reply to author
Forward
0 new messages