1
0
Fork 0
mirror of https://github.com/DO1JLR/ansible_playbook_servers.git synced 2024-09-14 19:53:56 +02:00
ansible_playbook_servers/roles/mailserver_preperation/templates/mysqlconfig.sql.j2

99 lines
2.3 KiB
Django/Jinja

{#-
This is the mysql command template for updating all existing users, domains and alias config.
The following yml is an example confiuration...
############
---
mailserver__domains:
- fqdn: example.com
mailserver__accounts:
- username: 'alice'
domain: 'example.com'
password_hash: # generate with $(doveadm pw -s SHA512-CRYPT)
# or $ python -c 'import crypt,getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))'
quota: '0'
enabled: true
sendonly: false
mailserver__alias:
- src_username: 'bob' # null for catchall
src_domain: 'example.com'
dest_username: 'alice'
dest_domain: 'example.com'
enabled: true
###############
OK, first we select the wanted database!
-#}
use vmail;
{#
Next we will define all existing domains.
We use INSERT IGNORE to insert the domains and ignore duplicates.
To delete a domain you have to do it manually using mysql commands or sth. like that!
-#}
INSERT IGNORE INTO domains
(domain)
values
{% for domain in mailserver__domains %}
('{{ domain["fqdn"] }}')
{%- if loop.last -%}
;
{%- else -%}
,
{%- endif %}
{% endfor %}
{#-
Now we will define the users and passwords.
We will use REPLACE to replace updated entrys or add new one.
#}
REPLACE into accounts
(username, domain, password, quota, enabled, sendonly)
values
{% for account in mailserver__accounts %}
('{{ account["username"] }}', '{{ account["domain"] }}', '{{ account["password_hash"] }}', {{ account["quota"]|default(0) | int }}, {{ account["enabled"] | bool | ternary('true', 'false') }} , {{ account["sendonly"] | bool | ternary('true', 'false' )}})
{%- if loop.last -%}
;
{%- else -%}
,
{%- endif %}
{% endfor %}
{#
Last we will define some aliases and catchall...
#}
REPLACE into aliases
(source_username, source_domain, destination_username, destination_domain, enabled)
values
{% for alias in mailserver__alias %}
(
{%- if alias["src_username"] == 'null' -%}
null
{%- else -%}
'{{ alias["src_username"] }}'
{%- endif -%}
, '{{ alias["src_domain"] }}', '{{ alias["dest_username"] }}', '{{ alias["dest_domain"] }}', {{ alias["enabled"] | bool | ternary('true', 'false') }})
{%- if loop.last -%}
;
{%- else -%}
,
{%- endif %}
{% endfor %}