Skip to content

Instantly share code, notes, and snippets.

@craigmarvelley
Created May 23, 2014 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigmarvelley/9a27c53413c5010b9ad4 to your computer and use it in GitHub Desktop.
Save craigmarvelley/9a27c53413c5010b9ad4 to your computer and use it in GitHub Desktop.
Launch and provision EC2 instances in one go
---
- name: Launch new instances
local_action:
module: ec2
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
region: "{{ ec2_region }}"
keypair: "{{ ec2_keypair }}"
group: "{{ ec2_group }}"
instance_type: "{{ ec2_instance_type }}"
image: "{{ ec2_image }}"
count: "{{ ec2_count }}"
wait: yes
register: launched_instances
- name: Add all instance public IPs to a host group so they can be referenced in tasks
add_host: hostname={{ item.public_ip }} groupname=launched_instance_hosts
with_items: launched_instances.instances
- name: Wait until SSH is available
local_action: wait_for host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started
with_items: launched_instances.instances
---
- name: Launch and configure ec2 instances
hosts:
- 127.0.0.1
connection: local
gather_facts: false
vars_files:
- vars/credentials.yml
roles:
- { role: launch_instances }
- { role: tag_instances }
- name: Configure ec2 instances post-creation
hosts: launched_instance_hosts # uses a var defined dynamically in the previous playbook
user: ubuntu
sudo: yes
gather_facts: true
roles:
- { role: webserver }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment