---
#--------------------------------------------------------------#
# Cleanup ETCD cluster info                               [etcd]
#--------------------------------------------------------------#
# when pitr from another cluster, we'll need to remove the current
# cluster's metadata from etcd /pg/<cluster>/
- name: remove postgres metadata from etcd
  tags: pg_meta
  ignore_errors: true
  no_log: true
  when:
    - pg_role == 'primary'
    - pg_cluster != ''
    - pg_cluster != 'root'  # fail if you choose 'root' as cluster name
    - groups['etcd'] is defined
    - groups['etcd'] | length > 0
    #- pg_pitr.cluster is defined
    #- pg_pitr.cluster != pg_cluster
  delegate_to: '{{ groups["etcd"][0] }}'
  become: true
  environment:
    ETCDCTL_ENDPOINTS: "{{ groups['etcd'] | map('regex_replace', '^(.*)$', 'https://\\1:' + etcd_port|string) | join(',') }}"
    ETCDCTL_CACERT: "/etc/etcd/ca.crt"
    ETCDCTL_CERT: "/etc/etcd/server.crt"
    ETCDCTL_KEY: "/etc/etcd/server.key"
    ETCDCTL_USER: "root:{{ etcd_root_password }}"
  vars:
    pg_meta_prefix: "{% if pg_mode == 'citus' %}{{ pg_namespace }}/{{ pg_shard }}/{{ pg_group }}{% else %}{{ pg_namespace }}/{{ pg_cluster }}{% endif %}"
  shell: |
    etcdctl del "{{ pg_meta_prefix }}" --prefix=true
  args: { executable: /bin/bash }
...
