Vagrant & Ansible 
for local dev and production 
deployment 
Christoffer Kaalund : Everplaces 
@ChrisKaalund
= Local development tool
• Super simple to setup dev env 
• Fully reproducible 
• Having a setup that is as close to prod as possible 
• Works with Chef, Puppet, Ansible & Shell Scripts to 
provision your dev env
Vagrant, VirtualBox, Ansible
Application deployment 
& 
Configuration Management
• Agentless - push not pull 
• Simple to setup, read and understand 
• Wildly powerful 
• Easy to extent 
• Python
ansible-playbook -i production webserver.yml
WHERE 
staging 
dev 
ansible-playbook -i production webserver.yml
WHAT 
database.yml 
site.yml (all) 
ansible-playbook -i production webserver.yml
inventories/ 
dev 
production 
group_vars/ 
all/ 
main.yml 
keys.yml 
database 
dev 
production 
host_vars/ 
... 
site.yml 
deploy.yml 
roles/ 
database/ 
tasks/ 
main.yml 
handlers/ 
main.yml 
files/ 
foo.txt 
templates/ 
bar.j2 
vars/ 
main.yml 
web/ 
... 
memcached/ 
...
Inventories 
Playbooks 
inventories/ 
dev 
production 
group_vars/ 
all/ 
main.yml 
keys.yml 
database 
dev 
production 
host_vars/ 
... 
site.yml 
deploy.yml 
roles/ 
database/ 
tasks/ 
main.yml 
handlers/ 
main.yml 
files/ 
foo.txt 
templates/ 
bar.j2 
vars/ 
main.yml 
web/ 
... 
memcached/ 
...
Inventories 
inventories/ 
dev 
production 
group_vars/ 
all/ 
main.yml 
keys.yml 
database 
dev 
production 
host_vars/ 
... 
# file: inventories/production 
[web] 
4.2.2.1 
4.2.2.2 
[database] 
8.8.8.8 
[production:children] 
web 
database
Inventories 
inventories/ 
dev 
production 
group_vars/ 
all/ 
main.yml 
keys.yml 
database 
dev 
production 
host_vars/ 
... 
# file: inventories/production 
[web] 
4.2.2.1 
4.2.2.2 
[database] 
8.8.8.8 
[production:children] 
web 
database 
# file: inventories/dev 
[web] 
192.168.33.16 
[database] 
192.168.33.16 
[dev:children] 
web 
database
Inventories 
inventories/ 
dev 
production 
group_vars/ 
all/ 
main.yml 
keys.yml 
database 
dev 
production 
host_vars/ 
... 
# file: inventories/production 
[web] 
Group 
4.2.2.1 
4.2.2.2 
[database] 
8.8.8.8 
[production:children] 
web 
database
Inventories 
inventories/ 
dev 
production 
group_vars/ 
all/ 
main.yml 
keys.yml 
database 
dev 
production 
host_vars/ 
... 
# file: inventories/production 
[web] 
Group 
4.2.2.1 
Host 
4.2.2.2 
[database] 
8.8.8.8 
[production:children] 
web 
database
Inventories > variables 
inventories/ 
dev 
production 
group_vars/ 
all/ 
main.yml 
keys.yml 
database 
dev 
production 
host_vars/ 
... 
--- 
# file: group_vars/database 
db_name: "{{ project_name }}_db" 
db_user: "{{ project_name }}_pg" 
db_encoding: UTF8 
db_collate_and_ctype: en_US.UTF-8 
db_template: template0 
# file: inventories/production 
[web] 
4.2.2.1 
4.2.2.2 
[database] 
8.8.8.8 
[production:children] 
web 
database
ansible-playbook -i inventories/production site.yml
Playbooks 
Playbooks = a list of plays
Playbooks 
A play = running tasks on hosts
Playbooks 
- hosts: web 
tasks: 
- name: Ensure apache is at the latest version 
yum: pkg=httpd state=latest 
- name: Ensure apache is running 
service: name=httpd state=started
Playbooks 
Roles = groups of tasks
Playbooks 
# file: site.yml 
--- 
- hosts: database 
sudo: yes 
sudo_user: root 
roles: 
- database 
- hosts: web 
sudo: yes 
sudo_user: root 
roles: 
- memcached 
- webserver 
deploy.yml 
site.yml 
roles/ 
database/ 
tasks/ 
main.yml 
handlers/ 
main.yml 
files/ 
foo.txt 
templates/ 
bar.j2 
vars/ 
main.yml 
web/ 
... 
memcached/ 
...
ansible-playbook -i inventories/production site.yml
Playbooks > Roles 
roles/ 
database/ 
tasks/ 
main.yml 
handlers/ 
main.yml 
files/ 
foo.txt 
templates/ 
bar.j2 
vars/ 
main.yml
Inventory Playbooks 
Groups 
Hosts 
Roles 
Tasks 
Modules

