---
- name: Backup etcd
  hosts: oo_etcd_hosts_to_backup
  tasks:
  - import_role:
      name: etcd
      tasks_from: backup.yml
    vars:
      r_etcd_common_backup_tag: "{{ etcd_backup_tag }}"
      r_etcd_common_backup_sufix_name: "{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}"
      r_etcd_common_skip_command_shim: true

- name: Gate on etcd backup
  hosts: localhost
  connection: local
  tasks:
  - set_fact:
      etcd_backup_completed: "{{ hostvars
                                 | lib_utils_oo_select_keys(groups.oo_etcd_hosts_to_backup)
                                 | lib_utils_oo_collect('inventory_hostname', {'r_etcd_common_backup_complete': true}) }}"
  - set_fact:
      etcd_backup_failed: "{{ groups.oo_etcd_hosts_to_backup | difference(etcd_backup_completed) | list }}"
  - fail:
      msg: "Upgrade cannot continue. The following hosts did not complete etcd backup: {{ etcd_backup_failed | join(',') }}"
    when: etcd_backup_failed | length > 0
