- The document discusses using Ansible to automate and simplify various deployment and server management tasks like running commands across multiple servers, deploying code from git, and notifying services of changes.
- It recommends organizing tasks into reusable roles corresponding to applications and services, and using tags to selectively deploy certain components.
- The conclusion advocates provisioning new servers on EC2 using Ansible, and then using roles to automatically configure them.
2. So I want to batch run this thing...
Do the API servers in production have a clock skew
problem? Quick check.
ansible -i prod api -a date
3. These commands look similar...
Classic nodejs deploy:
ssh prod-api-1
cd /opt/myapp
git pull
npm install
sudo service myapp restart
… for each prod-api-* ...
4. Make them a playbook
- hosts: api
vars:
app: myapp
tasks:
- name: clone from git
git: repo=”git@github.com:bigpandaio/{{app}}" dest=”/opt/
{{app}}"
- name: npm install
command: npm install --production
- name: restart service
service: name=”{{app}}” state=restarted
sudo: yes
5. But wait! My deployment also needs...
HipChat notification
tasks:
- hipchat: room=ops token={{token}} msg=”
Starting deploy”
...rest of playbook...
6. But wait! My deployment also needs...
Remove from ELB:
tasks:
- local_action:
module: ec2_elb
region: “{{region}}”
instance_id: “{{ec2_id}}”
ec2_elbs: “{{elb_name}}”
state: absent
7. But wait! My deployment also needs...
Re-add to ELB:
...
- local_action:
module: ec2_elb
region: “{{region}}”
instance_id: “{{ec2_id}}”
ec2_elbs: “{{elb_name}}”
state: present
8. But wait! My deployment also needs...
Notify BigPanda (*wink* *tug*)
- bigpanda: component={{app}} version=
{{version}} state=started
…
- bigpanda: component={{app}} version=
{{version}} state=finished
11. Grouping tasks into components
● Ansible’s solution is roles
● A role can be an app, service, common settings
○ roles/app1
○ roles/app2
○ roles/rabbitmq
○ roles/mongodb
○ roles/maintenance_cronjobs
12. My apps’ roles look the same!
● Use a generic parametrized role
● roles/nodejs_app
○ notifies bigpanda
○ git pull {{app}}
○ npm install
○ service {{app}} restart
○ self test the {{app}} service
● Specific roles depend on it
13. Deploying to stage with same roles
Use a different inventory for prod and stage:
ansible-playbook -i prod api.yml
ansible-playbook -i stage api.yml
14. Deploy ALL THE THINGS!
site.yml:
- include: api.yml
- include: mongodb.yml
- include: frontend.yml
15. Deploy some of the things
Tag all of your tasks/roles with their relevant app/service
name
- { role: app1, tags: app1 }
- name: Generate configuration
template: src=config.j2 dest=/dest/path
tags: [ myservice_config, myservice ]
16. Deploy some of the things
Then you can:
ansible-playbook -i prod site.yml --tags
app1
Or even:
alias deploy-prod=’ansible-playbook -i prod
site.yml --tags’
deploy-prod app1
..aaahhhhh..
18. Provision a server in EC2
● The ec2 module creates new instances
● We have the rest of the config as roles
● Simple solution:
ansible-playbook -i prod ec2_create.yml -e
type=frontend
ansible-playbook -i prod site.yml --limit
frontend
19. Provision a DC
Same thing really:
for i in frontend api mongodb; do
ansible-playbook -i prod ec2_create -e
type=$i
done
ansible-playbook -i prod site.yml