Vagrant and ansible

  • 1.
    Vagrant & Ansible for local dev and production deployment Christoffer Kaalund : Everplaces @ChrisKaalund
  • 3.
  • 4.
    • Super simpleto setup dev env • Fully reproducible • Having a setup that is as close to prod as possible • Works with Chef, Puppet, Ansible & Shell Scripts to provision your dev env
  • 5.
  • 7.
    Application deployment & Configuration Management
  • 8.
    • Agentless -push not pull • Simple to setup, read and understand • Wildly powerful • Easy to extent • Python
  • 9.
  • 10.
    WHERE staging dev ansible-playbook -i production webserver.yml
  • 11.
    WHAT database.yml site.yml(all) ansible-playbook -i production webserver.yml
  • 12.
    inventories/ dev production group_vars/ all/ main.yml keys.yml database dev production host_vars/ ... site.yml deploy.yml roles/ database/ tasks/ main.yml handlers/ main.yml files/ foo.txt templates/ bar.j2 vars/ main.yml web/ ... memcached/ ...
  • 13.
    Inventories Playbooks inventories/ dev production group_vars/ all/ main.yml keys.yml database dev production host_vars/ ... site.yml deploy.yml roles/ database/ tasks/ main.yml handlers/ main.yml files/ foo.txt templates/ bar.j2 vars/ main.yml web/ ... memcached/ ...
  • 14.
    Inventories inventories/ dev production group_vars/ all/ main.yml keys.yml database dev production host_vars/ ... # file: inventories/production [web] 4.2.2.1 4.2.2.2 [database] 8.8.8.8 [production:children] web database
  • 15.
    Inventories inventories/ dev production group_vars/ all/ main.yml keys.yml database dev production host_vars/ ... # file: inventories/production [web] 4.2.2.1 4.2.2.2 [database] 8.8.8.8 [production:children] web database # file: inventories/dev [web] 192.168.33.16 [database] 192.168.33.16 [dev:children] web database
  • 16.
    Inventories inventories/ dev production group_vars/ all/ main.yml keys.yml database dev production host_vars/ ... # file: inventories/production [web] Group 4.2.2.1 4.2.2.2 [database] 8.8.8.8 [production:children] web database
  • 17.
    Inventories inventories/ dev production group_vars/ all/ main.yml keys.yml database dev production host_vars/ ... # file: inventories/production [web] Group 4.2.2.1 Host 4.2.2.2 [database] 8.8.8.8 [production:children] web database
  • 18.
    Inventories > variables inventories/ dev production group_vars/ all/ main.yml keys.yml database dev production host_vars/ ... --- # file: group_vars/database db_name: "{{ project_name }}_db" db_user: "{{ project_name }}_pg" db_encoding: UTF8 db_collate_and_ctype: en_US.UTF-8 db_template: template0 # file: inventories/production [web] 4.2.2.1 4.2.2.2 [database] 8.8.8.8 [production:children] web database
  • 19.
  • 20.
    Playbooks Playbooks =a list of plays
  • 21.
    Playbooks A play= running tasks on hosts
  • 22.
    Playbooks - hosts:web tasks: - name: Ensure apache is at the latest version yum: pkg=httpd state=latest - name: Ensure apache is running service: name=httpd state=started
  • 23.
    Playbooks Roles =groups of tasks
  • 24.
    Playbooks # file:site.yml --- - hosts: database sudo: yes sudo_user: root roles: - database - hosts: web sudo: yes sudo_user: root roles: - memcached - webserver deploy.yml site.yml roles/ database/ tasks/ main.yml handlers/ main.yml files/ foo.txt templates/ bar.j2 vars/ main.yml web/ ... memcached/ ...
  • 25.
  • 26.
    Playbooks > Roles roles/ database/ tasks/ main.yml handlers/ main.yml files/ foo.txt templates/ bar.j2 vars/ main.yml
  • 27.
    Inventory Playbooks Groups Hosts Roles Tasks Modules