From 73b7c720056dc4277051462dfe871e6592182ea0 Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Sun, 9 Jun 2024 15:21:35 +0200 Subject: [PATCH] Added postfix_files feature as a simple means to add extra files/maps to the postfix config --- README.md | 15 +++++++++++++++ defaults/main.yml | 9 +++++++++ tasks/main.yml | 27 +++++++++++++++++++++++++++ tests/tests_set_file.yml | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 tests/tests_set_file.yml diff --git a/README.md b/README.md index 44f7e61..4f833d5 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,21 @@ postfix_conf: relayhost: example.com ``` +### postfix_files + +```yaml +postfix_files: + - name: sasl_passwd + content: example.com user:password + postmap: true + - name: sender_canonical_maps + content: /.+/ info@example.com +``` + +This is a list of files that will be placed in /etc/postfix and that can be converted into Postfix Lookup Tables if needed. + +It's meant as a simple mechanism to configure things such as SASL credentials and other small snippets. + ### postfix_check ```yaml diff --git a/defaults/main.yml b/defaults/main.yml index dabf77a..18b9f58 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -6,6 +6,15 @@ # postfix_conf: {} +# Additional config maps/files, e.g.: +# postfix_files: +# - name: 'sasl_passwd' +# content: 'smtp.example.com user@example.com:myFirstPassword +# postmap: true +# - name: 'sender_canonical_maps' +# content: '/.+/ user@example.com' +postfix_files: [] + # Whether to run 'postfix check' before it's started postfix_check: true diff --git a/tasks/main.yml b/tasks/main.yml index 27c5130..f5efddb 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -102,6 +102,33 @@ {% endif %} {% endfor %} +- name: Configure additional files + copy: + content: "{{ file['content'] }}" + dest: /etc/postfix/{{ file['name'] }} + owner: root + group: root + mode: '0640' + loop: "{{ postfix_files }}" + register: __postfix_postmap_files + no_log: true + loop_control: + loop_var: file + notify: + - Check postfix + - Restart postfix + +- name: Postmap files + command: postmap {{ result["dest"] | quote }} + when: + - result["changed"] + - result["file"]["postmap"] | d(false) + no_log: true + changed_when: true + loop: "{{ __postfix_postmap_files['results'] }}" + loop_control: + loop_var: result + - name: Apply changes when: __postfix_has_config_changed | d("") is search("True") block: diff --git a/tests/tests_set_file.yml b/tests/tests_set_file.yml new file mode 100644 index 0000000..5a76250 --- /dev/null +++ b/tests/tests_set_file.yml @@ -0,0 +1,33 @@ +--- +- name: Create a postmapped file + hosts: all + + vars: + postfix_files: + - name: test + content: test + postmap: true + + tasks: + - name: Run the role with test postmap file + include_role: + name: linux-system-roles.postfix + public: true + + - name: Check if postmap file exists + stat: + path: /etc/postfix/test.db + register: test_file + changed_when: false + + - name: Assert file is present + assert: + that: test_file.stat.exists + + - name: Clean up test files + file: + path: "{{ item }}" + state: absent + loop: + - /etc/postfix/test + - /etc/postfix/test.db