Skip to content

Commit 0cc9254

Browse files
committed
fix: configure postfix to listen only to IPv4 if IPv6 is disabled
Cause: The default postfix configuration uses `inet_interfaces = localhost` which tells postfix to listen on all interfaces resolving to `localhost` including both IPv4 and IPv6 interfaces. Consequence: If IPv6 is disabled on the host, postfix and command line tools such as postconf will issue an error, and the role will fail. Fix: Using `postconf -h default_database_type`, see if postconf fails with the error message that indicates IPv6 is disabled. If so, then set `inet_protocols = ipv4` so that postfix will only use the IPv4 interface. Result: The postfix role works when IPv6 is disabled. Adds a new test tests_disable_ipv6.yml to check for this. Signed-off-by: Rich Megginson <[email protected]>
1 parent 8fa3a47 commit 0cc9254

File tree

3 files changed

+120
-55
lines changed

3 files changed

+120
-55
lines changed

tasks/main.yml

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,30 @@
7070
use: "{{ (__postfix_is_ostree | d(false)) |
7171
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
7272

73-
- name: Get default database type from postconf
73+
- name: Check for IPv6 enabled
74+
vars:
75+
ipv6_err: >-
76+
postconf: fatal: parameter inet_interfaces: no local interface found for ::1
77+
block:
78+
- name: Get default database type from postconf - 1
79+
command: postconf -h default_database_type
80+
changed_when: false
81+
register: __postfix_register_dbtype
82+
failed_when:
83+
- __postfix_register_dbtype is failed
84+
- __postfix_register_dbtype.stderr is not search(ipv6_err)
85+
86+
- name: Configure postfix for no ipv6
87+
include_tasks: manage_config.yml
88+
vars:
89+
__postfix_conf:
90+
inet_protocols: ipv4
91+
__postfix_has_config_changed: True itemstr inet_protocols
92+
when:
93+
- __postfix_register_dbtype.rc == 1
94+
- __postfix_register_dbtype.stderr is search(ipv6_err)
95+
96+
- name: Get default database type from postconf - 2
7497
command: postconf -h default_database_type
7598
changed_when: false
7699
register: __postfix_register_dbtype
@@ -139,57 +162,6 @@
139162
loop_var: result
140163

141164
- name: Apply changes
142-
when: __postfix_has_config_changed | d("") is search("True")
143-
block:
144-
- name: Gather facts for ansible_date_time
145-
setup:
146-
filter:
147-
- ansible_date_time
148-
when: postfix_backup_multiple | bool
149-
150-
- name: Backup configuration
151-
copy:
152-
remote_src: true
153-
src: /etc/postfix/main.cf
154-
dest: /etc/postfix/main.cf.{{ postfix_backup_multiple |
155-
ternary(ansible_date_time.iso8601, "backup") }}
156-
mode: "0644"
157-
when: postfix_backup or postfix_backup_multiple
158-
159-
- name: Ensure Last modified header is absent
160-
lineinfile:
161-
path: /etc/postfix/main.cf
162-
regexp: '# Last modified:'
163-
state: absent
164-
165-
# Previously, the role inserted a plain-text comment at the top of main.cf.
166-
# This task removes this outdated header for compatibility.
167-
- name: Ensure the outdated ansible managed header is absent
168-
lineinfile:
169-
path: /etc/postfix/main.cf
170-
regexp: "# This file is managed by [aA]nsible"
171-
state: absent
172-
173-
- name: Ensure ansible_managed header in configuration file
174-
vars:
175-
__lsr_ansible_managed: "{{
176-
lookup('template', 'get_ansible_managed.j2') }}"
177-
blockinfile:
178-
path: /etc/postfix/main.cf
179-
block: "{{ __lsr_ansible_managed }}"
180-
insertbefore: BOF
181-
182-
- name: Configure Postfix
183-
command: postconf -e {{ item.key | quote }}={{ item.value | quote }}
184-
notify:
185-
- Check postfix
186-
- Restart postfix
187-
with_dict: "{{ postfix_conf }}"
188-
when:
189-
- item.key not in ['previous']
190-
- __postfix_has_config_changed
191-
| d("") is search("True itemstr " ~ item.key)
192-
changed_when:
193-
- item.key not in ['previous']
194-
- __postfix_has_config_changed
195-
| d("") is search("True itemstr " ~ item.key)
165+
include_tasks: manage_config.yml
166+
vars:
167+
__postfix_conf: "{{ postfix_conf }}"

