30 Jan 2018

How to launch AWS instances using Ansible Playbook

4:47 PM Ubuntu/linux 0 Comment

We can create an EC2 instances within  AWS using Ansible. Please find the below playbook to create two instances using Ansible.


– name: Provision an EC2 Instance
hosts: localhost
connection: local
gather_facts: False
tags: provisioning
# Necessary Variables for creating/provisioning the EC2 Instance
vars:
group_id:
– sg-3e82994d
volumes:
– device_name: /dev/sda1
volume_type: gp2
volume_size: 8
delete_on_termination: True
–  device_name: /dev/sda1
volume_type: gp2
volume_size: 8
delete_on_termination: True
tasks:
– name: Launch the first EC2 Instance
local_action: ec2
count=1
aws_access_key=XXXXXXXXXXXXXXXXXX
aws_secret_key=XXXXXXXXXXXXXXXXXXXXXXXXXX
group_id={{ group_id }}
instance_type=t2.micro
image=ami-cd0f5cb6
wait=false
region=us-east-1
keypair=MyKeyPair
vpc_subnet_id=subnet-2f3e894b
assign_public_ip=no
volumes={{ volumes }}
zone=us-east-1b
termination_protection=no
– name: Launch the second EC2 Instance
local_action: ec2
count=1
aws_access_key=XXXXXXXXXXXXXXXXXX
aws_secret_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXX
group_id={{ group_id }}
instance_type=t2.micro
image=ami-cd0f5cb6
wait=false
region=us-east-1
keypair=MyKeyPair
vpc_subnet_id=subnet-afaf8ca3
assign_public_ip=no
volumes={{ volumes }}
zone=us-east-1f
termination_protection=no
register: ec2
retries: “2”
delay: “3”
with_items: ec2.instances
no_log: False

 

 

Leave a Reply