---
- name: Making preprations for the provisioning
hosts: localhost
connection: local
gather_facts: no
roles:
- workstation
- name: Launch an Ubuntu 14.04 EC2 instance
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Find the latest Ubuntu AMI
ec2_ami_search: distro=ubuntu release=trusty region=ap-southeast-1 store=ebs-ssd virt=hvm
register: ubuntu_image
- name: Start the new EC2 instance
ec2:
image: "{{ ubuntu_image.ami }}"
region: ap-southeast-1
zone: ap-southeast-1b
instance_type: t2.small
vpc_subnet_id: subnet-xxxxxxxxx
group_id: ['sg-xxxxxxxx', 'sg-xxxxxxxx']
key_name: blahblah-deploy
wait: yes
wait_timeout: 500
instance_tags:
Role: Blahblah Base AMI
register: ec2
- name: Add the new instance to host group
add_host: hostname={{ item.private_ip }} groupname=launched
with_items: ec2.instances
- name: Wait for SSH to come up on the new instance
wait_for: host={{ item.private_ip }} port=22 delay=60 timeout=320 state=started
with_items: ec2.instances
- name: Copy the code to the new instance
hosts: launched
user: ubuntu
sudo: true
tasks:
- synchronize: src=code/ dest=/tmp/code delete=yes archive=yes
- name: Provision the new instance to create the base AMI
hosts: launched
user: ubuntu
sudo: true
vars:
app_user: blahblah
app_root: /home/{{ app_user }}/apps
roles:
- nodesource.node
- basebox
- name: Remove the code from the instance before creating the AMI
hosts: launched
user: ubuntu
sudo: true
tasks:
- file: path=/tmp/code state=absent
- name: Create an AMI from the provisioned instance
hosts: localhost
connection: local
gather_facts: no
tasks:
- ec2_ami: region=ap-southeast-1 instance_id={{
item.id }} wait=no name=basebox-2000000 wait=yes wait_timeout=300
with_items:
- "{{ ec2.instances }}"
- name: Delete the instance now that the AMI is created
hosts: localhost
connection: local
gather_facts: no
tasks:
- ec2:
region: ap-southeast-1
instance_ids: "{{ ec2.instance_ids }}"
state: 'absent'