---
- name: Add bastion to group
  add_host:
    name: "bastion.{{ public_hosted_zone }}"
    groups: bastion
  when:
    - byo_bastion == "no"

- name: Add masters to requisite groups
  add_host:
    name: "{{ hostvars[item].ec2_tag_Name }}"
    groups: masters, etcd, nodes, cluster_hosts
    openshift_node_labels:
      role: master
      KubernetesCluster: "{{ stack_name }}"
  with_items: "{{ groups['tag_openshift_role_master'] }}"
  when:
    - hostvars[item]['ec2_tag_KubernetesCluster'] == stack_name

- name: Add a master to the single master group
  add_host:
    name: "{{ item }}"
    groups: single_master
    openshift_node_labels:
      role: master
      KubernetesCluster: "{{ stack_name }}"
  with_items: "{{ groups['masters'].0 }}"

- name: Add infra instances to host group
  add_host:
    name: "{{ hostvars[item].ec2_tag_Name }}"
    groups: nodes, cluster_hosts, schedulable_nodes
    openshift_node_labels:
      role: infra
      KubernetesCluster: "{{ stack_name }}"
  with_items: "{{ groups['tag_openshift_role_infra'] }}"
  when:
    - hostvars[item]['ec2_tag_KubernetesCluster'] == stack_name

- name: Add app instances to host group
  add_host:
    name: "{{ hostvars[item].ec2_tag_Name }}"
    groups: nodes, cluster_hosts, schedulable_nodes
    openshift_node_labels:
      role: app
      KubernetesCluster: "{{ stack_name }}"
  with_items: "{{ groups['tag_openshift_role_app'] }}"
  when:
    - hostvars[item]['ec2_tag_KubernetesCluster'] == stack_name

- name: Add storage instances to host group
  add_host:
    name: "{{ hostvars[item].ec2_tag_Name }}"
    groups: nodes, cns, cluster_hosts, schedulable_nodes, glusterfs, storage
    openshift_node_labels:
      role: storage
      KubernetesCluster: "{{ stack_name }}"
      glusterfs_devices: '[ "/dev/xvdd" ]'
  with_items: "{{ groups['tag_openshift_role_storage'] }}"
  when:
    - groups.tag_openshift_role_storage is defined and hostvars[item]['ec2_tag_KubernetesCluster'] == stack_name

- name: Add new node instances to host group
  add_host:
    name: "{{ hostvars[item].ec2_tag_Name }}"
    groups: new_nodes
    openshift_node_labels:
      role: "{{ node_type }}"
      KubernetesCluster: "{{ stack_name }}"
  with_items: "{{ groups.tag_provision_node | default([]) }}"
  when:
    - new_node_stack is defined and node_type != "gluster" and hostvars[item]['ec2_tag_aws_cloudformation_stack_name'] == new_node_stack and add_node is defined and add_node == "yes"

- name: Add new gluster node instances to host group
  add_host:
    name: "{{ hostvars[item].ec2_tag_Name }}"
    groups: new_nodes, gluster
    openshift_node_labels:
      role: storage
      KubernetesCluster: "{{ stack_name }}"
  with_items: "{{ groups.tag_provision_node | default([]) }}"
  when:
    - groups.tag_openshift_role_storage is defined and hostvars[item]['ec2_tag_KubernetesCluster'] == stack_name and add_node is defined and add_node == "yes"
