Commit 86e99f08 by John Jarvis

first commit

parents
---
# This playbook demonstrates how to use the ansible cloudformation module to launch an AWS CloudFormation stack.
#
# This module requires that the boto python library is installed, and that you have your AWS credentials
# in $HOME/.boto
#The thought here is to bring up a bare infrastructure with CloudFormation, but use ansible to configure it.
#I generally do this in 2 different playbook runs as to allow the ec2.py inventory to be updated.
#This module also uses "complex arguments" which were introduced in ansible 1.1 allowing you to specify the
#Cloudformation template parameters
#This example launches a 3 node AutoScale group, with a security group, and an InstanceProfile with root permissions.
#If a stack does not exist, it will be created. If it does exist and the template file has changed, the stack will be updated.
#If the parameters are different, the stack will also be updated.
#CloudFormation stacks can take awhile to provision, if you are curious about its status, use the AWS
#web console or one of the CloudFormation CLI's.
#Example update -- try first launching the stack with 3 as the ClusterSize. After it is launched, change it to 4
#and run the playbook again.
- name: provision stack
hosts: localhost
connection: local
gather_facts: false
# Launch the cloudformation-example.json template. Register the output.
tasks:
- name: edX configuration
cloudformation: >
stack_name="ansible-cloudformation" state=present
region=us-east-1 disable_rollback=false
template=files/edx-server-ubuntu-configuration.json
args:
template_parameters:
KeyName: deployment
DiskType: ebs
InstanceType: m1.small
ClusterSize: 3
register: stack
- name: show stack outputs
debug: msg="My stack outputs are ${stack.stack_outputs}"
[ec2]
regions=all
destination_variable=public_dns_name
vpc_destination_variable=ip_address
cache_path=/tmp
cache_max_age=300
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Sample template to bring up an Edx Server. A WaitCondition is used to hold up the stack creation until the application is deployed. **WARNING** This template creates one or more Amazon EC2 instances. You will be billed for the AWS resources used if you create a stack from this template.",
"Parameters": {
"NameTag": {
"Type": "String",
"Description": "Name Tag"
},
"GroupTag": {
"Type": "String",
"Description": "Group Tag"
},
"KeyName": {
"Type": "String",
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the web server"
},
"InstanceType" : {
"Description" : "WebServer EC2 instance type",
"Type" : "String",
"Default" : "m1.small",
"AllowedValues" : [ "t1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","m3.xlarge","m3.2xlarge","c1.medium","c1.xlarge","cc1.4xlarge","cc2.8xlarge","cg1.4xlarge"],
"ConstraintDescription" : "must be a valid EC2 instance type."
},
"SSHLocation" : {
"Description" : "The IP address range that can be used to SSH to the EC2 instances",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "0.0.0.0/0",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
}
},
"Mappings" : {
"AWSInstanceType2Arch" : {
"t1.micro" : { "Arch" : "64" },
"m1.small" : { "Arch" : "64" },
"m1.medium" : { "Arch" : "64" },
"m1.large" : { "Arch" : "64" },
"m1.xlarge" : { "Arch" : "64" },
"m2.xlarge" : { "Arch" : "64" },
"m2.2xlarge" : { "Arch" : "64" },
"m2.4xlarge" : { "Arch" : "64" },
"m3.xlarge" : { "Arch" : "64" },
"m3.2xlarge" : { "Arch" : "64" },
"c1.medium" : { "Arch" : "64" },
"c1.xlarge" : { "Arch" : "64" }
},
"AWSRegionArch2AMI" : {
"us-east-1" : { "32" : "ami-def89fb7", "64" : "ami-d0f89fb9" },
"us-west-1" : { "32" : "ami-fc002cb9", "64" : "ami-ce7b6fba" },
"us-west-2" : { "32" : "ami-0ef96e3e", "64" : "ami-70f96e40" },
"eu-west-1" : { "32" : "ami-c27b6fb6", "64" : "ami-ce7b6fba" },
"sa-east-1" : { "32" : "ami-a1da00bc", "64" : "ami-a3da00be" },
"ap-southeast-1" : { "32" : "ami-66084734", "64" : "ami-64084736" },
"ap-southeast-2" : { "32" : "ami-06ea7a3c", "64" : "ami-04ea7a3e" },
"ap-northeast-1" : { "32" : "ami-fc6ceefd", "64" : "ami-fe6ceeff" }
}
},
"Resources" : {
"EdxServerUser" : {
"Type" : "AWS::IAM::User",
"Properties" : {
"Path": "/",
"Policies": [{
"PolicyName": "root",
"PolicyDocument": { "Statement":[{
"Effect":"Allow",
"Action": [
"cloudformation:DescribeStackResource",
"s3:Put"
],
"Resource":"*"
}]}
}]
}
},
"HostKeys" : {
"Type" : "AWS::IAM::AccessKey",
"Properties" : {
"UserName" : {"Ref": "EdxServerUser"}
}
},
"EdxServer": {
"Type": "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"packages" : {
"apt" : {
"ruby" : [],
"ruby-dev" : [],
"libopenssl-ruby" : [],
"rdoc" : [],
"ri" : [],
"irb" : [],
"build-essential" : [],
"wget" : [],
"ssl-cert" : [],
"rubygems" : [],
"git" : [],
"s3cmd" : []
}
},
"files" : {
"/home/ubuntu/.s3cfg" : {
"content" : { "Fn::Join" : ["", [
"[default]\n",
"access_key = ", { "Ref" : "HostKeys" }, "\n",
"secret_key = ", {"Fn::GetAtt": ["HostKeys", "SecretAccessKey"]}, "\n",
"use_https = True\n"
]]},
"mode" : "000644",
"owner" : "ubuntu",
"group" : "ubuntu"
}
}
}
}
},
"Properties": {
"Tags" : [ {
"Key" : "Name",
"Value" :{ "Ref": "NameTag" }
},
{
"Key" : "Group",
"Value" : { "Ref": "GroupTag" }
}
],
"SecurityGroups": [ { "Ref": "EdxServerSecurityGroup" } ],
"ImageId": { "Fn::FindInMap": [ "AWSRegionArch2AMI", { "Ref": "AWS::Region" }, { "Fn::FindInMap": [ "AWSInstanceType2Arch", { "Ref": "InstanceType" }, "Arch" ] } ]
},
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash\n",
"function error_exit\n",
"{\n",
" cfn-signal -e 1 -r \"$1\" '", { "Ref" : "EdxServerWaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"apt-get -y install python-setuptools\n",
"echo \"Python Tools installed\" - `date` >> /home/ubuntu/cflog.txt\n",
"easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
"echo \"Cloudformation Boostrap installed \" - `date` >> /home/ubuntu/cflog.txt\n",
"cfn-init --region ", { "Ref" : "AWS::Region" },
" -s ", { "Ref" : "AWS::StackId" }, " -r EdxServer ",
" --access-key ", { "Ref" : "HostKeys" },
" --secret-key ", {"Fn::GetAtt": ["HostKeys", "SecretAccessKey"]}, " || error_exit 'Failed to run cfn-init'\n",
"echo \"cfn-init run \" - `date` >> /home/ubuntu/cflog.txt\n",
"# If all went well, signal success\n",
"cfn-signal -e $? -r 'Edx Server configuration' '", { "Ref" : "EdxServerWaitHandle" }, "'\n"
]]}},
"KeyName": { "Ref": "KeyName" },
"InstanceType": { "Ref": "InstanceType" }
}
},
"EdxServerSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Open up SSH access plus Edx Server required ports",
"SecurityGroupIngress" : [
{ "IpProtocol": "tcp", "FromPort": "22", "ToPort": "22", "CidrIp": { "Ref" : "SSHLocation"} },
{ "IpProtocol": "tcp", "FromPort": "4000", "ToPort": "4000", "SourceSecurityGroupName": { "Ref" :"EdxClientSecurityGroup" }},
{ "IpProtocol": "tcp", "FromPort": "4040", "ToPort": "4040", "CidrIp": "0.0.0.0/0"}
]
}
},
"EdxClientSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Group with access to Edx Server"
}
},
"EdxServerWaitHandle" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle"
},
"EdxServerWaitCondition" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "EdxServer",
"Properties" : {
"Handle" : { "Ref" : "EdxServerWaitHandle" },
"Timeout" : "1200"
}
}
},
"Outputs" : {
"EdxSecurityGroup" : {
"Description" : "EC2 Security Group with access to the Edx server",
"Value" : { "Ref" :"EdxClientSecurityGroup" }
}
}
}
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample Template EC2_Instance_With_Block_Device_Mapping: Example to show how to attach EBS volumes and modify the root device using EC2 block device mappings. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",
"Parameters" : {
"InstanceType" : {
"Description" : "WebServer EC2 instance type",
"Type" : "String",
"Default" : "m1.small",
"AllowedValues" : [ "t1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m3.xlarge","m3.2xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","c1.medium","c1.xlarge","cc1.4xlarge","cc2.8xlarge","cg1.4xlarge","hi1.4xlarge","hs1.8xlarge"],
"ConstraintDescription" : "must be a valid EC2 instance type."
},
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the web server",
"Type" : "String"
},
"SSHFrom": {
"Description": "Lockdown SSH access to the bastion host (default can be accessed from anywhere)",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "0.0.0.0/0",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "must be a valid CIDR range of the form x.x.x.x/x."
}
},
"Mappings" : {
"AWSInstanceType2Arch" : {
"t1.micro" : { "Arch" : "PV64" },
"m1.small" : { "Arch" : "PV64" },
"m1.medium" : { "Arch" : "PV64" },
"m1.large" : { "Arch" : "PV64" },
"m1.xlarge" : { "Arch" : "PV64" },
"m3.xlarge" : { "Arch" : "PV64" },
"m3.2xlarge" : { "Arch" : "PV64" },
"m2.xlarge" : { "Arch" : "PV64" },
"m2.2xlarge" : { "Arch" : "PV64" },
"m2.4xlarge" : { "Arch" : "PV64" },
"c1.medium" : { "Arch" : "PV64" },
"c1.xlarge" : { "Arch" : "PV64" },
"cc1.4xlarge" : { "Arch" : "CLU64" },
"cc2.8xlarge" : { "Arch" : "CLU64" },
"cg1.4xlarge" : { "Arch" : "GPU64" },
"hi1.4xlarge" : { "Arch" : "PV64" },
"hs1.8xlarge" : { "Arch" : "PV64" }
},
"AWSRegionArch2AMI" : {
"us-east-1" : { "PV64" : "ami-3c994355", "CLU64" : "ami-08249861", "GPU64" : "ami-02f54a6b" },
"us-west-2" : { "PV64" : "ami-20800c10", "CLU64" : "ami-2431bf14", "GPU64" : "NOT_YET_SUPPORTED" },
"us-west-1" : { "PV64" : "ami-87712ac2", "CLU64" : "NOT_YET_SUPPORTED", "GPU64" : "NOT_YET_SUPPORTED" },
"eu-west-1" : { "PV64" : "ami-c37474b7", "CLU64" : "ami-d97474ad", "GPU64" : "ami-1b02026f" },
"ap-southeast-1" : { "PV64" : "ami-a6a7e7f4", "CLU64" : "NOT_YET_SUPPORTED", "GPU64" : "NOT_YET_SUPPORTED" },
"ap-southeast-2" : { "PV64" : "ami-bd990e87", "CLU64" : "NOT_YET_SUPPORTED", "GPU64" : "NOT_YET_SUPPORTED" },
"ap-northeast-1" : { "PV64" : "ami-4e6cd34f", "CLU64" : "NOT_YET_SUPPORTED", "GPU64" : "NOT_YET_SUPPORTED" },
"sa-east-1" : { "PV64" : "ami-1e08d103", "CLU64" : "NOT_YET_SUPPORTED", "GPU64" : "NOT_YET_SUPPORTED" }
}
},
"Resources" : {
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
{ "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
"KeyName" : { "Ref" : "KeyName" },
"InstanceType" : { "Ref" : "InstanceType" },
"SecurityGroups" : [{ "Ref" : "Ec2SecurityGroup" }],
"BlockDeviceMappings" : [
{
"DeviceName" : "/dev/sda1",
"Ebs" : { "VolumeSize" : "50" }
},{
"DeviceName" : "/dev/sdm",
"Ebs" : { "VolumeSize" : "100" }
}
]
}
},
"Ec2SecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "HTTP and SSH access",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22", "ToPort" : "22",
"CidrIp" : { "Ref" : "SSHFrom" }
} ]
}
}
},
"Outputs" : {
"Instance" : {
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "PublicDnsName" ] },
"Description" : "DNS Name of the newly created EC2 instance"
}
}
}
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample Template RDS_MySQL_55_With_Tags: Sample template showing how to create an RDS DBInstance version 5.5 with tags and alarming on important metrics that indicate the health of the database **WARNING** This template creates an Amazon Relational Database Service database instance and Amazon CloudWatch alarms. You will be billed for the AWS resources used if you create a stack from this template.",
"Parameters": {
"DBName": {
"Default": "MyDatabase",
"Description" : "The database name",
"Type": "String",
"MinLength": "1",
"MaxLength": "64",
"AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
},
"DBUser": {
"NoEcho": "true",
"Description" : "The database admin account username",
"Type": "String",
"MinLength": "1",
"MaxLength": "16",
"AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
},
"DBPassword": {
"NoEcho": "true",
"Description" : "The database admin account password",
"Type": "String",
"MinLength": "1",
"MaxLength": "41",
"AllowedPattern" : "[a-zA-Z0-9]*",
"ConstraintDescription" : "must contain only alphanumeric characters."
},
"DBAllocatedStorage": {
"Default": "5",
"Description" : "The size of the database (Gb)",
"Type": "Number",
"MinValue": "5",
"MaxValue": "1024",
"ConstraintDescription" : "must be between 5 and 1024Gb."
},
"DBInstanceClass": {
"Default": "db.m1.small",
"Description" : "The database instance type",
"Type": "String",
"AllowedValues" : [ "db.m1.small", "db.m1.large", "db.m1.xlarge", "db.m2.xlarge", "db.m2.2xlarge", "db.m2.4xlarge" ],
"ConstraintDescription" : "must select a valid database instance type."
}
},
"Mappings" : {
"InstanceTypeMap" : {
"db.m1.small" : {
"CPULimit" : "60",
"FreeStorageSpaceLimit" : "1024",
"ReadIOPSLimit" : "100",
"WriteIOPSLimit" : "100"
},
"db.m1.large" : {
"CPULimit" : "60",
"FreeStorageSpaceLimit" : "1024",
"ReadIOPSLimit" : "100",
"WriteIOPSLimit" : "100"
},
"db.m1.xlarge" : {
"CPULimit" : "60",
"FreeStorageSpaceLimit" : "1024",
"ReadIOPSLimit" : "100",
"WriteIOPSLimit" : "100"
},
"db.m2.xlarge" : {
"CPULimit" : "60",
"FreeStorageSpaceLimit" : "1024",
"ReadIOPSLimit" : "100",
"WriteIOPSLimit" : "100"
},
"db.m2.2xlarge" : {
"CPULimit" : "60",
"FreeStorageSpaceLimit" : "1024",
"ReadIOPSLimit" : "100",
"WriteIOPSLimit" : "100"
},
"db.m2.4xlarge" : {
"CPULimit" : "60",
"FreeStorageSpaceLimit" : "1024",
"ReadIOPSLimit" : "100",
"WriteIOPSLimit" : "100"
}
}
},
"Resources" : {
"MyDB" : {
"Type" : "AWS::RDS::DBInstance",
"Properties" : {
"DBName" : { "Ref" : "DBName" },
"AllocatedStorage" : { "Ref" : "DBAllocatedStorage" },
"DBInstanceClass" : { "Ref" : "DBInstanceClass" },
"Engine" : "MySQL",
"EngineVersion" : "5.5",
"MasterUsername" : { "Ref" : "DBUser" },
"MasterUserPassword" : { "Ref" : "DBPassword" },
"Tags" : [{
"Key" : "Name",
"Value" : "My SQL Database"
}]
},
"DeletionPolicy" : "Snapshot"
}
},
"Outputs" : {
"JDBCConnectionString": {
"Description" : "JDBC connection string for database",
"Value" : { "Fn::Join": [ "", [ "jdbc:mysql://",
{ "Fn::GetAtt": [ "MyDB", "Endpoint.Address" ] },
":",
{ "Fn::GetAtt": [ "MyDB", "Endpoint.Port" ] },
"/",
{ "Ref": "DBName" }]]}
},
"DBAddress" : {
"Description" : "Address of database endpoint",
"Value" : { "Fn::GetAtt": [ "MyDB", "Endpoint.Address" ] }
},
"DBPort" : {
"Description" : "Database endpoint port number",
"Value" : { "Fn::GetAtt": [ "MyDB", "Endpoint.Port" ] }
}
}
}
[localhost]
127.0.0.1 ansible_python_interpreter=/home/jarv/.virtualenvs/sys/bin/python
- hosts: tag_Name_edx-tst1
sudo: yes
user: ubuntu
tasks:
- user: name=jarv comment="John Jarvis"
- action: file dest=/home/jarv/.ssh state=directory
- copy:
content: |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHv6xeLcSBJfMwKKNvFry39aewwu8Mim/ZDCTMe5KQIAajuEQfsnlzOG2mmnyIAn8TcW6VnT5z0dDffK/6ZGpiW9xRREpy40+vuP/bd2kuAiTyvLn1F6qKPrfIQhTYJrrvg2c1K9NSeu8rwQaZc3ApXC55GAmyY+fP4RTL5c6GMC/LiaMo0nvsPDdAxZVrkvc+0VK6QQiLz0LJc0W7KrtOUpcqzrye7z8FLbWcIAgKoAUdpGNs9TPNL/gy8H8ArwjW7zSfrvH8NAiLUtI/brNobC7vPoo6MkQgpGcFbOKJUYMhFP6KrEJqv99EQXlf9NKfQcaFoC55oIzKdQHzAZTz jarv@madison
dest: /home/jarv/.ssh/authorized_keys
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment