ansible-haproxy icon indicating copy to clipboard operation
ansible-haproxy copied to clipboard

Removal of jinja conditional blocks

Open mjbnz opened this issue 4 years ago • 0 comments

This Pull Request is a little on the larger side, but in principle is quite simple. Due to ansible setting trim_blocks to true for jinja templating, having a block that ends a line eats the following line break, as evidenced by the blank lines that were strategically placed into the templates.

Changing from a jinja conditional block to a variable expression with filters avoids this.

The following two methods have been used - one for expected bare strings, and the other with expected lists. In both instances, where a leading space is needed to be generated, an empty string list (['']) is prepended to the variable (coerced into a list in the case of strings using a ternary() call), to then be joined using join(' ') to add spaces between all elements - including the first, which is now an empty string, to create the leading space.

Strings:

{{ string_var is defined | ternary([''] + [string_var], []) | join(' ') }}

Lists:

Where var is required and a space is given before the opening braces:

{{ list_var | join(' ') }}

Where an inital space is needed because the var is optional:

{{ ([''] + list_var | default([])) | join(' ') }}

mjbnz avatar Jun 30 '20 06:06 mjbnz