From aa6b20c463975671f4bf3139d18b415491d57fb5 Mon Sep 17 00:00:00 2001 From: jack Date: Sun, 22 Mar 2026 18:13:13 +0700 Subject: [PATCH] fix: make mail account creation idempotent by ignoring 'already exists' error 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 --- roles/tools/tasks/main.yml | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/roles/tools/tasks/main.yml b/roles/tools/tasks/main.yml index cf3f24d..806480e 100644 --- a/roles/tools/tasks/main.yml +++ b/roles/tools/tasks/main.yml @@ -131,11 +131,6 @@ delay: 10 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 ansible.builtin.command: > docker exec mailserver setup email add {{ item.address }} {{ item.password }} @@ -143,9 +138,11 @@ - { address: "noreply@{{ domain_base }}", password: "{{ mailserver_noreply_password }}" } - { address: "admin@{{ domain_base }}", password: "{{ mailserver_admin_password }}" } - { address: "jack@{{ domain_base }}", password: "{{ mailserver_jack_password }}" } - when: > - not postfix_accounts_file.stat.exists or - item.address not in (lookup('file', tools_root + '/mailserver/config/postfix-accounts.cf', errors='ignore') | default('', true)) + register: mail_account_result + changed_when: mail_account_result.rc == 0 + failed_when: > + mail_account_result.rc != 0 and + 'already exists' not in mail_account_result.stderr # ── DKIM ───────────────────────────────────────────────────────────────────── - name: Check if DKIM key exists