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

100 lines
2.3 KiB
Text
Raw Normal View History

2021-01-09 01:12:27 +01:00
{#-
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
2021-01-08 23:47:14 +01:00
mailserver__accounts:
- username: 'alice'
domain: 'example.com'
2021-01-09 01:12:27 +01:00
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
2021-01-08 23:47:14 +01:00
mailserver__alias:
2021-01-09 01:12:27 +01:00
- src_username: 'bob' # null for catchall
2021-01-08 23:47:14 +01:00
src_domain: 'example.com'
dest_username: 'alice'
dest_domain: 'example.com'
enabled: true
###############
OK, first we select the wanted database!
-#}
use vmail;
2021-01-08 23:47:14 +01:00
{#
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.
2021-01-08 23:47:14 +01:00
#}
2021-01-08 23:47:14 +01:00
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 %}
2021-01-08 23:47:14 +01:00
{#
Last we will define some aliases and catchall...
2021-01-08 23:47:14 +01:00
#}
2021-01-08 23:47:14 +01:00
REPLACE into aliases
(source_username, source_domain, destination_username, destination_domain, enabled)
values
{% for alias in mailserver__alias %}
2021-01-09 01:12:27 +01:00
(
2021-01-09 01:41:21 +01:00
{%- if alias["src_username"] == 'null' -%}
2021-01-09 01:12:27 +01:00
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 %}
2021-01-08 23:47:14 +01:00
{% endfor %}