Skip to content

Instantly share code, notes, and snippets.

@drybjed
Created June 28, 2014 15:35
Show Gist options
  • Save drybjed/18704fbd83b8963baac9 to your computer and use it in GitHub Desktop.
Save drybjed/18704fbd83b8963baac9 to your computer and use it in GitHub Desktop.
{#
Abusing Jinja2 templates 101: Postfix local facts
This template manages contents of /etc/ansible/facts.d/postfix.fact
and allows to configure Postfix ba multiple separate roles using
dependency variables. Configuration will be stored and preserved
idempotently between ansible-playbook runs.
Recognized variables:
- postfix_dependency_lists: hash variable which defines lists of values
to set for specified Postfix configuration option
- postfix_dependency_maincf: list of options to enable in Postfix when
specified capabilities are enabled
#}
{% set postfix_tpl_local_capabilities_result = [] %}
{% for capability in postfix %}
{% if postfix_tpl_local_capabilities_result.append(capability) %}{% endif %}
{% endfor %}
{% set postfix_tpl_local_lists_result = {} %}
{% set postfix_tpl_seen_local_lists = [] %}
{% if (ansible_local is defined and ansible_local) and
(ansible_local.postfix is defined and ansible_local.postfix) and
(ansible_local.postfix.lists is defined and ansible_local.postfix.lists) %}
{% for fact_key, fact_values in ansible_local.postfix.lists.iteritems() %}
{% set postfix_tpl_local_list = [] %}
{% for element in fact_values %}
{% if postfix_tpl_local_list.append(element) %}{% endif %}
{% endfor %}
{% if postfix_dependent_lists is defined and postfix_dependent_lists %}
{% if fact_key in postfix_dependent_lists.keys() %}
{% for new_element in postfix_dependent_lists[fact_key] %}
{% if new_element is mapping %}
{% if (new_element.capability is defined and new_element.capability) and
(new_element.capability in postfix) %}
{% if new_element.list is defined and new_element.list %}
{% for x in new_element.list %}
{% if x not in postfix_tpl_local_list %}
{% if postfix_tpl_local_list.append(x) %}{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% elif (new_element.no_capability is defined and new_element.no_capability) and
(new_element.no_capability not in postfix) %}
{% if new_element.list is defined and new_element.list %}
{% for x in new_element.list %}
{% if x not in postfix_tpl_local_list %}
{% if postfix_tpl_local_list.append(x) %}{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% else %}
{% if new_element not in postfix_tpl_local_list %}
{% if postfix_tpl_local_list.append(new_element) %}{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% if postfix_tpl_seen_local_lists.append(fact_key) %}{% endif %}
{% endif %}
{% endif %}
{% if postfix_tpl_local_lists_result.update({ fact_key: postfix_tpl_local_list }) %}{% endif %}
{% endfor %}
{% endif %}
{% if (postfix_dependent_lists is defined and postfix_dependent_lists) and
((postfix_dependent_lists.keys() | length) > (postfix_tpl_seen_local_lists | length)) %}
{% for list_key, list_values in postfix_dependent_lists.iteritems() %}
{% if list_key not in postfix_tpl_seen_local_lists %}
{% set postfix_tpl_local_list = [] %}
{% for element in list_values %}
{% if element is mapping %}
{% if (element.capability is defined and element.capability) and
(element.capability in postfix) %}
{% if element.list is defined and element.list %}
{% for x in element.list %}
{% if x not in postfix_tpl_local_list %}
{% if postfix_tpl_local_list.append(x) %}{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% elif (element.no_capability is defined and element.no_capability) and
(element.no_capability not in postfix) %}
{% if element.list is defined and element.list %}
{% for x in element.list %}
{% if x not in postfix_tpl_local_list %}
{% if postfix_tpl_local_list.append(x) %}{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% else %}
{% if element not in postfix_tpl_local_list %}
{% if postfix_tpl_local_list.append(element) %}{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% if postfix_tpl_local_lists_result.update({ list_key: postfix_tpl_local_list }) %}{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% set postfix_tpl_local_maincf_result = {} %}
{% set postfix_tpl_seen_local_maincf = [] %}
{% if (ansible_local is defined and ansible_local) and
(ansible_local.postfix is defined and ansible_local.postfix) and
(ansible_local.postfix.maincf is defined and ansible_local.postfix.maincf) %}
{% for fact_key, fact_value in ansible_local.postfix.maincf.iteritems() %}
{% set postfix_tpl_local_value = fact_value %}
{% for entry in postfix_dependent_maincf %}
{% if (entry.param is defined and entry.param) and (entry.param == fact_key) %}
{% if (entry.capability is defined and entry.capability) and
(entry.capability in postfix) %}
{% if entry.value is defined and entry.value %}
{% set postfix_tpl_local_value = entry.value %}
{% endif %}
{% elif (entry.no_capability is defined and entry.no_capability) and
(entry.no_capability not in postfix) %}
{% if entry.value is defined and entry.value %}
{% set postfix_tpl_local_value = entry.value %}
{% endif %}
{% else %}
{% if entry.value is defined and entry.value %}
{% set postfix_tpl_local_value = entry.value %}
{% endif %}
{% endif %}
{% if postfix_tpl_seen_local_maincf.append(fact_key) %}{% endif %}
{% endif %}
{% endfor %}
{% if postfix_tpl_local_maincf_result.update({fact_key: postfix_tpl_local_value}) %}{% endif %}
{% endfor %}
{% endif %}
{% if (postfix_dependent_maincf is defined and postfix_dependent_maincf) and
((postfix_dependent_maincf | length) > (postfix_tpl_seen_local_maincf | length)) %}
{% for entry in postfix_dependent_maincf %}
{% if (entry.param is defined and entry.param) and (entry.param not in postfix_tpl_seen_local_maincf) %}
{% set postfix_tpl_local_value = '' %}
{% if (entry.capability is defined and entry.capability) and
(entry.capability in postfix) %}
{% if entry.value is defined and entry.value %}
{% set postfix_tpl_local_value = entry.value %}
{% endif %}
{% elif (entry.no_capability is defined and entry.no_capability) and
(entry.no_capability not in postfix) %}
{% if entry.value is defined and entry.value %}
{% set postfix_tpl_local_value = entry.value %}
{% endif %}
{% else %}
{% if entry.value is defined and entry.value %}
{% set postfix_tpl_local_value = entry.value %}
{% endif %}
{% endif %}
{% if postfix_tpl_local_maincf_result.update({entry.param: postfix_tpl_local_value}) %}{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{
"capabilities": {{ postfix_tpl_local_capabilities_result | to_nice_json }},
"lists": {{ postfix_tpl_local_lists_result | to_nice_json }},
"maincf": {{ postfix_tpl_local_maincf_result | to_nice_json }}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment