Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions templates/macros.j2
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{%- macro insert_value(value) -%}{#- Inserts the value correctly formatted and quoted for UCL -#}
{%- if value | type == "bool" -%}
{%- if value is boolean -%}
{#- bool -#}
{{ value | bool | lower }}
{%- elif value | type == "int" or value | type == "float" -%}
{%- elif value is number -%}
{#- numeric -#}
{{ value | string }}
{%- elif value | type == "unicode" or value | type == "AnsibleUnsafeText" or value | type == "str" or value | type == "AnsibleUnicode" -%}
{%- elif value is string -%}
{#- Catch-all for any string-like object, including Ansible tags/vaults -#}
{%- if value | regex_search('^([0-9])+(.[0-9]+)?(s|m|h|d|min)$') -%}
{#- time -#}
{{ value }}
Expand All @@ -20,16 +21,23 @@

{% macro is_complex(var) %}{#- Checks whether a list contains another list or dict -#}
{%- for item in var -%}
{%- if item | type == "dict" or item | type == "dict" -%}
True
{%- if item is mapping or (item is sequence and item is not string) -%}
true
{%- endif -%}
{%- endfor -%}
{% endmacro %}

{%- macro insert_dict(key, value) -%}{#- Formats a variable for inserting it into UCL -#}
{%- if value | type == "list" -%}
{%- if value is mapping -%}
{#- dict -#}
{{ key }} {
{% for k, v in value.items() %}
{{ insert_dict(k, v) | indent(4, True) }}
{% endfor -%}
}
{%- elif value is sequence and value is not string -%}
{#- list: If one of the items is complex, repeatedly define them, otherwise use an array -#}
{%- if is_complex(value) | length > 0 -%}{#- Implementing this by setting a variable is not possible because Jinja fails unexpectedly when using 'is defined' in this macro -#}
{%- if is_complex(value) -%}{#- Implementing this by setting a variable is not possible because Jinja fails unexpectedly when using 'is defined' in this macro -#}
{%- for item in value %}
{{ insert_dict(key, item) }}
{% endfor -%}
Expand All @@ -40,13 +48,6 @@
{%- endfor -%}
]
{%- endif -%}
{%- elif value | type == "dict" -%}
{#- dict -#}
{{ key }} {
{% for k, v in value.items() %}
{{ insert_dict(k, v) | indent(4, True) }}
{% endfor -%}
}
{%- else -%}
{#- simple type like string, number, bool -#}
{{ key }} = {{ insert_value(value) }};
Expand Down