---
- name: validate role inputs
  fail:
    msg: Please pass in the required role variables
  when:
  - r_openshift_aws_node_group_clusterid is not defined
  - r_openshift_aws_node_group_region is not defined
  - r_openshift_aws_node_group_subnet_name is not defined

- name: fetch the subnet to use in scale group
  ec2_vpc_subnet_facts:
    region: "{{ r_openshift_aws_node_group_region }}"
    filters:
      "tag:Name": "{{ r_openshift_aws_node_group_subnet_name }}"
  register: subnetout

- name: Create the scale group
  ec2_asg:
    name: "{{ r_openshift_aws_node_group_name }}"
    launch_config_name: "{{ r_openshift_aws_node_group_lc_name }}"
    health_check_period: "{{ r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].health_check.period }}"
    health_check_type: "{{ r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].health_check.type }}"
    min_size: "{{ r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].min_size }}"
    max_size: "{{ r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].max_size }}"
    desired_capacity: "{{ r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].desired_size }}"
    region: "{{ r_openshift_aws_node_group_region }}"
    termination_policies: "{{ r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].termination_policy if 'termination_policy' in  r_openshift_aws_node_group_config[r_openshift_aws_node_group_type] else omit }}"
    load_balancers: "{{ r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].elbs if 'elbs' in r_openshift_aws_node_group_config[r_openshift_aws_node_group_type] else omit }}"
    wait_for_instances: "{{ r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].wait_for_instances | default(False)}}"
    vpc_zone_identifier: "{{ subnetout.subnets[0].id }}"
    tags:
    - "{{ r_openshift_aws_node_group_config.tags | combine(r_openshift_aws_node_group_config[r_openshift_aws_node_group_type].tags) }}"
