---
# tasks file for ec2_provision_instances

# ============================================================
# create a keypair using the ssh key

- name: create the keypair for ec2
  ec2_key:
    name: "{{ resource_prefix }}"
    region: "{{ ec2_region }}"
    ec2_access_key: "{{ ec2_access_key }}"
    ec2_secret_key: "{{ ec2_secret_key }}"
    key_material: "{{ key_material }}"
    wait: yes
    state: present

# ============================================================
# create some instances for testing, and add them to a new
# group ("ec2") for use later

- name: create ec2 instances for testing
  ec2:
    instance_type: t1.micro
    image: ami-fb8e9292
    group: default
    region: "{{ ec2_region }}"
    ec2_access_key: "{{ ec2_access_key }}"
    ec2_secret_key: "{{ ec2_secret_key }}"
    key_name: "{{ resource_prefix }}"
    wait: yes
    instance_tags:
        Name: "{{ resource_prefix }}"
    exact_count: "{{ count }}"
    count_tag:
        Name: "{{ resource_prefix }}"
  register: ec2_provision_result

- name: add ec2 instances to a new group
  add_host:
    hostname: "{{ item.public_ip }}"
    groups: "ec2"
    ansible_ssh_private_key_file: "{{ sshkey }}"
  with_items: ec2_provision_result.instances

- name: wait for the instances to become available
  wait_for:
    port: 22
    host: "{{ item.public_ip }}"
  with_items: ec2_provision_result.instances
