---
- name: Stop Nomad
  hosts: nomad_instances

  tasks:
    - name: Stop Nomad
      systemd:
        name: nomad
        state: stopped
      become: true

- name: Stop Vault
  hosts: vault_instances
  gather_facts: false

  tasks:
    - name: Stop Vault
      systemd:
        name: vault
        state: stopped
      become: true

- name: Recover Consul
  hosts: consul_instances
  gather_facts: false

  tasks:
    - name: Stop Consul
      systemd:
        name: consul
        state: stopped
      become: true

    - name: Get node-id
      slurp:
        src: /opt/consul/node-id
      register: consul_node_id
      become: true

    - name: Node Info
      debug:
        msg: |
          node_id: {{ consul_node_id.content | b64decode }}
          address: {{ ansible_default_ipv4.address }}

    - name: Save
      copy:
        dest: "/opt/consul/raft/peers.json"
        # I used to have reject('equalto', inventory_hostname) in the loop, but I'm not sure if I should
        content: |
          [
          {% for host in ansible_play_hosts -%}
            {
            "id": "{{ hostvars[host].consul_node_id.content | b64decode }}",
            "address": "{{ hostvars[host].ansible_default_ipv4.address }}:8300",
            "non_voter": false
            }{% if not loop.last %},{% endif %}
          {% endfor -%}
          ]
      become: true

    - name: Restart Consul
      systemd:
        name: consul
        state: restarted
      become: true

- name: Start Vault
  hosts: vault_instances
  gather_facts: false

  tasks:
    - name: Start Vault
      systemd:
        name: vault
        state: started
      become: true

- name: Start Nomad
  hosts: nomad_instances
  gather_facts: false

  tasks:
    - name: Start Nomad
      systemd:
        name: nomad
        state: started
      become: true