---
# create rhsm_user, rhsm_password, rhsm_subscription_pool and rhsm_server for functionality with older rhsm_user
- name: Set deprecated fact for rhel_subscription_user
  set_fact:
    rhsm_user: "{{ rhel_subscription_user }}"
  when: rhel_subscription_user is defined

- name: Set deprecated fact for rhel_subscription_pass
  set_fact:
    rhsm_password: "{{ rhel_subscription_pass }}"
  when: rhel_subscription_pass is defined

- name: Set deprecated fact for rhel_subscription_pool
  set_fact:
    rhsm_pool: "{{ rhel_subscription_pool }}"
  when: rhel_subscription_pool is defined

- name: Add masters to requisite groups
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    openshift_hostname: "{{ hostvars[item].inventory_hostname }}"
    groups: allnodes, masters, etcd, nodes, cluster_hosts, master
    openshift_node_labels:
      role: master
  with_items: "{{ groups[cluster_id + '-master'] }}"

- name: Add a master to the single master group
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: single_master
    openshift_node_labels:
      role: master
  with_items: "{{ groups[cluster_id + '-master'][0] }}"

- name: Add infra instances to host group
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: allnodes, nodes, cluster_hosts, schedulable_nodes, infra
    openshift_hostname: "{{ hostvars[item].inventory_hostname }}"
    openshift_node_labels:
      role: infra
  with_items: "{{ groups[cluster_id + '-infra'] }}"

- name: Add app instances to host group
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    openshift_hostname: "{{ hostvars[item].inventory_hostname }}"
    groups: allnodes, nodes, cluster_hosts, schedulable_nodes, app
    openshift_node_labels:
      role: app
  with_items: "{{ groups[cluster_id + '-app'] }}"

- name: Add new node instances to host group
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    openshift_hostname: "{{ hostvars[item].inventory_hostname }}"
    groups: allnodes, new_nodes
    openshift_node_labels:
      role: "{{ node_type }}"
  with_items: "{{ groups.tag_provision_node | default([]) }}"
  when:
    - add_node is defined

- name: Add NFS instances to allnodes
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: allnodes
  with_items: "{{ groups[cluster_id + '-infra-nfs'] }}"

- name: Add loadbalancer instances to allnodes
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: allnodes
  with_items: "{{ groups[cluster_id + '-loadbalancer'] }}"

- name: Add cns instances to allnodes
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: allnodes
  with_items: "{{ groups[cluster_id + '-storage'] | default([]) }}"

- name: Add crs instances to allnodes
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: allnodes
  with_items: "{{ groups[cluster_id + '-crs'] | default([]) }}"

- name: Add cns instances to host group
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: nodes, cluster_hosts, schedulable_nodes
    openshift_node_labels:
      role: storage
  with_items: "{{ groups[cluster_id + '-storage'] }}"
  when:
    - "'cns' in container_storage and add_node is defined and 'storage' in node_type"

- name: Add crs nodes to the storage group
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: storage, crs
  with_items: "{{ groups[cluster_id + '-crs'] }}"
  when:
    - "'crs' in container_storage and add_node is defined and 'storage' in node_type"

- name: Add a crs node to the single crs group
  add_host:
    name: "{{ hostvars[item].inventory_hostname }}"
    groups: single_crs
  with_items: "{{ groups[cluster_id + '-crs'][0] }}"
  when:
    - "'crs' in container_storage and add_node is defined and 'storage' in node_type"
