---
- name: Create EC2 instance ( Bastion )
  ec2:
    assign_public_ip: yes
    count_tag:
      Name: "bastion"
    exact_count: 1
    group: [
      "Bastion"
    ]
    instance_type: "{{ ec2_type_bastion }}"
    image: "{{ ec2ami }}"
    instance_tags:
      Name: "bastion"
      Clusterid: "{{ clusterid }}"
      ami: "{{ ec2ami }}"
      "kubernetes.io/cluster/{{ clusterid }}": "{{ clusterid }}"
    key_name: "{{ clusterid }}.{{ dns_domain }}"
    monitoring: no
    region: "{{ aws_region }}"
    termination_protection: no
    user_data: "{{ lookup('template', ( playbook_dir + '/roles/aws/templates/ec2_userdata.sh.j2') ) }}"
    volumes:
      - device_name: /dev/sda1
        volume_type: gp2
        volume_size: 25
        delete_on_termination: true
    vpc_subnet_id: "{{ item.subnet }}"
    wait: yes
  with_items:
    - name: "bastion"
      subnet: "{{ subnet_public.results.0.subnet.id }}"
      type: "{{ ec2_type_bastion }}"
  retries: 3
  delay: 3
  register: ec2bastion

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].id }}"
    - tagss: "clusterid={{ clusterid }}, ami={{ ec2ami }}"
  with_items: "{{ ec2bastion.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/sda1'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-rootvol, clusterid={{ clusterid }}, "
  with_items: "{{ ec2bastion.results }}"

- name: Create EIP instance for EC2 / Bastion
  ec2_eip:
    device_id: "{{ ec2bastion.results[0].tagged_instances[0].id }}"
    in_vpc: yes
    region: "{{ aws_region }}"
    state: present
  retries: 3
  register: eipbastion

- name: Create EC2 instances ( Master )
  ec2:
    assign_public_ip: no
    count_tag:
      Name: "{{ item.name }}"
    exact_count: 1
    group: [
      "master",
      "node"
    ]
    instance_type: "{{ item.type }}"
    image: "{{ ec2ami }}"
    instance_tags:
      Name: "{{ item.name }}"
    key_name: "{{ clusterid }}.{{ dns_domain }}"
    monitoring: no
    region: "{{ aws_region }}"
    termination_protection: no
    user_data: "{{ lookup('template', ( playbook_dir + '/roles/aws/templates/ec2_userdata.sh.j2') ) }}"
    volumes:
      - device_name: /dev/sda1
        volume_type: gp2
        volume_size: 100
      - device_name: /dev/xvdb
        volume_type: gp2
        volume_size: 100
      - device_name: /dev/xvdc
        volume_type: gp2
        volume_size: 100
      - device_name: /dev/xvdd
        volume_type: gp2
        volume_size: 100
        delete_on_termination: true
    vpc_subnet_id: "{{ item.subnet }}"
    wait: yes
  with_items:
    - name: "master1"
      subnet: "{{ subnet_private.results.0.subnet.id }}"
      type: "{{ ec2_type_master }}"
    - name: "master2"
      subnet: "{{ subnet_private.results.1.subnet.id }}"
      type: "{{ ec2_type_master }}"
    - name: "master3"
      subnet: "{{ subnet_private.results.2.subnet.id }}"
      type: "{{ ec2_type_master }}"
  retries: 3
  delay: 3
  register: ec2master

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].id }}"
    - tagss: "clusterid={{ clusterid }}, ami={{ ec2ami }}, kubernetes.io/cluster/{{ clusterid }}={{ clusterid }}, host-type=master, sub-host-type=default"
  with_items: "{{ ec2master.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/sda1'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-rootvol, clusterid={{ clusterid }}, "
  with_items: "{{ ec2master.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/xvdb'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-xvdb, clusterid={{ clusterid }}, "
  with_items: "{{ ec2master.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/xvdc'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-xvdc, clusterid={{ clusterid }}, "
  with_items: "{{ ec2master.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/xvdd'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-xvdd, clusterid={{ clusterid }}, "
  with_items: "{{ ec2master.results }}"

- name: Create EC2 instances ( Infrastructure )
  ec2:
    assign_public_ip: no
    count_tag:
      Name: "{{ item.name }}"
    exact_count: 1
    group: [
      "infra",
      "node"
    ]
    instance_type: "{{ item.type }}"
    image: "{{ ec2ami }}"
    instance_tags:
      Name: "{{ item.name }}"
    key_name: "{{ clusterid }}.{{ dns_domain }}"
    monitoring: no
    region: "{{ aws_region }}"
    termination_protection: no
    user_data: "{{ lookup('template', ( playbook_dir + '/roles/aws/templates/ec2_userdata.sh.j2') ) }}"
    volumes:
      - device_name: /dev/sda1
        volume_type: gp2
        volume_size: 100
      - device_name: /dev/xvdb
        volume_type: gp2
        volume_size: 100
      - device_name: /dev/xvdc
        volume_type: gp2
        volume_size: 100
    vpc_subnet_id: "{{ item.subnet }}"
    wait: yes
  with_items:
    - name: "infra1"
      subnet: "{{ subnet_private.results.0.subnet.id }}"
      type: "{{ ec2_type_infra }}"
    - name: "infra2"
      subnet: "{{ subnet_private.results.1.subnet.id }}"
      type: "{{ ec2_type_infra }}"
    - name: "infra3"
      subnet: "{{ subnet_private.results.2.subnet.id }}"
      type: "{{ ec2_type_infra }}"
  retries: 3
  delay: 3
  register: ec2infra

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].id }}"
    - tagss: "clusterid={{ clusterid }}, ami={{ ec2ami }}, kubernetes.io/cluster/{{ clusterid }}={{ clusterid }}, host-type=node, sub-host-type=infra"
  with_items: "{{ ec2infra.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/sda1'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-rootvol, clusterid={{ clusterid }}, "
  with_items: "{{ ec2infra.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/xvdb'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-xvdb, clusterid={{ clusterid }}, "
  with_items: "{{ ec2infra.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/xvdc'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-xvdc, clusterid={{ clusterid }}, "
  with_items: "{{ ec2infra.results }}"

- name: Create EC2 instances ( App node )
  ec2:
    assign_public_ip: no
    count_tag:
      Name: "{{ item.name }}"
    exact_count: 1
    group: [
      "node"
    ]
    instance_type: "{{ item.type }}"
    image: "{{ ec2ami }}"
    instance_tags:
      Name: "{{ item.name }}"
    key_name: "{{ clusterid }}.{{ dns_domain }}"
    monitoring: no
    region: "{{ aws_region }}"
    termination_protection: no
    user_data: "{{ lookup('template', ( playbook_dir + '/roles/aws/templates/ec2_userdata.sh.j2') ) }}"
    volumes:
      - device_name: /dev/sda1
        volume_type: gp2
        volume_size: 100
      - device_name: /dev/xvdb
        volume_type: gp2
        volume_size: 100
      - device_name: /dev/xvdc
        volume_type: gp2
        volume_size: 100
    vpc_subnet_id: "{{ item.subnet }}"
    wait: yes
  with_items:
    - name: "node1"
      subnet: "{{ subnet_private.results.0.subnet.id }}"
      type: "{{ ec2_type_node }}"
    - name: "node2"
      subnet: "{{ subnet_private.results.1.subnet.id }}"
      type: "{{ ec2_type_node }}"
    - name: "node3"
      subnet: "{{ subnet_private.results.2.subnet.id }}"
      type: "{{ ec2_type_node }}"
  retries: 3
  delay: 3
  register: ec2node

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].id }}"
    - tagss: "clusterid={{ clusterid }}, ami={{ ec2ami }}, kubernetes.io/cluster/{{ clusterid }}={{ clusterid }}, host-type=node, sub-host-type=compute"
  with_items: "{{ ec2node.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/sda1'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-rootvol, clusterid={{ clusterid }}, "
  with_items: "{{ ec2node.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/xvdb'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-xvdb, clusterid={{ clusterid }}, "
  with_items: "{{ ec2node.results }}"

- include_tasks: tag.yaml
  vars:
    - resource: "{{ item.tagged_instances[0].block_device_mapping['/dev/xvdc'].volume_id }}"
    - tagss: "Name={{ item.tagged_instances[0].id }}-xvdc, clusterid={{ clusterid }}, "
  with_items: "{{ ec2node.results }}"
