Skip to content

Instantly share code, notes, and snippets.

@Mike781
Last active March 7, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mike781/3e17f39969ef733c5884 to your computer and use it in GitHub Desktop.
Save Mike781/3e17f39969ef733c5884 to your computer and use it in GitHub Desktop.
Ansible playbook to deploy ec2 instances
---
#This playbook will provision ec2 instances. It assumes you already have the
#AWS CLI installed, keys configured, Ansible installed, etc.
- name: Provision multiple micro ec2 instances
hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Provision instances
ec2:
key_name: {{key_name}} #replace with key name
instance_type: t2.micro #optional: replace with instance type
region: us-east-1 #optional: replace with region
image: {{ami image}} #replace with AWS image
group_id: {{group_id}} #replace with AWS group ID
wait: true
exact_count: 2 #optional: replace with number of instances to spawn
count_tag:
Name: {{name}} # Replace with name
instance_tags:
Name: {{tag}} # Replace with tag (can be same as name)
register: ec2
#This groups the new hosts to allow you to
#add more tasks further down to run on them
- name: Add public IP to host group
add_host: hostname={{ item.public_ip}} groupname=ec2hosts
with_items: ec2.instances
#This waits for SSH to come up before running more tasks
- name: Wait for SSH to come up
wait_for: hostname={{ item.public_ip }} port=22 delay=60 timeout=320 state=started
with_items: ec2.instances
#optional: Additional tasks go here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment