---
- include_tasks: setup_master_group.yml

- name: fetch masters
  ec2_instance_facts:
    region: "{{ openshift_aws_region | default('us-east-1') }}"
    filters:
      "{{ {'tag:kubernetes.io/cluster/' ~ openshift_aws_clusterid: openshift_aws_clusterid,
           'tag:host-type': 'master', 'instance-state-name': 'running'} }}"
  register: mastersout
  retries: 20
  delay: 3
  until: "'instances' in mastersout and mastersout.instances|length > 0"

- name: fetch new node instances
  ec2_instance_facts:
    region: "{{ openshift_aws_region }}"
    filters:
      "{{ {'tag:kubernetes.io/cluster/' ~ openshift_aws_clusterid: openshift_aws_clusterid,
           'tag:host-type': 'node',
           'instance-state-name': 'running'} }}"
  register: instancesout
  retries: 20
  delay: 3
  until: "'instances' in instancesout and instancesout.instances|length > 0"

- name: Dump the private dns names
  debug:
    msg: "{{ instancesout.instances|map(attribute='private_dns_name') | list }}"

- name: Dump the master public ip address
  debug:
    msg: "{{ mastersout.instances[0].public_ip_address }}"

- name: approve nodes
  oc_adm_csr:
    #approve_all: True
    nodes: "{{ instancesout.instances|map(attribute='private_dns_name') | list  }}"
    timeout: 60
    fail_on_timeout: "{{ openshift_aws_node_accept_fail_on_timeout | default(false) | bool }}"
  register: nodeout
  delegate_to: "{{ groups.masters.0 }}"
