|
| 1 | +{# vim: foldmarker=[[[,]]]:foldmethod=marker |
| 2 | +# Commonly used set of macros in DebOps. |
| 3 | +# It can be imported in repositories as needed. |
| 4 | +# Changes to this file should go upstream: https://github.com/debops/debops-playbooks/blob/master/templates/debops__tpl_macros.j2 |
| 5 | +# |
| 6 | +# Copyright [[[ |
| 7 | +# ============= |
| 8 | +# |
| 9 | +# Copyright (C) 2014-2017 Maciej Delmanowski <[email protected]> |
| 10 | +# Copyright (C) 2015-2017 Robin Schneider <[email protected]> |
| 11 | +# Copyright (C) 2014-2017 DebOps https://debops.org/ |
| 12 | +# |
| 13 | +# This file is part of DebOps. |
| 14 | +# |
| 15 | +# DebOps is free software; you can redistribute it and/or modify |
| 16 | +# it under the terms of the GNU General Public License version 3, as |
| 17 | +# published by the Free Software Foundation. |
| 18 | +# |
| 19 | +# DebOps is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with DebOps. If not, see https://www.gnu.org/licenses/. |
| 26 | +# |
| 27 | +# ]]] |
| 28 | +# |
| 29 | +# Usage [[[ |
| 30 | +# ========= |
| 31 | +# |
| 32 | +# Copy the template file to `./templates/import/debops__tpl_macros.j2` of your |
| 33 | +# role and import it from there into various other templates or even use it |
| 34 | +# in templates which are called by {{ lookup("template", ...) }}. |
| 35 | +# |
| 36 | +# Make sure to retain the filename of this file so that automatic updates of |
| 37 | +# this file can be implemented. |
| 38 | +# |
| 39 | +# To use the macros in your own template, this file needs to be imported like so: |
| 40 | +# |
| 41 | +# {% import 'templates/import/debops__tpl_macros.j2' as debops__tpl_macros with context %} |
| 42 | +# |
| 43 | +# Then you can start using the macros like this: |
| 44 | +# |
| 45 | +# {{ debops__tpl_macros.indent(some_content, 4) }} |
| 46 | +# |
| 47 | +# ]]] #} |
| 48 | + |
| 49 | +{% macro get_yaml_list_for_elem(list_or_elem) %}{# [[[ #} |
| 50 | +{{ ([ list_or_elem ] |
| 51 | + if (list_or_elem is string or list_or_elem in [True, False]) |
| 52 | + else (list_or_elem|list)) | to_nice_yaml }} |
| 53 | +{% endmacro %}{# ]]] #} |
| 54 | + |
| 55 | +{% macro get_realm_yaml_list(domains, fallback_realm) %}{# [[[ #} |
| 56 | +{% set custom_realm_list = [] %} |
| 57 | +{% if domains and (ansible_local|d() and ansible_local.pki|d() and ansible_local.pki.known_realms|d()) %} |
| 58 | +{% for domain in (get_yaml_list_for_elem(domains) | from_yaml) %} |
| 59 | +{% if domain in ansible_local.pki.known_realms %} |
| 60 | +{% set _ = custom_realm_list.append(domain) %} |
| 61 | +{% elif (domain.split('.')[1:] | join('.')) in ansible_local.pki.known_realms %} |
| 62 | +{% set _ = custom_realm_list.append(domain.split('.')[1:] | join('.')) %} |
| 63 | +{% endif %} |
| 64 | +{% endfor %} |
| 65 | +{% endif %} |
| 66 | +{% if custom_realm_list|length == 0 %} |
| 67 | +{% set _ = custom_realm_list.append(fallback_realm) %} |
| 68 | +{% endif %} |
| 69 | +{{ custom_realm_list | to_nice_yaml }} |
| 70 | +{% endmacro %}{# ]]] #} |
| 71 | + |
| 72 | +{% macro get_apache_version() %}{# [[[ #} |
| 73 | +{{ ansible_local.apache.version |
| 74 | + if (ansible_local|d() and ansible_local.apache|d() and |
| 75 | + ansible_local.apache.version|d()) |
| 76 | + else "2.4.0" -}} |
| 77 | +{% endmacro %}{# ]]] #} |
| 78 | + |
| 79 | +{% macro get_apache_min_version() %}{# [[[ #} |
| 80 | +{{ ansible_local.apache.min_version |
| 81 | + if (ansible_local|d() and ansible_local.apache|d() and |
| 82 | + ansible_local.apache.min_version|d()) |
| 83 | + else "2.4.0" -}} |
| 84 | +{% endmacro %}{# ]]] #} |
| 85 | + |
| 86 | +{% macro get_openssl_version() %}{# [[[ #} |
| 87 | +{{ ansible_local.pki.openssl_version |
| 88 | + if (ansible_local|d() and ansible_local.pki|d() and |
| 89 | + ansible_local.pki.openssl_version|d()) |
| 90 | + else "0.0.0" }} |
| 91 | +{% endmacro %}{# ]]] #} |
| 92 | + |
| 93 | +{% macro get_gnutls_version() %}{# [[[ #} |
| 94 | +{{ ansible_local.pki.gnutls_version |
| 95 | + if (ansible_local|d() and ansible_local.pki|d() and |
| 96 | + ansible_local.pki.gnutls_version|d()) |
| 97 | + else "0.0.0" }} |
| 98 | +{% endmacro %}{# ]]] #} |
| 99 | + |
| 100 | +{% macro indent(content, width=4, indentfirst=False) %}{# [[[ #} |
| 101 | +{# Fixed version of the `indent` filter which does not insert trailing spaces on empty lines. |
| 102 | +## Note that you can not use this macro like a filter but have to use it like a regular macro. |
| 103 | +## Example: {{ debops__tpl_macros.indent(some_content, 4) }} |
| 104 | +## |
| 105 | +## Python re.sub seems to default to re.MULTILINE in Ansible. |
| 106 | +#} |
| 107 | +{{ content | indent(width, indentfirst) | regex_replace("[ \\t\\r\\f\\v]+(\\n|$)", "\\1") -}} |
| 108 | +{% endmacro %}{# ]]] #} |
| 109 | + |
| 110 | +{% macro merge_dict(current_dict, to_merge_dict, dict_key='name') %}{# [[[ #} |
| 111 | +{% set merged_dict = current_dict %} |
| 112 | +{% if to_merge_dict %} |
| 113 | +{% if to_merge_dict is mapping %} |
| 114 | +{% for dict_name in to_merge_dict.keys() | sort %} |
| 115 | +{% if to_merge_dict[dict_name][dict_key]|d() %} |
| 116 | +{% set _ = merged_dict.update({to_merge_dict[dict_name][dict_key]:(current_dict[to_merge_dict[dict_name][dict_key]]|d({}) | combine(to_merge_dict[dict_name], recursive=True))}) %} |
| 117 | +{% elif to_merge_dict[dict_name][dict_key] is undefined %} |
| 118 | +{% set _ = merged_dict.update({dict_name:(current_dict[dict_name]|d({}) | combine(to_merge_dict[dict_name], recursive=True))}) %} |
| 119 | +{% endif %} |
| 120 | +{% endfor %} |
| 121 | +{% elif to_merge_dict is not string and to_merge_dict is not mapping %} |
| 122 | +{% set flattened_dict = lookup("flattened", to_merge_dict) %} |
| 123 | +{% for element in ([ flattened_dict ] if flattened_dict is mapping else flattened_dict) %} |
| 124 | +{% if element[dict_key]|d() %} |
| 125 | +{% set _ = merged_dict.update({element[dict_key]:(current_dict[element[dict_key]]|d({}) | combine(element, recursive=True))}) %} |
| 126 | +{% endif %} |
| 127 | +{% endfor %} |
| 128 | +{% endif %} |
| 129 | +{% endif %} |
| 130 | +{{ merged_dict | to_json }} |
| 131 | +{% endmacro %}{# ]]] #} |
| 132 | + |
| 133 | +{% macro flattened() %}{# [[[ #} |
| 134 | +{# This macro does what the flattened lookup from Ansible fails to do in Jinja templates as of Ansible 2.2. |
| 135 | +## All macro arguments are flattened into one "flat" list. |
| 136 | +## Uses a less known feature of Jinja for using the *args and *kwargs syntax as known from |
| 137 | +## Python. Even if the macro does not declare any arguments, it will happily |
| 138 | +## flatten any non-key-value arguments you provide. |
| 139 | +## Additional key-value arguments can be used to influence the behavior of the macro. |
| 140 | +## The macro uses recursion to flatten nested lists. |
| 141 | +## Ref: https://stackoverflow.com/questions/13944751/args-kwargs-in-jinja2-macros |
| 142 | +## Usage: |
| 143 | +## |
| 144 | +## {{ debops__tpl_macros.flattened(['list1', 55, ["deeplist1", "deeplist elem", True, False, undefined], {'mapping': True}], 'raw1', 22, True, False) }} |
| 145 | +## → ["list1", 55, "deeplist1", "deeplist elem", true, false, "raw1", 22, true, false] |
| 146 | +## |
| 147 | +## {{ debops__tpl_macros.flattened(['list1', 55, ["deeplist1", "deeplist elem", True, False, undefined], {'mapping': True}], 'raw1', 22, True, False, filter_undef=False) }} |
| 148 | +## → ["list1", 55, "deeplist1", "deeplist elem", true, false, null, "raw1", 22, true, false] |
| 149 | +## |
| 150 | +## {{ debops__tpl_macros.flattened(['list1', 55, ["deeplist1", "deeplist elem", True, False, undefined], {'mapping': True}], 'raw1', 22, True, False, filter_mapping=False) }} |
| 151 | +## → ["list1", 55, "deeplist1", "deeplist elem", true, false, {"mapping": true}, "raw1", 22, true, false] |
| 152 | +## |
| 153 | +## Ansible versions tested with: 2.1, 2.2 |
| 154 | +## Jinja versions tested with: 2.8.1 |
| 155 | +#} |
| 156 | +{% set filter_undef = kwargs.filter_undef|d(True) | bool %} |
| 157 | +{% set filter_mapping = kwargs.filter_mapping|d(True) | bool %} |
| 158 | +{# The following options are planned but currently don’t work: #} |
| 159 | +{% set append_mapping_keys = kwargs.append_mapping_keys|d(False) | bool %} |
| 160 | +{% set append_mapping_values = kwargs.append_mapping_values|d(False) | bool %} |
| 161 | +{# |
| 162 | +{{ "filter_undef:" + (filter_undef | string) }} |
| 163 | +{{ "filter_mapping:" + (filter_mapping | string) }} |
| 164 | +{{ "varargs:" + (varargs | string) }} |
| 165 | +#} |
| 166 | +{% set elem_flattened = [] %} |
| 167 | +{% for arg in varargs %} |
| 168 | +{# |
| 169 | +{{ "arg: " + (arg | string) }} |
| 170 | +#} |
| 171 | +{% if filter_undef and (arg is undefined) %} |
| 172 | +{# Filter out. #} |
| 173 | +{% elif append_mapping_keys and (arg is mapping) %} |
| 174 | +{# Does not work as of Jinja 2.8? #} |
| 175 | +{% set _ = elem_flattened.extend(flattened(arg.keys(), filter_undef=filter_undef, filter_mapping=filter_mapping) | from_json) %} |
| 176 | +{% elif append_mapping_values and (arg is mapping) %} |
| 177 | +{# Does not work as of Jinja 2.8? #} |
| 178 | +{% set _ = elem_flattened.extend(flattened(arg.values(), filter_undef=filter_undef, filter_mapping=filter_mapping) | from_json) %} |
| 179 | +{% elif filter_mapping and (arg is mapping) %} |
| 180 | +{# Filter out. #} |
| 181 | +{% elif (arg is undefined) %} |
| 182 | +{% set _ = elem_flattened.append(None) %} |
| 183 | +{% elif (arg is string) or (arg is number) or (arg is sameas True) or (arg is sameas False) or (arg is mapping) %} |
| 184 | +{% set _ = elem_flattened.append(arg) %} |
| 185 | +{% elif (arg is iterable) %} |
| 186 | +{% for element in arg %} |
| 187 | +{% set _ = elem_flattened.extend(flattened(element, filter_undef=filter_undef, filter_mapping=filter_mapping) | from_json) %} |
| 188 | +{% endfor %} |
| 189 | +{% endif %} |
| 190 | +{% endfor %} |
| 191 | +{{ elem_flattened | to_json }} |
| 192 | +{% endmacro %}{# ]]] #} |
0 commit comments