---
- name: Test setting previous=replaced with postfix_conf
  hosts: all
  tasks:
    - name: Run the role with previous=replaced only
      include_role:
        name: linux-system-roles.postfix
        public: true
      vars:
        postfix_manage_firewall: true
        postfix_manage_selinux: false
        postfix_conf:
          previous: replaced

    - name: Check firewall and selinux status
      include_tasks: check_firewall_selinux.yml

    - name: Run the role to configure relay_domains and relayhost
      include_role:
        name: linux-system-roles.postfix
        public: true
      vars:
        postfix_manage_firewall: true
        postfix_manage_selinux: true
        postfix_conf:
          relay_domains: London
          relayhost: example.com

    - name: Flush handlers
      meta: flush_handlers

    - name: Check firewall and selinux status again
      include_tasks: check_firewall_selinux.yml

    - name: Check header for ansible_managed, fingerprint
      include_tasks: tasks/check_header.yml
      vars:
        __file: /etc/postfix/main.cf
        __fingerprint: "system_role:postfix"

    - name: Reset all settings and configure relay_domains
      include_role:
        name: linux-system-roles.postfix
      vars:
        postfix_conf:
          previous: replaced
          relay_domains: Liverpool city

    - name: Flush handlers again
      meta: flush_handlers

    - name: Print postfix settings that have explicit value set
      command: postconf -n
      register: __postfix_explicit_config
      changed_when: false

    - name: Verify that relayhost is unset and relay_domains is set
      assert:
        that:
          - __postfix_explicit_config.stdout is search(
            "relay_domains = Liverpool city")
          - (__postfix_explicit_config.stdout | regex_search('^relayhost\\s*=\\s*$', multiline=True)) or
            __postfix_explicit_config.stdout is not search("relayhost")
