fix: make mail account creation idempotent by ignoring 'already exists' error
All checks were successful
CI/CD / syntax-check (push) Successful in 1m39s
CI/CD / deploy (push) Successful in 15m40s

Replace fragile file-content lookup with proper failed_when that accepts
'already exists' exit code 1 as a non-failure. Simpler and works on every run.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jack 2026-03-22 18:13:13 +07:00
parent 836d554e7b
commit aa6b20c463

View file

@ -131,11 +131,6 @@
delay: 10 delay: 10
until: postfix_status.rc == 0 until: postfix_status.rc == 0
- name: Check postfix-accounts.cf exists
ansible.builtin.stat:
path: "{{ tools_root }}/mailserver/config/postfix-accounts.cf"
register: postfix_accounts_file
- name: Create mail accounts - name: Create mail accounts
ansible.builtin.command: > ansible.builtin.command: >
docker exec mailserver setup email add {{ item.address }} {{ item.password }} docker exec mailserver setup email add {{ item.address }} {{ item.password }}
@ -143,9 +138,11 @@
- { address: "noreply@{{ domain_base }}", password: "{{ mailserver_noreply_password }}" } - { address: "noreply@{{ domain_base }}", password: "{{ mailserver_noreply_password }}" }
- { address: "admin@{{ domain_base }}", password: "{{ mailserver_admin_password }}" } - { address: "admin@{{ domain_base }}", password: "{{ mailserver_admin_password }}" }
- { address: "jack@{{ domain_base }}", password: "{{ mailserver_jack_password }}" } - { address: "jack@{{ domain_base }}", password: "{{ mailserver_jack_password }}" }
when: > register: mail_account_result
not postfix_accounts_file.stat.exists or changed_when: mail_account_result.rc == 0
item.address not in (lookup('file', tools_root + '/mailserver/config/postfix-accounts.cf', errors='ignore') | default('', true)) failed_when: >
mail_account_result.rc != 0 and
'already exists' not in mail_account_result.stderr
# ── DKIM ───────────────────────────────────────────────────────────────────── # ── DKIM ─────────────────────────────────────────────────────────────────────
- name: Check if DKIM key exists - name: Check if DKIM key exists