Vagrant Provisioning with Ansible

Vagrant Provisioning with Ansible

As presented at PHP Southcoast 2015

719435d98d452de7ac367c828266cf01?s=128

Erika Heidi

July 18, 2015
Tweet

Transcript

  1. None
  2. whoami

  3. What to expect from this talk 1. Vagrant: quick recap

    2. Ansible Overview 3. Playbook crash-course 4. Standalone Ansible
  4. VAGRANT: QUICK RECAP

  5. None
  6. ANSIBLE OVERVIEW

  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!
  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.
  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
  10. Ansible Output

  11. Ansible Output (with cowsay)

  12. 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
  13. DEMO

  14. None
  15. WRITING PLAYBOOKS

  16. Variables --- - hosts: all sudo: yes vars: web_server: nginx

    tasks: - name: Install {{ web_server }} apt: pkg={{ web_server }} state=latest
  17. Facts

  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
  19. Looping: with_items tasks: - name: Install Packages apt: pkg={{ item

    }} state=latest with_items: - nginx - php5-fpm - git
  20. 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
  21. Templates <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot {{ doc_root }} <Directory

    {{ doc_root }}> AllowOverride All Require all granted </Directory> </VirtualHost>
  22. Templates - Usage - name: Change default apache vhost template:

    src=templates/apache.tpl dest=/etc/apache2/sites-available/000-default.conf
  23. 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
  24. 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
  25. None
  26. ORGANIZING PLAYBOOKS

  27. 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
  28. 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
  29. STANDALONE ANSIBLE

  30. 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
  31. 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
  32. ad-hoc commands

  33. ad-hoc commands

  34. 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
  35. Running Ansible on Vagrant vms $ ansible all -i [inventory]

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

  37. DEMO

  38. RESOURCES

  39. phansible.com

  40. None
  41. Vagrant Cookbook - Leanpub leanpub.com/vagrantcookbook/c/phpsc15 Also available on Amazon (paperback)

  42. QUESTIONS?

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