02_manager_ops.ymlΒΆ

---
- name: βž• add config.toml
  stack_host_template:
    src: "config.toml.j2"
    dest: "{{ LISTMONK_CONFIG_HOST }}"
  notify: compose-up

- meta: flush_handlers

- name: πŸ“‚ Include database setup routine for Listmonk
  include_tasks: "01_database.yml"

- name: πŸ”Ž List the Listmonk accounts and their Super Admin role
  ansible.builtin.shell:
    cmd: "{{ LISTMONK_EXEC_DATABASE }} -v ON_ERROR_STOP=1 -tAq"
    stdin: "{{ lookup('file', 'sql/admin_accounts.sql') }}"
  register: listmonk_admin_accounts
  changed_when: false

- name: 🧭 Decide whether the Listmonk administrator is created or realigned
  ansible.builtin.set_fact:
    listmonk_admin_plan: >-
      {{ listmonk_admin_accounts.stdout | from_json
         | platform_admin_plan(LISTMONK_ADMIN_USERNAME, LISTMONK_ADMIN_EMAIL, 'Listmonk', true) }}

- name: πŸ”„ Update administrator email and password login in Listmonk (as superuser)
  shell: |
    {{ LISTMONK_EXEC_DATABASE }} -v ON_ERROR_STOP=1 << 'EOSQL'
    UPDATE users
    SET email = '{{ LISTMONK_ADMIN_EMAIL }}',
        password_login = {{ 'false' if lookup('config',application_id, 'services.sso.enabled') else 'true' }}
    WHERE username = '{{ LISTMONK_ADMIN_USERNAME }}';
    EOSQL
  args:
    executable: /bin/bash

- name: πŸ”‘ Ensure the Listmonk Super Admin exists and accepts the declared password
  ansible.builtin.shell:
    cmd: >-
      {{ LISTMONK_EXEC_DATABASE }} -v ON_ERROR_STOP=1
      -v login={{ LISTMONK_ADMIN_USERNAME | quote }}
      -v email={{ LISTMONK_ADMIN_EMAIL | quote }}
      -v password_login={{ 'false' if LISTMONK_OIDC_ENABLED | bool else 'true' }}
      -v create={{ 'true' if listmonk_admin_plan == 'create' else 'false' }}
    stdin: "{{ lookup('file', 'sql/realign_admin_credential.sql') }}"
  environment:
    LISTMONK_REALIGN_PASSWORD: "{{ LISTMONK_ADMIN_PASSWORD }}"
  register: listmonk_admin_password
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
  changed_when: listmonk_admin_password.stdout is search('\[CHANGED\]')
  failed_when: >-
    listmonk_admin_password.rc != 0
    or listmonk_admin_password.stdout is search('\[FAIL\]')

- name: πŸ“Œ Apply every Listmonk setting that differs, in one psql run
  ansible.builtin.shell:
    cmd: "{{ LISTMONK_EXEC_DATABASE }} -v ON_ERROR_STOP=1"
    stdin: "{{ lookup('template', 'sql/apply_settings.sql.j2') }}"
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
  register: listmonk_settings
  changed_when: "'UPDATE 0' not in listmonk_settings.stdout"
  notify: container-recreate

- name: "🎨 Carry the corporate design in the Listmonk settings"
  ansible.builtin.include_tasks: 03_design.yml

- name: "🚿 Recreate Listmonk so the settings above are loaded"
  ansible.builtin.meta: flush_handlers