Skip to content

Instantly share code, notes, and snippets.

@mchelen
Last active August 29, 2015 14:15
Show Gist options
  • Save mchelen/51bb24c0f3dda62b3756 to your computer and use it in GitHub Desktop.
Save mchelen/51bb24c0f3dda62b3756 to your computer and use it in GitHub Desktop.
ansible aws

you must set up a security group ansible-test on ec2

the basic requirement is for inbound ssh and i used my ip as the allowed traffic source

remember! t2 instances must be launched in VPC, so you have to create a VPC security group if not using default

also you must specify a subnet id or network interface id

subnets can be found with: ec2-describe-subnets

key_name is the name of the ec2 key you have set up on the ec2 console

for this example, we are using region us-east-1 and ami ami-9a562df2 (ubuntu 14)

note: the hosts file will not be modified, the new instance ip is only stored in memory while executing tasks in the playbook

sudo pip install boto

ansible-playbook -i hosts demo_setup.yml

#!/bin/bash
# this file must be run with `source creds.sh`
export AWS_ACCESS_KEY_ID='foo'
export AWS_SECRET_ACCESS_KEY='bar'
# demo_setup.yml
- hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Provision a set of instances
ec2:
key_name: ansible-test-key
group: ansible-test
instance_type: t2.micro
image: "ami-9a562df2"
wait: true
exact_count: 1
region: us-east-1
count_tag:
Name: ansible-test
instance_tags:
Name: ansible-test
vpc_subnet_id: subnet-55dea56f
assign_public_ip: yes
register: ec2
- name: Add all instance public IPs to host group
add_host: hostname={{ item.public_ip }} groupname=ec2hosts
with_items: ec2.instances
- hosts: ec2hosts
name: configuration play
user: ubuntu
gather_facts: true
tasks:
- name: Check NTP service
service: name=ntpd state=started
[local]
localhost
[ec2hosts]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment