Removal of jinja conditional blocks #113
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 aternary()
call), to then be joined usingjoin(' ')
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(' ') }}