---
- name: Pull etcd system container
  command: atomic pull --storage=ostree {{ openshift.etcd.etcd_image }}
  register: pull_result
  changed_when: "'Pulling layer' in pull_result.stdout"

- name: Set initial Etcd cluster
  set_fact:
    etcd_initial_cluster: >-
      {% for host in etcd_peers | default([]) -%}
      {% if loop.last -%}
      {{ hostvars[host].etcd_hostname }}={{ etcd_peer_url_scheme }}://{{ hostvars[host].etcd_ip }}:{{ etcd_peer_port }}
      {%- else -%}
      {{ hostvars[host].etcd_hostname }}={{ etcd_peer_url_scheme }}://{{ hostvars[host].etcd_ip }}:{{ etcd_peer_port }},
      {%- endif -%}
      {% endfor -%}

- name: Install or Update Etcd system container package
  oc_atomic_container:
    name: etcd
    image: "{{ openshift.etcd.etcd_image }}"
    state: latest
    values:
      - ETCD_LISTEN_PEER_URLS={{ etcd_listen_peer_urls }}
      - ETCD_NAME={{ etcd_hostname }}
      - ETCD_INITIAL_CLUSTER={{ etcd_initial_cluster }}
      - ETCD_LISTEN_CLIENT_URLS={{ etcd_listen_client_urls }}
      - ETCD_INITIAL_ADVERTISE_PEER_URLS={{ etcd_initial_advertise_peer_urls }}
      - ETCD_INITIAL_CLUSTER_STATE={{ etcd_initial_cluster_state }}
      - ETCD_INITIAL_CLUSTER_TOKEN={{ etcd_initial_cluster_token }}
      - ETCD_ADVERTISE_CLIENT_URLS={{ etcd_advertise_client_urls }}
      - ETCD_CA_FILE={{ etcd_system_container_conf_dir }}/ca.crt
      - ETCD_CERT_FILE={{ etcd_system_container_conf_dir }}/server.crt
      - ETCD_KEY_FILE={{ etcd_system_container_conf_dir }}/server.key
      - ETCD_PEER_CA_FILE={{ etcd_system_container_conf_dir }}/ca.crt
      - ETCD_PEER_CERT_FILE={{ etcd_system_container_conf_dir }}/peer.crt
      - ETCD_PEER_KEY_FILE={{ etcd_system_container_conf_dir }}/peer.key
