Vagrant & Ansible <3



By Kamil Gałuszka from 

S4F and Hackerspace Activist

What is Vagrant?

  1. Vagrant helps you maintain the same env for every developer. And production of course.
  2. Vagrant works with VMWare, VirtualBox  :)
  3. Vagrant easily integrate with 
  • Ansible
  • Puppet
  • Chef
  • plain bash scripts :)

What is Ansible?

  1. Ansible is configuration and automation deployment tool
  2. Ansible has simple yml syntax
  3. Concepts in ansible:

  • Playbook
  • Host inventory
  • Roles
  • Group vars

How this help frontend developer?


  • You can work with different backend technologies from Python, Ruby, PHP, Java. Every technology has different configurations problems.
  • You need to setup:
  1. Database
  2. System packages
  3. Different app dependencies
  4. Other services that app depends on like

  • search server
  • message queue
  • supervisor
  • web server 
  • itd.

Vagrant file


Vagrant.configure("2") do |config|
  config.vm.box = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"
  config.vm.network :private_network, ip: "192.168.10.10"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.provision "ansible" do |ansible|
    inventory_files = File.expand_path("playbook/inventory/vagrant/files/", File.dirname(__FILE__))
    ansible.host_key_checking = false
    ansible.playbook = "playbook/playbook_vagrant.yml"
    ansible.inventory_path = "playbook/inventory/vagrant/hosts"
    ansible.extra_vars = { inventory_files: inventory_files }
    ansible.verbose = "v"
  end
end
   

Example Ansible playbook.



- name: Configure all web servers
  hosts: webservers
  sudo: yes
  roles:
    - { role: common }
    - { role: postgresql }
    - { role: elasticsearch }

- name: Configure all web servers
  hosts: webservers
  sudo: yes
  roles:
    - { role: common }
    - { role: python }
    - { role: app_vagrant }   

Example Ansible role

- name: Install Packages
  apt: pkg={{item}} state=installed update_cache=yes
  with_items:
    - postgresql
    - postgresql-9.3
    - postgresql-contrib-9.3

- name: Create Database User
  sudo_user: postgres
  postgresql_user:  user={{database.user}} password={{database.password}} >
    role_attr_flags=CREATEDB,NOSUPERUSER

- name: Create Database.
  sudo_user: postgres
  postgresql_db: >
    name={{database.name}}
    owner={{database.user}}
    login_host=localhost
    login_user={{database.user}}
    login_password={{database.password}}    

Example ansible vars

Host
[webservers]
192.168.10.10

[dbservers]
192.168.10.10
group_vars
httpd_port: 80
repository:
  location: ssh://git@bitbucket.org:repo.git
project:
  name: test
  user: www-data
  group: www-data
  mode: live
  root: /var/www/test/
  venv: /var/www/env/
  source: /var/www/test/source/
database:
  name: test
  host: localhost
  port: 5432
  user: test
  password: test
    

Templates in Ansible

Role
- name: Create and compile /etc/circus.ini
  notify: restart circus
  template: >
    src=circus.ini.j2
    dest=/etc/circus.ini
Template
[circus]
statsd = 0
httpd = 0

[watcher:webapp]
uid = {{project.user}}
gid = {{project.group}}
cmd = {{project.venv}}bin/chaussette
args = --fd $(circus.sockets.websocket) {{project.name}}.wsgi_production.application
numprocesses = 4
virtualenv = {{project.venv}}
copy_env = True
working_dir = {{project.source}}work/
use_sockets = True

            
        

Handlers

Task

- name: Create and compile /etc/circus.ini
  notify: restart circus
  template: >
    src=circus.ini.j2
    dest=/etc/circus.ini

Handler


- name: restart circus
  service: name=circus state=restarted
        

Thanks