๐Ÿ“— Ansible playbooks and roles for building an idempotent, interconnected and scalable infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
ansible-playbooks/roles/asterisk/templates/_macros.j2

85 lines
3.9 KiB

{% macro config_template(config_name, asterisk_cfg) -%}
{% set ns = namespace(objects=false) -%}
{% if config_name is string and asterisk_cfg[config_name] is mapping -%}
{% for section in (asterisk_cfg[config_name] | dict2items) -%}
{% if section.value is mapping -%}
{% set template_parts = [] -%}
{% if (section.value['__template__'] is boolean) and (section.value['__template__'] == true) -%}
{% set template_parts = template_parts + ['!'] -%}
{% endif -%}
{% if section.value['__template_from__'] is string -%}
{% set template_parts = template_parts + [section.value['__template_from__']] -%}
{% elif section.value['__template_from__'] | type_debug == 'list' -%}
{% set template_parts = template_parts + section.value['__template_from__'] -%}
{% endif -%}
{% if section.value['__comment__'] is string -%}
; {{ section.value['__comment__'] }}
{% endif -%}
{% if template_parts | length == 0 -%}
[{{ section.key }}]
{% else -%}
[{{ section.key }}]({{ template_parts | join(',') }})
{% endif -%}
{% set ns.objects = (section.value['__inner_objects__'] | d(asterisk_cfg[config_name]['__inner_objects__'] | d(false))) -%}
{% for option in (section.value | dict2items) -%}
{% if not option.key.startswith('__') and not option.key.endswith('__') -%}
{% if option.value | type_debug == 'list' -%}
{% if option.value | length == 0 -%}
{{ option.key }} =>
{% else -%}
{% for option_element in option.value -%}
{% set option_value = 'yes' if (option_element is boolean and option_element == true) else ('no' if (option_element is boolean and option_element == false) else option_element ) -%}
{{ option.key }} => {{ option_value }}
{% endfor -%}
{% endif -%}
{% elif option.value is mapping -%}
{% set option_is_object = option.value['__inner_objects__'] | d(ns.objects) -%}
{% if option.value['__comment__'] is string -%}
; {{ option.value['__comment__'] }}
{% endif -%}
{% if option.value['__include_before__'] is string -%}
#include {{ option.value['__include_before__'] }}
{% endif -%}
{% if option.value['__try_include_before__'] is string -%}
#tryinclude {{ option.value['__try_include_before__'] }}
{% endif -%}
{% set option_value = 'yes' if (option.value['__value__'] is boolean and option.value['__value__'] == true) else ('no' if (option.value['__value__'] is boolean and option.value['__value__'] == false) else option.value['__value__'] ) -%}
{{ option.key }} {{ '=>' if option_is_object else '=' }} {{ option_value }}
{% if option.value['__include_after__'] is string -%}
#include {{ option.value['__include_after__'] }}
{% endif -%}
{% if option.value['__try_include_after__'] is string -%}
#tryinclude {{ option.value['__try_include_after__'] }}
{% endif -%}
{% else -%}
{% set option_value = 'yes' if (option.value is boolean and option.value == true) else ('no' if (option.value is boolean and option.value == false) else option.value ) -%}
{{ option.key }} {{ '=>' if ns.objects else '=' }} {{ option_value }}
{% endif -%}
{% endif -%}
{% endfor -%}
{% if not loop.last %}
{% endif -%}
{% elif (section.key == '__include__') and (section.value is string) -%}
#include {{ section.value }}
{% elif (section.key == '__try_include__') and (section.value is string) -%}
#tryinclude {{ section.value }}
{% endif -%}
{% endfor -%}
{% endif -%}
{% endmacro -%}