Skip to content

Commit 0ebb0d5

Browse files
authored
Merge pull request saltstack-formulas#43 from EvaSDK/generic-map-handling
Generic map handling
2 parents 1442318 + e10ad0e commit 0ebb0d5

File tree

7 files changed

+80
-92
lines changed

7 files changed

+80
-92
lines changed

pillar.example

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ postfix:
55

66
enable_service: True
77

8-
virtual:
9-
groupaliasexample:
10-
11-
12-
singlealiasexample: '[email protected]'
13-
14-
relay_domains:
15-
example.com: 'OK'
16-
17-
sasl_passwd:
18-
smtp.example.com: 'somepassword'
19-
20-
sender_canonical:
21-
22-
23-
248
postgrey:
259
enabled: True
2610
enable_service: True
@@ -62,6 +46,14 @@ postfix:
6246
smtp_tls_cert_file: /etc/postfix/ssl/example.com-relay-client-cert.crt
6347
smtp_tls_key_file: /etc/postfix/ssl/example.com-relay-client-cert.key
6448

49+
smtp_sasl_password_maps: hash:/etc/postfix/sasl_passwd
50+
51+
sender_canonical_maps: hash:/etc/postfix/sender_canonical
52+
53+
relay_recipient_maps: hash:/etc/postfix/relay_domains
54+
55+
virtual_alias_maps: hash:/etc/postfix/virtual
56+
6557
certificates:
6658
server-cert:
6759
public_cert: |
@@ -88,3 +80,20 @@ postfix:
8880
-----BEGIN RSA PRIVATE KEY-----
8981
(Your Private key)
9082
-----END RSA PRIVATE KEY-----
83+
84+
mapping:
85+
smtp_sasl_password_maps:
86+
- smtp.example.com: myaccount:somepassword
87+
88+
sender_canonical_maps:
89+
90+
91+
92+
relay_recipient_maps:
93+
- example.com: OK
94+
95+
virtual_alias_maps:
96+
- groupaliasexample:
97+
98+
99+
- singlealiasexample: [email protected]

postfix/files/mapping.j2

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Managed by config management
2+
3+
{%- macro format_value(key, value) %}
4+
{#- Some settings, like virtual_alias_maps can take multiple values. Handle this case. -#}
5+
{%- if value is iterable and value is not string -%}
6+
{{ key }} {{ value|join(", ") }}
7+
{%- else -%}
8+
{{ key }} {{ value }}
9+
{%- endif -%}
10+
{%- endmacro %}
11+
12+
{%- if data is mapping %}
13+
{% for key, value in data.iteritems() %}
14+
{{ format_value(key, value) }}
15+
{%- endfor -%}
16+
{%- else %}
17+
{#- Some settings need order, handle OrderedDict #}
18+
{% for item in data %}
19+
{{ format_value(item.keys()[0], item.values()[0]) }}
20+
{%- endfor -%}
21+
{%- endif %}

postfix/init.sls

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,6 @@ postfix:
1212
- watch:
1313
- pkg: postfix
1414
15-
{%- macro postmap_file(filename, mode=644) %}
16-
{%- set file_path = '/etc/postfix/' ~ filename %}
17-
postmap_{{ filename }}:
18-
file.managed:
19-
- name: {{ file_path }}
20-
- source: salt://postfix/{{ filename }}
21-
- user: root
22-
- group: root
23-
- mode: {{ mode }}
24-
- template: jinja
25-
- require:
26-
- pkg: postfix
27-
cmd.wait:
28-
- name: /usr/sbin/postmap {{ file_path }}
29-
- cwd: /
30-
- watch:
31-
- file: {{ file_path }}
32-
{%- endmacro %}
33-
3415
# manage /etc/aliases if data found in pillar
3516
{% if 'aliases' in pillar.get('postfix', '') %}
3617
{{ postfix.aliases_file }}:
@@ -51,22 +32,37 @@ run-newaliases:
5132
- file: {{ postfix.aliases_file }}
5233
{% endif %}
5334
54-
# manage /etc/postfix/virtual if data found in pillar
55-
{% if 'virtual' in pillar.get('postfix', '') %}
56-
{{ postmap_file('virtual') }}
57-
{% endif %}
58-
59-
# manage /etc/postfix/relay_domains if data found in pillar
60-
{% if 'relay_domains' in pillar.get('postfix', '') %}
61-
{{ postmap_file('relay_domains') }}
62-
{% endif %}
63-
64-
# manage /etc/postfix/sasl_passwd if data found in pillar
65-
{% if 'sasl_passwd' in pillar.get('postfix', '') %}
66-
{{ postmap_file('sasl_passwd', 600) }}
67-
{% endif %}
68-
69-
# manage /etc/postfix/sender_canonical if data found in pillar
70-
{% if 'sender_canonical' in pillar.get('postfix', '') %}
71-
{{ postmap_file('sender_canonical') }}
72-
{% endif %}
35+
# manage various mappings
36+
{% for mapping, data in salt['pillar.get']('postfix:mapping', {}).items() %}
37+
{%- set need_postmap = False %}
38+
{%- set file_path = salt['pillar.get']('postfix:config:' ~ mapping) %}
39+
{%- if ':' in file_path %}
40+
{%- set file_path = file_path.split(':')[1] %}
41+
{%- set need_postmap = True %}
42+
{%- endif %}
43+
postfix_{{ mapping }}:
44+
file.managed:
45+
- name: {{ file_path }}
46+
- source: salt://postfix/files/mapping.j2
47+
- user: root
48+
- group: root
49+
{%- if mapping.endswith('_sasl_password_maps') %}
50+
- mode: 600
51+
{%- else %}
52+
- mode: 644
53+
{%- endif %}
54+
- template: jinja
55+
- context:
56+
data: {{ data|json() }}
57+
- require:
58+
- pkg: postfix
59+
{%- if need_postmap %}
60+
cmd.wait:
61+
- name: /usr/sbin/postmap {{ file_path }}
62+
- cwd: /
63+
- watch:
64+
- file: {{ file_path }}
65+
- watch_in:
66+
- service: postfix
67+
{%- endif %}
68+
{% endfor %}

postfix/relay_domains

Lines changed: 0 additions & 7 deletions
This file was deleted.

postfix/sasl_passwd

Lines changed: 0 additions & 7 deletions
This file was deleted.

postfix/sender_canonical

Lines changed: 0 additions & 7 deletions
This file was deleted.

postfix/virtual

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)