Skip to content

Instantly share code, notes, and snippets.

@thom8
Created November 20, 2015 00:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thom8/6427608eba33fee17dfb to your computer and use it in GitHub Desktop.
Save thom8/6427608eba33fee17dfb to your computer and use it in GitHub Desktop.
ansible-playbook deploy-playbook.yml -e "project=<project>" -e "branch=<branch>"
---
- hosts: "tag_UID_{{ (branch+project) | checksum }}"
connection: local
gather_facts: no
tasks:
- name: Terminate existing instance
local_action: ec2
state=absent
instance_ids="{{ ec2_id }}"
region=ap-southeast-2
- hosts: localhost
connection: local
gather_facts: no
vars:
- dns_zones:
- domain1.com
- domain2.com
- branch_shortname: "{{ branch | basename }}"
- instance_name: "drupal.{{ project }}.{{ branch_shortname }}"
- instance_uid: "{{ (branch+project) | checksum }}"
tasks:
- name: Find current template
ec2_ami_find:
region: ap-southeast-2
owner: self
ami_tags:
Project: "{{ project }}"
no_result_action: fail
register: template
- name: Deploy instance
ec2:
region: ap-southeast-2
key_name: jenkins
instance_type: m3.medium
image: "{{ template.results[0].ami_id }}"
instance_tags:
Name: "{{ instance_name }}"
Project: "{{ project }}"
Branch: "{{ branch }}"
Service: Drupal
UID: "{{ instance_uid }}"
wait: yes
group: default
count: 1
register: ec2
- name: Add instance public IP to host group
add_host:
hostname: "{{ item.public_ip }}"
groups: "ec2hosts"
with_items: ec2.instances
- name: Wait for SSH to come up
wait_for: host={{ item.public_dns_name }} port=22 delay=20 timeout=320 state=started
with_items: ec2.instances
- name: Add DNS entry on route 53
route53:
command: create
zone: "{{ item }}"
record: "{{ branch_shortname }}.{{ project }}.{{ item }}"
overwrite: yes
type: A
ttl: 60
value: "{{ ec2.instances[0].public_ip }}"
with_items: dns_zones
- hosts: "ec2hosts"
become: yes
gather_facts: no
tasks:
# - name: Provision instance
# include: tasks/instance-provision.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment