Skip to content

Instantly share code, notes, and snippets.

@pb8226
Last active October 26, 2015 22:45
Show Gist options
  • Save pb8226/165aafe9d5c8e1b12176 to your computer and use it in GitHub Desktop.
Save pb8226/165aafe9d5c8e1b12176 to your computer and use it in GitHub Desktop.
Ansible Blue/Green Deploy to Rackspace Playbook
---
- name: Get Load Balancer Info
gather_facts: False
hosts: webservers
tasks:
- name: Retrieve Load Balancer Information
local_action:
module: rax_clb
name: '{{load_balancer_name}}'
port: 80
protocol: HTTP
type: PUBLIC
timeout: 90
region: '{{rackspace_region}}'
wait: yes
state: present
credentials: /tmp/rackspace_credentials.ini
register: lb
- name: create old_web_servers host group
add_host: name={{ item.id }} ansible_ssh_host={{ item.address }}
ansible_ssh_user=root lb_id={{ lb.balancer.id }} host_id={{ item.id }}
groups=old_web_servers
with_items: lb.balancer.nodes
when: lb.balancer.nodes is defined
- name: Gather info about existing servers
hosts: old_web_servers
gather_facts: False
tasks:
- name: Get facts about servers
local_action:
module: rax_facts
address: "{{ansible_ssh_host}}"
region: '{{rackspace_region}}'
credentials: /tmp/rackspace_credentials.ini
- name: Set Rax Id
set_fact:
rax_id: "{{ rax_id }}"
when: rax_id is defined
- name: Build Cloud Servers
hosts: webservers
gather_facts: False
tasks:
- name: Create Network List For Non App Dynamics Monitored Environments
set_fact:
rackspace_networks:
- private
- public
- name: Server build request
local_action:
module: rax
name: "{{server_name}}"
flavor: 'performance1-8'
image: '{{rackspace_image}}'
wait: yes
state: present
region: '{{rackspace_region}}'
wait_timeout: 6000
count: '{{instances_count}}'
auto_increment: yes
group: "{{server_name}}"
networks: "{{rackspace_networks}}"
meta:
application: "Application Name"
role: "Node Server"
market: '{{app_market}}'
environment: '{{app_environment}}'
build: '{{rackspace_image}}'
credentials: /tmp/rackspace_credentials.ini
register: rax
- name: Add New Servers To new_webservers host group
hosts: webservers
gather_facts: False
tasks:
- local_action:
module: add_host
hostname: "{{ item.name }}"
ansible_ssh_host: "{{ item.rax_accessipv4 }}"
ansible_ssh_user: ssh_username
load_balancer_id: "{{lb.balancer.id}}"
rackspace_private_ip: "{{item.rax_networks.private[0]}}"
ansible_ssh_private_key_file: "{{rackspace_private_key_path}}"
groupname: new_webservers
with_items: rax.success
- name: Create Config File
hosts: webservers
connection: local
gather_facts: False
tasks:
# Copy Standard Config File
- template: src=./processes.j2 dest=./processes.json
- name: Start Server Processes
hosts: new_webservers
gather_facts: False
tasks:
# Make sure server is ready
- wait_for: port=22 delay=60
# Copy Standard Config File
- copy: src=./processes.json dest=/home/wwiatqaadmin/app/server/processes.json owner=wwiatqaadmin group=wwiatqaadmin mode=0644
# Start PM2
- command: /usr/bin/pm2 start processes.json
args:
chdir: /home/wwiatqaadmin/app/server
# Save PM2 State
- command: /usr/bin/pm2 save
args:
chdir: /home/wwiatqaadmin/app/server
- name: Add Rackspace Monitoring Agent Role
hosts: new_webservers
sudo: yes
roles:
- { role: cmx-cloud-monitoring-agent }
vars:
cloud_monitoring_cpu_alarm: True
cloud_monitoring_filesystem_alarm: True
cloud_monitoring_memory_alarm: True
- name: Update Load Balancer With New Nodes
hosts: new_webservers
gather_facts: False
serial: 1
tasks:
- name: Add Servers to Load Balancer
local_action:
module: rax_clb_nodes
load_balancer_id: '{{load_balancer_id}}'
address: "{{ rackspace_private_ip }}"
port: "3000"
condition: enabled
type: primary
wait_timeout: 6000
wait: yes
region: '{{rackspace_region}}'
credentials: /tmp/rackspace_credentials.ini
- name: Remove Old Webservers From Load Balancer
hosts: old_web_servers
gather_facts: False
serial: 1
tasks:
# Drain connections from a node
- name: Drain connections from load balancer
local_action:
module: rax_clb_nodes
load_balancer_id: '{{lb_id}}'
node_id: "{{host_id}}"
condition: draining
wait: yes
wait_timeout: 6000
region: '{{rackspace_region}}'
credentials: /tmp/rackspace_credentials.ini
ignore_errors: yes
# Remove a node from the load balancer
- name: Remove node from load balancer
local_action:
module: rax_clb_nodes
load_balancer_id: '{{lb_id}}'
node_id: "{{host_id}}"
state: absent
wait: yes
wait_timeout: 6000
region: '{{rackspace_region}}'
credentials: /tmp/rackspace_credentials.ini
ignore_errors: yes
- name: Terminate Old Webservers
hosts: old_web_servers
gather_facts: False
tasks:
# Terminate removed nodes
- local_action:
module: rax
instance_ids:
- "{{ rax_id }}"
wait: yes
wait_timeout: 6000
state: absent
region: '{{rackspace_region}}'
count: 0
exact_count: yes
credentials: /tmp/rackspace_credentials.ini
when: rax_id is defined
#sometimes rackspace servers get stuck while deleting
ignore_errors: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment