---
- name: Backup etcd
  import_tasks: backup.yml

- name: Etcd | reload systemd
  systemd:
    daemon_reload: true
  listen:
    - Restart etcd
    - Restart etcd-events

- name: Reload etcd
  service:
    name: etcd
    state: restarted
  when: is_etcd_master
  listen: Restart etcd

- name: Reload etcd-events
  service:
    name: etcd-events
    state: restarted
  when: is_etcd_master
  listen: Restart etcd-events

- name: Wait for etcd up
  uri:
    url: "https://{% if is_etcd_master %}{{ etcd_address }}{% else %}127.0.0.1{% endif %}:2379/health"
    validate_certs: no
    client_cert: "{{ etcd_cert_dir }}/member-{{ inventory_hostname }}.pem"
    client_key: "{{ etcd_cert_dir }}/member-{{ inventory_hostname }}-key.pem"
  register: result
  until: result.status is defined and result.status == 200
  retries: 60
  delay: 1
  listen: Restart etcd

- name: Cleanup etcd backups
  import_tasks: backup_cleanup.yml

- name: Wait for etcd-events up
  uri:
    url: "https://{% if is_etcd_master %}{{ etcd_address }}{% else %}127.0.0.1{% endif %}:2383/health"
    validate_certs: no
    client_cert: "{{ etcd_cert_dir }}/member-{{ inventory_hostname }}.pem"
    client_key: "{{ etcd_cert_dir }}/member-{{ inventory_hostname }}-key.pem"
  register: result
  until: result.status is defined and result.status == 200
  retries: 60
  delay: 1
  listen: Restart etcd-events

- name: Set etcd_secret_changed
  set_fact:
    etcd_secret_changed: true
