{#- which table are we setting up -#} use vmail; {#- setup mailserver domain #} insert into domains (domain) values ('{{ mailserver_domain }}'); {#- create all mail users, domains and enter password hashes... CREATE TABLE `accounts` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `username` varchar(64) NOT NULL, `domain` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `quota` int unsigned DEFAULT '0', `enabled` boolean DEFAULT '0', `sendonly` boolean DEFAULT '0', PRIMARY KEY (id), UNIQUE KEY (`username`, `domain`), FOREIGN KEY (`domain`) REFERENCES `domains` (`domain`) ); 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 #} {% for account in mailserver__accounts %} {# INSERT into accounts (username, domain, password, quota, enabled, sendonly) values ('{{ account["username"] }}', '{{ account["domain"] }}', '{{ account["password_hash"] }}', {{ account["quota"]|default(0) | int }}, {{ account["enabled"] | bool |ternary('true', 'false') }} , {{ account["sendonly"] | bool |ternary('true', 'false' }}); #} REPLACE into accounts (username, domain, password, quota, enabled, sendonly) values ('{{ account["username"] }}', '{{ account["domain"] }}', '{{ account["password_hash"] }}', {{ account["quota"]|default(0) | int }}, {{ account["enabled"] | bool | ternary('true', 'false') }} , {{ account["sendonly"] | bool | ternary('true', 'false' )}}); {% endfor %} {#- create all mail aliases and stuff like that... #} {% for alias in mailserver__alias %} {# INSERT into aliases (source_username, source_domain, destination_username, destination_domain, enabled) values ('{{ alias["src_username"] }}', '{{ alias["src_domain"] }}', '{{ alias["dest_username"] }}', '{{ alias["dest_domain"] }}', {{ alias["enabled"] | bool | ternary('true', 'false') }}); #} REPLACE into aliases (source_username, source_domain, destination_username, destination_domain, enabled) values ( {%- 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') }}); {% endfor %}