# SPDX-License-Identifier: MIT
---
- name: Create a corosync.conf file content using pcs-0.10
  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
