---
- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - include_vars: "{{ item }}"
      with_first_found:
        - vars.yml
        - vars.yaml

    - name: list available AMIs
      ec2_ami_facts:
        region: "{{ aws_region }}"
        filters: "{{ aws_ami_tags }}"
      register: ami_facts
      when: aws_image is not defined

    - name: determine which AMI to use
      set_fact:
        aws_image: "{{ ami_facts.images[-1].image_id }}"
      when: aws_image is not defined

    - name: determine which AMI to use
      set_fact:
        aws_image: "{{ ami_facts.images[-1].image_id }}"
      when: aws_image is not defined

    - name: Create AWS security group
      ec2_group:
        name: "{{ item.aws_security_group }}"
        description: "Openshift Ansible SG for {{ vm_prefix }}"
        vpc_id: "{{ aws_vpc_id }}"
        region: "{{ aws_region }}"
        rules:
          - proto: all
            cidr_ip: 0.0.0.0/0
        rules_egress:
          - proto: all
            cidr_ip: 0.0.0.0/0
        tags: "{{ aws_sg_tags }}"
      with_items: "{{ aws_instances }}"
      when: not aws_use_auto_terminator | default(true)
      vars:
        aws_sg_tags: |
          {
            "kubernetes.io/cluster/{{ aws_cluster_id }}": "true",
            "expirationDate": "{{ item.aws_expiration_date | default(aws_expiration_date) }}"
          }

    - name: Create EC2 instance
      ec2:
        region: "{{ aws_region }}"
        key_name: "{{ aws_key }}"
        instance_type: "{{ item.aws_flavor }}"
        image: "{{ item.aws_image | default(aws_image) }}"
        wait: yes
        group: "{{ item.aws_security_group }}"
        count: 1
        vpc_subnet_id: "{{ aws_subnet }}"
        assign_public_ip: yes
        instance_tags: "{{ aws_instance_tags }}"
        volumes: "{{ item.aws_volumes | default(omit) }}"
        instance_initiated_shutdown_behavior: "{{ (aws_use_auto_terminator | default(true)) | ternary('stop', 'terminate') }}"
      register: ec2
      with_items: "{{ aws_instances }}"
      vars:
        aws_instance_tags: |
          {
            "kubernetes.io/cluster/{{ aws_cluster_id }}": "true",
            "Name": "{{ item.name }}",
            "ansible-groups": "{{ item.ansible_groups | join(',') }}",
            "ansible-node-group": "{{ item.node_group }}",
            "expirationDate": "{{ item.aws_expiration_date | default(aws_expiration_date) }}"
          }

    - name: Add machine to inventory
      add_host:
        name: "{{ item.instances.0.tags['Name'] }}"
        ansible_host: "{{ item.instances.0.dns_name }}"
        ansible_user: "{{ item.instances.0.aws_user | default(aws_user)}}"
        groups: "{{ item.instances.0.tags['ansible-groups'].split(',') }}"
        aws_region: "{{ aws_region }}"
        aws_ip: "{{ item.instances.0.public_ip }}"
        aws_id: "{{ item.instances.0.id }}"
        openshift_node_group_name: "{{ item.instances.0.tags['ansible-node-group'] }}"
      with_items: "{{ ec2.results }}"

    - name: write the inventory
      template:
        src: ./template-inventory.j2
        dest: "inventory/hosts"

    - name: Refresh inventory to ensure new instances exist in inventory
      meta: refresh_inventory

- hosts: all
  gather_facts: no
  become: true
  tasks:
    - wait_for_connection: {}
    - setup: {}
    - name: Make sure hostname is set to public ansible host
      hostname:
        name: "{{ ansible_host }}"

- import_playbook: ../../playbooks/openshift-node/network_manager.yml
- import_playbook: ../../playbooks/prerequisites.yml
- import_playbook: ../../playbooks/deploy_cluster.yml
