Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vagrant Provisioning with Ansible

Erika Heidi
February 07, 2015

Vagrant Provisioning with Ansible

Talk presented at SunshinePHP 2015

Erika Heidi

February 07, 2015
Tweet

More Decks by Erika Heidi

Other Decks in Programming

Transcript

  1. View Slide

  2. whoami

    (PHP) Developer Advocate at
    DigitalOcean

    Working with PHP and Linux
    for 10+ years

    Author of Vagrant Cookbook
    and phansible.com

    View Slide

  3. What to expect from this talk
    1. Vagrant: quick recap
    2. Ansible Overview
    3. Writing Playbooks
    4. Standalone Ansible

    View Slide

  4. VAGRANT:
    QUICK RECAP

    View Slide

  5. View Slide

  6. ANSIBLE
    OVERVIEW

    View Slide

  7. Ansible Overview

    Simple and Straightforward

    Human-readable automation
    language

    Agentless - needs only SSH

    Extensive list of built-in modules

    Used by Twitter, Atlassian, EA,
    Spotify, even NASA!

    View Slide

  8. Installation
    $ brew update
    $ brew install ansible
    $ sudo apt-add-repository -y ppa:ansible/ansible
    $ sudo apt-get update
    $ sudo apt-get install -y ansible
    Detailed installation instructions: do.co/ansible-docs
    Mac OSX
    Ubuntu
    *Windows is not officially supported as controller machine.

    View Slide

  9. A Simple Playbook
    # playbook.yml
    ---
    - hosts: all
    sudo: true
    tasks:
    - name: Update apt-cache
    apt: update_cache=yes
    - name: Install Nginx
    apt: pkg=nginx state=latest

    View Slide

  10. Playbook x Manifest
    #Ansible playbook.yml
    ---
    - hosts: all
    sudo: true
    tasks:
    - name: Update apt-cache
    apt: update_cache=yes
    - name: Install Nginx
    apt: pkg=nginx state=latest
    #Puppet default.pp
    exec { 'apt-get update':
    command => '/usr/bin/apt-get update'
    }
    package { 'nginx':
    ensure => "installed",
    require => Exec['apt-get update'],
    }

    View Slide

  11. Ansible Output

    View Slide

  12. Ansible Output (with cowsay)

    View Slide

  13. Ansible as Provisioner
    #Vagrantfile
    Vagrant.configure("2") do |config|
    config.vm.box = "hashicorp/precise64"
    config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    end
    end

    View Slide

  14. DEMO

    View Slide

  15. WRITING
    PLAYBOOKS

    View Slide

  16. Variables
    ---
    - hosts: all
    sudo: yes
    vars:
    web_server: nginx
    tasks:
    - name: Install {{ web_server }}
    apt: pkg={{ web_server }} state=latest

    View Slide

  17. Facts

    View Slide

  18. Conditionals
    - name: "shutdown Debian flavored systems"
    command: /sbin/shutdown -t now
    when: ansible_os_family == "Debian"
    - name: foo is not defined
    fail: msg="Bailing out. this play requires 'bar'"
    when: bar is not defined

    View Slide

  19. Conditionals
    - name: Check if PHP is installed
    register: php_install
    command: php -v
    ignore_errors: true
    - name: Do something if PHP is installed
    debug: var=php_install
    when: php_install|success
    - name: Do something if PHP is NOT installed
    debug: msg='PHP is NOT installed!'
    when: php_install|failed

    View Slide

  20. Conditionals
    - name: Check if PHP is installed
    register: php_install
    command: php -v
    ignore_errors: true
    - name: Do something if PHP is installed
    debug: var=php_install
    when: php_install|success
    - name: Do something if PHP is NOT installed
    debug: msg='PHP is NOT installed!'
    when: php_install|failed

    View Slide

  21. Conditionals
    - name: Check if PHP is installed
    register: php_install
    command: php -v
    ignore_errors: true
    - name: Do something if PHP is installed
    debug: var=php_install
    when: php_install|success
    - name: Do something if PHP is NOT installed
    debug: msg='PHP is NOT installed!'
    when: php_install|failed

    View Slide

  22. View Slide

  23. View Slide

  24. Looping: with_items
    tasks:
    - name: Install Packages
    apt: pkg={{ item }} state=latest
    with_items:
    - nginx
    - php5-fpm
    - git

    View Slide

  25. Looping: with_items
    ---
    - hosts: all
    sudo: yes
    vars:
    sys_packages: [ 'nginx', 'php5-fpm', 'git' ]
    tasks:
    - name: Install Packages
    apt: pkg={{ item }} state=latest
    with_items: sys_packages

    View Slide

  26. Templates

    ServerAdmin webmaster@localhost
    DocumentRoot {{ doc_root }}

    AllowOverride All
    Require all granted


    View Slide

  27. Templates - Usage
    - name: Change default apache vhost
    template: src=templates/apache.tpl
    dest=/etc/apache2/sites-available/000-default.conf

    View Slide

  28. Handlers (services)
    ---
    - hosts: all
    sudo: yes
    vars:
    - doc_root: /vagrant
    tasks:
    - name: Change default apache vhost
    template: src=templates/apache.tpl dest=/etc/apache2/sites-
    available/000-default.conf
    notify: restart apache
    handlers:
    - name: restart apache
    service: name=apache2 state=restarted

    View Slide

  29. View Slide

  30. ORGANIZING
    PLAYBOOKS

    View Slide

  31. Including Tasks
    ---
    - hosts: all
    sudo: true
    vars:
    doc_root: /vagrant/web
    tasks:
    - include: tasks/init.yml
    - include: tasks/nginxphp.yml
    handlers:
    - name: restart nginx
    service: name=nginx state=restarted

    View Slide

  32. Roles
    .
    ├── playbook.yml
    └── roles
    ├── init
    │ └── tasks
    │ └── main.yml
    └── nginxphp
    ├── handlers
    │ └── main.yml
    ├── tasks
    │ └── main.yml
    └── templates
    └── vhost.tpl
    #playbook.yml
    ---
    - hosts: all
    sudo: true
    vars:
    doc_root: /vagrant/web
    roles:
    - init
    - nginxphp

    View Slide

  33. STANDALONE
    ANSIBLE

    View Slide

  34. Let's talk inventories!
    #/etc/ansible/hosts
    [webservers]
    erikaheidi.com
    dev-human.com
    [testservers]
    178.62.192.53
    95.85.35.248
    178.62.221.111

    View Slide

  35. ad-hoc commands

    View Slide

  36. ad-hoc commands

    View Slide

  37. Running playbooks
    $ ansible-playbook -i staging -l webservers playbook.yml
    $ ansible-playbook playbook.yml --list-hosts
    $ ansible-playbook playbook.yml --list-tasks
    ansible-playbook [-i inventory] [-l group] playbook.yml

    View Slide

  38. Vagrant auto-generated inventory
    # Generated by Vagrant
    default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
    .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory

    View Slide

  39. Running Ansible on Vagrant vms
    $ ansible -i [inventory] --private-key=[vagrant_priv_key]
    -u vagrant -a "php -v"
    $ ansible-playbook -i [inventory] --private-key=[key] -u
    vagrant demo01.yml
    .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
    .vagrant/machines/default/virtualbox/private_key

    View Slide

  40. Running Ansible on Vagrant vms

    View Slide

  41. RESOURCES

    View Slide

  42. phansible.com

    View Slide

  43. Vagrant Cookbook - Leanpub
    leanpub.com/vagrantcookbook/c/ssp15
    Also available on Amazon (paperback)

    View Slide

  44. QUESTIONS?

    View Slide

  45. Ansible Tutorials:
    http://do.co/ansible
    Please rate this talk:
    https://joind.in/13459

    View Slide