Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removal of jinja conditional blocks #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mjbnz
Copy link
Contributor

@mjbnz mjbnz commented Jun 30, 2020

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(' ') }}

Signed-off-by: Mike Beattie <mike@ethernal.org>
@mjbnz mjbnz changed the title Jinja syntax clean Removal of jinja conditional blocks Jun 30, 2020
Signed-off-by: Mike Beattie <mike@ethernal.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant