├── README.md ├── hosts ├── provision.yml └── vars ├── aws-creds.yml └── dev-environment.yml /README.md: -------------------------------------------------------------------------------- 1 | ansible-aws-examples 2 | ==================== 3 | 4 | Example playbooks to launch AWS infrastructure. 5 | 6 | Steps: 7 | 8 | 1. Edit vars/aws-creds.yml to include your own AWS credentials 9 | 2. Edit vars/dev-environment.yml to define the security groups, 10 | sets of instances, etc, that will be used. 11 | 3. Run: ansible-playbook -i hosts provision.yml 12 | 13 | You can shut down the infrastructure by setting the exact_count values 14 | in the vars/dev-environment.yml file to 0 and re-running provision.yml. 15 | -------------------------------------------------------------------------------- /hosts: -------------------------------------------------------------------------------- 1 | [local] 2 | localhost 3 | -------------------------------------------------------------------------------- /provision.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Provision ec2 instances based on the environment 3 | hosts: localhost 4 | connection: local 5 | gather_facts: False 6 | 7 | vars_files: 8 | - vars/dev-environment.yml 9 | - vars/aws-creds.yml 10 | 11 | tasks: 12 | 13 | - name: Create required security groups 14 | ec2_group: 15 | name: "{{ item.name }}" 16 | description: "{{ item.desc }}" 17 | rules: "{{ item.rules }}" 18 | region: "{{ ec2_region }}" 19 | ec2_access_key: "{{ ec2_access_key }}" 20 | ec2_secret_key: "{{ ec2_secret_key }}" 21 | with_items: security_groups 22 | 23 | - name: Launch instances 24 | ec2: 25 | region: "{{ ec2_region }}" 26 | ec2_access_key: "{{ ec2_access_key }}" 27 | ec2_secret_key: "{{ ec2_secret_key }}" 28 | keypair: "{{ item.keypair }}" 29 | group: "{{ item.group }}" 30 | instance_type: "{{ item.instance_type }}" 31 | image: "{{ item.image }}" 32 | instance_tags: "{{ item.instance_tags }}" 33 | exact_count: "{{ item.exact_count }}" 34 | count_tag: "{{ item.count_tag }}" 35 | wait: true 36 | register: ec2 37 | with_items: ec2_instances 38 | -------------------------------------------------------------------------------- /vars/aws-creds.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # this file can be encrypted with ansible-vault 3 | 4 | ec2_access_key: xxx 5 | ec2_secret_key: xxx 6 | -------------------------------------------------------------------------------- /vars/dev-environment.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # set these parameters to control the region, keypair, and AMI that are launched 3 | ec2_region: us-east-1 4 | instances_keypair: keypair-name 5 | image_id: ami-bf5021d6 6 | 7 | # security groups to be created 8 | security_groups: 9 | - name: ssh 10 | desc: the security group for the jumphost 11 | rules: 12 | - proto: tcp 13 | from_port: 22 14 | to_port: 22 15 | cidr_ip: 0.0.0.0/0 16 | 17 | - name: web 18 | desc: the security group for the web server 19 | rules: 20 | - proto: tcp 21 | from_port: "80" 22 | to_port: "80" 23 | cidr_ip: 0.0.0.0/0 24 | - proto: tcp 25 | from_port: "443" 26 | to_port: "443" 27 | cidr_ip: 0.0.0.0/0 28 | 29 | # Instances to launch be launched. 30 | # If you re-run the playbook after modifying exact_count, 31 | # instances will be terminated if the actual count > exact_count, 32 | # or new instances will be launched if actual count < exact_count. 33 | 34 | ec2_instances: 35 | - instance_type: m1.small 36 | image: "{{ image_id }}" 37 | assign_public_ip: true 38 | keypair: "{{instances_keypair}}" 39 | group: ['ssh'] 40 | instance_tags: 41 | Name: dev_jumphost 42 | exact_count: 1 43 | count_tag: 44 | Name: dev_jumphost 45 | 46 | - instance_type: m1.small 47 | image: "{{ image_id }}" 48 | keypair: "{{instances_keypair}}" 49 | group: ['web', 'ssh'] 50 | instance_tags: 51 | Name: web 52 | exact_count: 1 53 | count_tag: 54 | Name: web 55 | --------------------------------------------------------------------------------