---
- name: Render Greenfield Template
  template: src=roles/cloudformation-infra/files/greenfield.json.j2 dest=roles/cloudformation-infra/files/{{ stack_name }}-greenfield.json
  when: create_vpc == "yes"

- name: Render Brownfield Template
  template: src=roles/cloudformation-infra/files/brownfield.json.j2 dest=roles/cloudformation-infra/files/{{ stack_name }}-brownfield.json
  when: create_vpc == "no" and byo_bastion == "no"

- name: Render Brownfield BYO Bastion Template
  template: src=roles/cloudformation-infra/files/brownfield-byo-bastion.json.j2 dest=roles/cloudformation-infra/files/{{ stack_name }}-brownfield-byo-bastion.json
  when: create_vpc == "no" and byo_bastion == "yes"

- name: Create Greenfield Infrastructure
  cloudformation:
    stack_name: "{{ stack_name }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/{{ stack_name }}-greenfield.json"
    template_parameters:
      Region: "{{ region }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      PublicHostedZone: "{{ public_hosted_zone }}"
      MasterApiPort: "{{ console_port }}"
      MasterHealthTarget: "TCP:{{ console_port }}"
      MasterClusterHostname: "{{ openshift_master_cluster_hostname }}"
      MasterClusterPublicHostname: "{{ openshift_master_cluster_public_hostname }}"
      AppWildcardDomain: "*.{{ wildcard_zone }}"
      VpcCidrBlock: "{{ cidr_block }}"
      VpcName: "{{ vpc_prefix }}"
      SubnetCidrBlocks: "{{ subnet_blocks }}"
      KeyName: "{{ keypair }}"
      MasterInstanceType: "{{ master_instance_type }}"
      AmiId: "{{ ami }}"
      BastionUserData: "{{ lookup('file', 'user_data_bastion.yml') | b64encode }}"
      MasterDockerVolSize: "{{ docker_storage }}"
      MasterDockerVolType: gp2
      MasterEtcdVolSize: "{{ etcd_storage }}"
      MasterEtcdVolType: gp2
      MasterUserData: "{{ lookup('file', 'user_data_master.yml') | b64encode }}"
      InfraInstanceType: "{{ node_instance_type }}"
      InfraDockerVolSize: "{{ docker_storage }}"
      InfraDockerVolType: gp2
      NodeUserData: "{{ lookup('file', 'user_data_node.yml') | b64encode }}"
      AppNodeInstanceType: "{{ app_instance_type }}"
      NodeDockerVolSize: "{{ docker_storage }}"
      NodeDockerVolType: gp2
      S3BucketName: "{{ s3_bucket_name }}"
      S3User: "{{ s3_username }}"
    tags:
      clusterid: "{{ stack_name }}"
  when: create_vpc == "yes"

- name: Create Brownfield Infrastructure
  cloudformation:
    stack_name: "{{ stack_name }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/{{ stack_name }}-brownfield.json"
    template_parameters:
      Vpc: "{{ vpc_id }}"
      PrivateSubnet1: "{{ private_subnet_id1 }}"
      PrivateSubnet2: "{{ private_subnet_id2 }}"
      PrivateSubnet3: "{{ private_subnet_id3 }}"
      PublicSubnet1: "{{ public_subnet_id1 }}"
      PublicSubnet2: "{{ public_subnet_id2 }}"
      PublicSubnet3: "{{ public_subnet_id3 }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      PublicHostedZone: "{{ public_hosted_zone }}"
      MasterApiPort: "{{ console_port }}"
      MasterHealthTarget: "TCP:{{ console_port }}"
      MasterClusterHostname: "{{ openshift_master_cluster_hostname }}"
      MasterClusterPublicHostname: "{{ openshift_master_cluster_public_hostname }}"
      AppWildcardDomain: "*.{{ wildcard_zone }}"
      KeyName: "{{ keypair }}"
      MasterInstanceType: "{{ master_instance_type }}"
      AmiId: "{{ ami }}"
      BastionInstanceType: "{{ bastion_instance_type }}"
      BastionUserData: "{{ lookup('file', 'user_data_bastion.yml') | b64encode }}"
      MasterRootVolSize: "50"
      BastionRootVolSize: "10"
      BastionRootVolType: gp2
      MasterRootVolType: gp2
      MasterDockerVolSize: "{{ docker_storage }}"
      MasterDockerVolType: gp2
      MasterEtcdVolSize: "{{ etcd_storage }}"
      MasterEtcdVolType: gp2
      MasterEmptyVolSize: "5"
      MasterEmptyVolType: gp2
      MasterUserData: "{{ lookup('file', 'user_data_master.yml') | b64encode }}"
      InfraInstanceType: "{{ node_instance_type }}"
      InfraRootVolSize: "50"
      InfraRootVolType: gp2
      InfraDockerVolSize: "{{ docker_storage }}"
      InfraDockerVolType: gp2
      NodeEmptyVolSize: "{{ emptydir_storage }}"
      NodeEmptyVolType: gp2
      NodeUserData: "{{ lookup('file', 'user_data_node.yml') | b64encode }}"
      AppNodeInstanceType: "{{ app_instance_type }}"
      NodeRootVolSize: "50"
      NodeRootVolType: gp2
      NodeDockerVolSize: "{{ docker_storage }}"
      NodeDockerVolType: gp2
      S3BucketName: "{{ s3_bucket_name }}"
      S3User: "{{ s3_username }}"
    tags:
      clusterid: "{{ stack_name }}"
  when: create_vpc == "no" and byo_bastion == "no"

- name: Create Brownfield Infrastructure with existing bastion
  cloudformation:
    stack_name: "{{ stack_name }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/{{ stack_name }}-brownfield-byo-bastion.json"
    template_parameters:
      Vpc: "{{ vpc_id }}"
      BastionSg: "{{ bastion_sg }}"
      PrivateSubnet1: "{{ private_subnet_id1 }}"
      PrivateSubnet2: "{{ private_subnet_id2 }}"
      PrivateSubnet3: "{{ private_subnet_id3 }}"
      PublicSubnet1: "{{ public_subnet_id1 }}"
      PublicSubnet2: "{{ public_subnet_id2 }}"
      PublicSubnet3: "{{ public_subnet_id3 }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      PublicHostedZone: "{{ public_hosted_zone }}"
      MasterApiPort: "{{ console_port }}"
      MasterHealthTarget: "TCP:{{ console_port }}"
      MasterClusterHostname: "{{ openshift_master_cluster_hostname }}"
      MasterClusterPublicHostname: "{{ openshift_master_cluster_public_hostname }}"
      AppWildcardDomain: "*.{{ wildcard_zone }}"
      KeyName: "{{ keypair }}"
      MasterInstanceType: "{{ master_instance_type }}"
      AmiId: "{{ ami }}"
      MasterRootVolSize: "50"
      MasterRootVolType: gp2
      MasterDockerVolSize: "{{ docker_storage }}"
      MasterDockerVolType: gp2
      MasterEtcdVolSize: "{{ etcd_storage }}"
      MasterEtcdVolType: gp2
      MasterEmptyVolSize: "5"
      MasterEmptyVolType: gp2
      MasterUserData: "{{ lookup('file', 'user_data_master.yml') | b64encode }}"
      InfraInstanceType: "{{ node_instance_type }}"
      InfraRootVolSize: "50"
      InfraRootVolType: gp2
      InfraDockerVolSize: "{{ docker_storage }}"
      InfraDockerVolType: gp2
      NodeEmptyVolSize: "{{ emptydir_storage }}"
      NodeEmptyVolType: gp2
      NodeUserData: "{{ lookup('file', 'user_data_node.yml') | b64encode }}"
      AppNodeInstanceType: "{{ app_instance_type }}"
      NodeRootVolSize: "50"
      NodeRootVolType: gp2
      NodeDockerVolSize: "{{ docker_storage }}"
      NodeDockerVolType: gp2
      S3BucketName: "{{ s3_bucket_name }}"
      S3User: "{{ s3_username }}"
    tags:
      clusterid: "{{ stack_name }}"
  when: create_vpc == "no" and byo_bastion == "yes"

- name: Add App Node
  cloudformation:
    stack_name: "{{ stack_name }}-{{ shortname }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/add-node.json"
    template_parameters:
      NodeName: "{{ fqdn }}"
      NodeSg: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeSGId'] | default(node_sg) }}"
      Subnet: "{{ subnet_id }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      KeyName: "{{ keypair }}"
      AmiId: "{{ ami }}"
      InstanceType: "{{ node_instance_type }}"
      NodeEmptyVolSize: "{{ emptydir_storage }}"
      NodeEmptyVolType: gp2
      NodeUserData: "{{ lookup('file', 'user_data_node.yml') | b64encode }}"
      NodeRootVolSize: "50"
      NodeRootVolType: gp2
      NodeDockerVolSize: "{{ docker_storage }}"
      NodeDockerVolType: gp2
      NodeInstanceProfile: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeARN'] | default(iam_role) }}"
      NodeType: "{{ node_type }}"
    tags:
      clusterid: "{{ stack_name }}"
      KubernetesCluster: "{{ stack_name }}"
  when: create_vpc == "no" and add_node == "yes" and node_type == "app"

- name: Add Infra Node
  cloudformation:
    stack_name: "{{ stack_name }}-{{ shortname }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/add-infra-node.json"
    template_parameters:
      NodeName: "{{ fqdn }}"
      NodeSg: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeSGId'] | default(node_sg) }}"
      InfraSg: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['InfraSGId'] | default(infra_sg) }}"
      Subnet: "{{ subnet_id }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      KeyName: "{{ keypair }}"
      AmiId: "{{ ami }}"
      InstanceType: "{{ node_instance_type }}"
      NodeEmptyVolSize: "{{ emptydir_storage }}"
      NodeEmptyVolType: gp2
      NodeUserData: "{{ lookup('file', 'user_data_node.yml') | b64encode }}"
      NodeRootVolSize: "50"
      NodeRootVolType: gp2
      NodeDockerVolSize: "{{ docker_storage }}"
      NodeDockerVolType: gp2
      NodeInstanceProfile: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeARN'] | default(iam_role) }}"
      NodeType: "{{ node_type }}"
    tags:
      clusterid: "{{ stack_name }}"
      KubernetesCluster: "{{ stack_name }}"
  when: create_vpc == "no" and add_node == "yes" and node_type == "infra"

- name: Get cfn stack outputs
  cloudformation_facts:
    stack_name: "{{ stack_name }}"
    region: "{{ region }}"
  register: stack

- name: Add CNS Gluster Nodes with io1
  cloudformation:
    stack_name: "{{ stack_name }}-{{ glusterfs_stack_name }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/add-cns-storage-iops.json"
    template_parameters:
      GlusterNodeDns1: "{{ gluster_node01 }}"
      GlusterNodeDns2: "{{ gluster_node02 }}"
      GlusterNodeDns3: "{{ gluster_node03 }}"
      NodeSg: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeSGId'] | default(node_sg) }}"
      PrivateSubnet1: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet1'] | default(private_subnet_id1) }}"
      PrivateSubnet2: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet2'] | default(private_subnet_id2) }}"
      PrivateSubnet3: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet3'] | default(private_subnet_id3) }}"
      PublicHostedZone: "{{ public_hosted_zone }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      KeyName: "{{ keypair }}"
      AmiId: "{{ ami }}"
      InstanceType: "{{ node_instance_type }}"
      NodeEmptyVolSize: "{{ emptydir_storage }}"
      NodeEmptyVolType: gp2
      NodeUserData: "{{ lookup('file', 'user_data_node.yml') | b64encode }}"
      NodeRootVolSize: "50"
      NodeRootVolType: gp2
      GlusterVolSize: "{{ glusterfs_volume_size }}"
      GlusterVolType: "{{ glusterfs_volume_type }}"
      Iops: "{{ iops }}"
      NodeDockerVolSize: "{{ docker_storage }}"
      NodeDockerVolType: gp2
      NodeInstanceProfile: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeARN'] | default(iam_role) }}"
      NodeType: glusterfs
    tags:
      clusterid: "{{ stack_name }}"
      KubernetesCluster: "{{ stack_name }}"
  when: deploy_glusterfs == "true" and glusterfs_volume_type == "io1" or create_vpc == "no" and add_node == "yes" and node_type == "glusterfs" and glusterfs_volume_type == "io1"

- name: Add CNS Gluster Nodes
  cloudformation:
    stack_name: "{{ stack_name }}-{{ glusterfs_stack_name }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/add-cns-storage.json"
    template_parameters:
      GlusterNodeDns1: "{{ gluster_node01 }}"
      GlusterNodeDns2: "{{ gluster_node02 }}"
      GlusterNodeDns3: "{{ gluster_node03 }}"
      NodeSg: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeSGId'] | default(node_sg) }}"
      PrivateSubnet1: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet1'] | default(private_subnet_id1) }}"
      PrivateSubnet2: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet2'] | default(private_subnet_id2) }}"
      PrivateSubnet3: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet3'] | default(private_subnet_id3) }}"
      PublicHostedZone: "{{ public_hosted_zone }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      KeyName: "{{ keypair }}"
      AmiId: "{{ ami }}"
      InstanceType: "{{ node_instance_type }}"
      NodeEmptyVolSize: "{{ emptydir_storage }}"
      NodeEmptyVolType: gp2
      NodeUserData: "{{ lookup('file', 'user_data_node.yml') | b64encode }}"
      NodeRootVolSize: "50"
      NodeRootVolType: gp2
      GlusterVolSize: "{{ glusterfs_volume_size }}"
      GlusterVolType: "{{ glusterfs_volume_type }}"
      NodeDockerVolSize: "{{ docker_storage }}"
      NodeDockerVolType: gp2
      NodeInstanceProfile: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeARN'] | default(iam_role) }}"
      NodeType: glusterfs
    tags:
      clusterid: "{{ stack_name }}"
      KubernetesCluster: "{{ stack_name }}"
  when: deploy_glusterfs == "true" and glusterfs_volume_type != "io1" or create_vpc == "no" and add_node == "yes" and node_type == "glusterfs" and glusterfs_volume_type != "io1"

- name: Add CRS Gluster Nodes with IO1
  cloudformation:
    stack_name: "{{ stack_name }}-{{ glusterfs_stack_name }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/add-crs-storage-iops.json"
    template_parameters:
      Vpc: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['StackVpc'] | default(vpc) }}"
      BastionSG: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['BastionSGId'] | default(bastion_sg) }}"
      NodeSG: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeSGId'] | default(node_sg) }}"
      GlusterNodeDns1: "{{ gluster_node01 }}"
      GlusterNodeDns2: "{{ gluster_node02 }}"
      GlusterNodeDns3: "{{ gluster_node03 }}"
      PrivateSubnet1: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet1'] | default(private_subnet_id1) }}"
      PrivateSubnet2: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet2'] | default(private_subnet_id2) }}"
      PrivateSubnet3: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet3'] | default(private_subnet_id3) }}"
      PublicHostedZone: "{{ public_hosted_zone }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      KeyName: "{{ keypair }}"
      AmiId: "{{ ami }}"
      InstanceType: "{{ node_instance_type }}"
      NodeUserData: "{{ lookup('file', 'user_data_gluster.yml') | b64encode }}"
      NodeRootVolSize: "50"
      NodeRootVolType: gp2
      GlusterVolSize: "{{ glusterfs_volume_size }}"
      GlusterVolType: "{{ glusterfs_volume_type }}"
      Iops: "{{ iops }}"
    tags:
      clusterid: "{{ stack_name }}"
      KubernetesCluster: "{{ stack_name }}"
  when: deploy_crs is defined and create_vpc == "no" and deploy_crs == "yes" and glusterfs_volume_type == "io1"

- name: Add CRS Gluster Nodes
  cloudformation:
    stack_name: "{{ stack_name }}-{{ glusterfs_stack_name }}"
    state: "present"
    region: "{{ region }}"
    template: "roles/cloudformation-infra/files/add-crs-storage.json"
    template_parameters:
      Vpc: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['StackVpc'] | default(vpc) }}"
      BastionSG: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['BastionSGId'] | default(bastion_sg) }}"
      NodeSG: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['NodeSGId'] | default(node_sg) }}"
      GlusterNodeDns1: "{{ gluster_node01 }}"
      GlusterNodeDns2: "{{ gluster_node02 }}"
      GlusterNodeDns3: "{{ gluster_node03 }}"
      PrivateSubnet1: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet1'] | default(private_subnet_id1) }}"
      PrivateSubnet2: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet2'] | default(private_subnet_id2) }}"
      PrivateSubnet3: "{{ stack['ansible_facts']['cloudformation'][stack_name]['stack_outputs']['PrivateSubnet3'] | default(private_subnet_id3) }}"
      PublicHostedZone: "{{ public_hosted_zone }}"
      Route53HostedZone: "{{ public_hosted_zone }}."
      KeyName: "{{ keypair }}"
      AmiId: "{{ ami }}"
      InstanceType: "{{ node_instance_type }}"
      NodeUserData: "{{ lookup('file', 'user_data_gluster.yml') | b64encode }}"
      NodeRootVolSize: "50"
      NodeRootVolType: gp2
      GlusterVolSize: "{{ glusterfs_volume_size }}"
      GlusterVolType: "{{ glusterfs_volume_type }}"
    tags:
      clusterid: "{{ stack_name }}"
      KubernetesCluster: "{{ stack_name }}"
  when: deploy_crs is defined and create_vpc == "no" and deploy_crs == "yes" and glusterfs_volume_type != "io1"
