๐Ÿ“— 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.

80 lines
2.7 KiB

2 years ago
{% if mail_server.domains | d([]) | length > 0 -%}
INSERT INTO mail_domains (domain) VALUES
{% for domain in mail_server.domains -%}
('{{ domain }}'){% if not loop.last -%},{%- endif %}
{% endfor -%}
ON CONFLICT DO NOTHING;
{% endif -%}
{% if mail_server.users | d([]) | length > 0 -%}
INSERT INTO mail_users (enabled, username, domain_id, password_md5, password_sha1,
password_sha256, password_plaintext, quota_mb, no_reply) VALUES
{% for user in mail_server.users -%}
(true,
'{{ user.name }}',
(SELECT id FROM mail_domains WHERE domain = '{{ user.domain }}'),
'{{ user.cram_md5 | d(user.password | hash('md5')) }}',
'{{ user.password | hash('sha1') }}',
'{{ user.password | hash('sha256') }}',
'{{ user.password }}',
{{ user.quota_mb | d(0) }},
{{ 'true' if (user.no_reply | d(false) == true) else 'false' }}
){% if not loop.last -%},{%- endif %}
{% endfor -%}
ON CONFLICT (username, domain_id) DO UPDATE SET
password_md5 = EXCLUDED.password_md5,
password_sha1 = EXCLUDED.password_sha1,
password_sha256 = EXCLUDED.password_sha256,
password_plaintext = EXCLUDED.password_plaintext,
quota_mb = EXCLUDED.quota_mb,
no_reply = EXCLUDED.no_reply;
{% endif -%}
{% if mail_server.aliases | d([]) | length > 0 -%}
INSERT INTO mail_aliases (enabled, alias_username, alias_domain_id, email_username, email_domain_id) VALUES
{% for alias in mail_server.aliases -%}
(true,
'{{ alias.source }}',
(SELECT id FROM mail_domains WHERE domain = '{{ alias.source_domain }}'),
'{{ alias.target }}',
(SELECT id FROM mail_domains WHERE domain = '{{ alias.target_domain }}')
){% if not loop.last -%},{%- endif %}
{% endfor -%}
ON CONFLICT DO NOTHING;
{% endif -%}
{% if mail_server.forwards | d([]) | length > 0 -%}
INSERT INTO mail_forwards (enabled, source, destination) VALUES
{% for forward in mail_server.forwards -%}
(true,
'{{ forward.source }}',
'{{ forward.destination }}'
){% if not loop.last -%},{%- endif %}
{% endfor -%}
ON CONFLICT DO NOTHING;
{% endif -%}
{% if mail_server.global_shares | d([]) | length > 0 -%}
INSERT INTO mail_anyone_shares (from_user) VALUES
{% for share in mail_server.global_shares -%}
('{{ share }}'
){% if not loop.last -%},{%- endif %}
{% endfor -%}
ON CONFLICT DO NOTHING;
{% endif -%}
{% if mail_server.user_shares | d([]) | length > 0 -%}
INSERT INTO mail_user_shares (from_user, to_user) VALUES
{% for share in mail_server.user_shares -%}
('{{ share.from }}',
'{{ share.to }}'
){% if not loop.last -%},{%- endif %}
{% endfor -%}
ON CONFLICT DO NOTHING;
{% endif -%}