William Liu

Ansible and Vagrant


Summary

Notes on Devops systems, including VirtualBox, Vagrant, Ansible, Docker. Installation instructions for Mac.

Vagrant and VirtualBox

Vagrant helps create and configure lightweight, reproducible, and portable development environments.

VirtualBox is separate, but often goes with Vagrant because its virtualization for hardware (usually for servers).

Setup Vagrant and Virtualbox

Install with Homebrew’s Cask.

Vagrant Useful Commands

Useful commands include:

vagrant init centos/7 to initialize vagrant with centos/7 box, with boxes that can be found at hashicorp vagrant up to bring vagrant up and running vagrant box add centos/7 to add a box vagrant ssh to ssh into the machine vagrant reload --provision to provision the server

Vagrantfile

Wherever you did the init with Vagrant is your root directory. You should see a hidden .vagrant directory and a Vagrantfile.

You can configure the Vagrantfile, e.g. setup the provisioning like:

Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
  end
end

Ansible

Ansible helps automate provisioning, configuration management, and application deployment using an agentless architecture.

Ansible does this by setting up playbooks that contain plays, tasks and handlers. We can also assign roles and variables.

###Installing Ansible

Install Ansible with pip install ansible (will also install other libraries).

Inventory File

Inventory Files are just lists of hosts. Check what hosts are available with ansible all -m ping.

Here’s a sample inventory file (has an INI-like format):

mail.jobwaffle.com:5309

[webservers]
foo.jobwaffle.com
bar.jobwaffle.com

[dbservers]
db1.jobwaffle.com
db2.jobwaffle.com 

Run ad-hoc Ansible commands

With just an inventory file, you can run one off ansible commands directly like:

Playbooks

Playbooks declare configurations, orchestrate steps across machines, and launch tasks (asynchronously and synchronously).

Hosts and Users

Hosts determine which machines to target (e.g. dbservers, webservers). This is marked by hosts: something Roles (aka Users) determine which role or user to run as executing a task (e.g. root). This is marked by say remote_user: root

Example Usage:

---
- hosts: webservers
  remote_user: root

Tasks

Each play contains a list of tasks that are executed in order, one at a time.

Handlers

Playbooks have a basic event system that can respond to change only once; this system is called handlers and they are called by other plays. Handlers are triggered by tasks

An example handler might be to restart httpd:

handlers:
- name: restart apache
  service: name=httpd state=restarted