# SPDX-License-Identifier: MIT
---
- name: Create a corosync.conf tempfile
  tempfile:
    state: file
    suffix: _ha_cluster_corosync_conf
  register: __ha_cluster_tempfile_corosync_conf
  run_once: true  # noqa: run_once[task]
  # We always need to create corosync.conf file to see whether it's the same as
  # what is already present on the cluster nodes. However, we don't want to
  # report it as a change since the only thing which matters is copying the
  # resulting corosync.conf to cluster nodes.
  check_mode: false
  changed_when: not ansible_check_mode

- name: Create a corosync.conf file content
  command:
    cmd: >
      pcs cluster setup
      --corosync_conf {{ __ha_cluster_tempfile_corosync_conf.path | quote }}
      --overwrite
      {% if "cluster.config.uuid" in __ha_cluster_pcs_capabilities %}
        --no-cluster-uuid
      {% endif %}
      --
      {{ ha_cluster_cluster_name | quote }}
      {% for node in ansible_play_hosts %}
        {{ hostvars[node].__ha_cluster_node_name | quote }}
        {%
          for addr in
            hostvars[node].__ha_cluster_local_node.corosync_addresses | d([])
        %}
          addr={{ addr | quote }}
        {% endfor %}
      {% endfor %}

      {% if ha_cluster_transport | d({}) %}
        transport {{ ha_cluster_transport.type | d('knet') | quote }}
        {% for option in ha_cluster_transport.options | d([]) %}
          {{ option.name | quote }}={{ option.value | quote }}
        {% endfor %}
        {% for link in ha_cluster_transport.links | d([]) %}
          link
          {% for option in link %}
            {{ option.name | quote }}={{ option.value | quote }}
          {% endfor %}
        {% endfor %}
        {% if ha_cluster_transport.compression | d([]) %}
          compression
          {% for option in ha_cluster_transport.compression %}
            {{ option.name | quote }}={{ option.value | quote }}
          {% endfor %}
        {% endif %}
        {% if ha_cluster_transport.crypto | d([]) %}
          crypto
          {% for option in ha_cluster_transport.crypto %}
            {{ option.name | quote }}={{ option.value | quote }}
          {% endfor %}
        {% endif %}
      {% endif %}

      {% if ha_cluster_totem.options | d([]) %}
        totem
        {% for option in ha_cluster_totem.options %}
          {{ option.name | quote }}={{ option.value | quote }}
        {% endfor %}
      {% endif %}

      {%
        if __ha_cluster_sbd_needs_atb | d(false)
          or ha_cluster_quorum.options | d([])
      %}
        quorum
        {% for option in ha_cluster_quorum.options | d([]) %}
          {% if
            not __ha_cluster_sbd_needs_atb | d(false)
            or option.name != 'auto_tie_breaker'
          %}
            {{ option.name | quote }}={{ option.value | quote }}
          {% endif %}
        {% endfor %}
        {% if __ha_cluster_sbd_needs_atb | d(false) %}
          auto_tie_breaker=1
        {% endif %}
      {% endif %}
  run_once: true  # noqa: run_once[task]
  # We always need to create corosync.conf file to see whether it's the same as
  # what is already present on the cluster nodes. However, we don't want to
  # report it as a change since the only thing which matters is copying the
  # resulting corosync.conf to cluster nodes.
  check_mode: false
  changed_when: not ansible_check_mode

- name: Add qdevice configuration to corosync.conf
  command:
    cmd: >
      pcs quorum device add
      --corosync_conf {{ __ha_cluster_tempfile_corosync_conf.path | quote }}
      --
      {% for option in ha_cluster_quorum.device.generic_options | d([]) %}
        {{ option.name | quote }}={{ option.value | quote }}
      {% endfor %}
      model
      {{ ha_cluster_quorum.device.model | d() | quote }}
      {% for option in ha_cluster_quorum.device.model_options | d([]) %}
        {% if option.name != 'pcs-address' %}
          {{ option.name | quote }}={{ option.value | quote }}
        {% endif %}
      {% endfor %}
      {% if ha_cluster_quorum.device.heuristics_options | d([]) %}
        heuristics
        {% for option in ha_cluster_quorum.device.heuristics_options %}
          {{ option.name | quote }}={{ option.value | quote }}
        {% endfor %}
      {% endif %}
  run_once: true  # noqa: run_once[task]
  when:
    - __ha_cluster_qdevice_in_use
  # We always need to create corosync.conf file to see whether it's the same as
  # what is already present on the cluster nodes. However, we don't want to
  # report it as a change since the only thing which matters is copying the
  # resulting corosync.conf to cluster nodes.
  check_mode: false
  changed_when: not ansible_check_mode

- name: Fetch created corosync.conf file
  slurp:
    src: "{{ __ha_cluster_tempfile_corosync_conf.path }}"
  register: __ha_cluster_data_corosync_conf
  run_once: true  # noqa: run_once[task]
  when: __ha_cluster_tempfile_corosync_conf.path is defined

- name: Distribute corosync.conf file
  copy:
    content: "{{ __ha_cluster_data_corosync_conf['content'] | b64decode }}"
    dest: /etc/corosync/corosync.conf
    owner: root
    group: root
    mode: "0644"
  register: __ha_cluster_distribute_corosync_conf
  when: __ha_cluster_data_corosync_conf is defined

- name: Remove a corosync.conf tempfile
  file:
    path: "{{ __ha_cluster_tempfile_corosync_conf.path }}"
    state: absent
  when: __ha_cluster_tempfile_corosync_conf.path is defined
  run_once: true  # noqa: run_once[task]
  # We always need to create corosync.conf file to see whether it's the same as
  # what is already present on the cluster nodes. However, we don't want to
  # report it as a change since the only thing which matters is copying the
  # resulting corosync.conf to cluster nodes.
  check_mode: false
  changed_when: not ansible_check_mode