tasks/manage_config.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
# input __postfix_conf - dict of key/value pairs to apply
3+
- name: Apply changes
4+
when: __postfix_has_config_changed | d("") is search("True")
5+
block:
6+
- name: Gather facts for ansible_date_time
7+
setup:
8+
filter: ansible_date_time
9+
when: postfix_backup_multiple | bool
10+
11+
- name: Backup configuration
12+
copy:
13+
remote_src: true
14+
src: /etc/postfix/main.cf
15+
dest: /etc/postfix/main.cf.{{ postfix_backup_multiple |
16+
ternary(ansible_date_time.iso8601, "backup") }}
17+
mode: "0644"
18+
when: postfix_backup or postfix_backup_multiple
19+
20+
- name: Ensure Last modified header is absent
21+
lineinfile:
22+
path: /etc/postfix/main.cf
23+
regexp: '# Last modified:'
24+
state: absent
25+
26+
# Previously, the role inserted a plain-text comment at the top of main.cf.
27+
# This task removes this outdated header for compatibility.
28+
- name: Ensure the outdated ansible managed header is absent
29+
lineinfile:
30+
path: /etc/postfix/main.cf
31+
regexp: "# This file is managed by [aA]nsible"
32+
state: absent
33+
34+
- name: Ensure ansible_managed header in configuration file
35+
vars:
36+
__lsr_ansible_managed: "{{
37+
lookup('template', 'get_ansible_managed.j2') }}"
38+
blockinfile:
39+
path: /etc/postfix/main.cf
40+
block: "{{ __lsr_ansible_managed }}"
41+
insertbefore: BOF
42+
43+
- name: Configure Postfix
44+
command: postconf -e {{ item.key | quote }}={{ item.value | quote }}
45+
notify:
46+
- Check postfix
47+
- Restart postfix
48+
with_dict: "{{ __postfix_conf }}"
49+
when:
50+
- item.key not in ['previous']
51+
- __postfix_has_config_changed
52+
| d("") is search("True itemstr " ~ item.key)
53+
changed_when:
54+
- item.key not in ['previous']
55+
- __postfix_has_config_changed
56+
| d("") is search("True itemstr " ~ item.key)

tests/tests_disable_ipv6.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
- name: Ensure that the rule runs with IPv6 disabled
3+
hosts: all
4+
gather_facts: false
5+
tasks:
6+
- name: Disable IPv6
7+
include_role:
8+
name: fedora.linux_system_roles.bootloader
9+
vars:
10+
bootloader_settings:
11+
- kernel: ALL
12+
options:
13+
- name: ipv6.disable_ipv6
14+
value: "1"
15+
bootloader_reboot_ok: true
16+
17+
- name: Run handlers
18+
meta: flush_handlers
19+
20+
- name: Run the postfix role
21+
include_role:
22+
name: linux-system-roles.postfix
23+
public: true
24+
25+
- name: Enable IPv6
26+
include_role:
27+
name: fedora.linux_system_roles.bootloader
28+
vars:
29+
bootloader_settings:
30+
- kernel: ALL
31+
options:
32+
- name: ipv6.disable_ipv6
33+
state: absent
34+
bootloader_reboot_ok: true
35+
36+
- name: Run handlers again
37+
meta: flush_handlers

0 commit comments

Comments
 (0)