Notes on Devops systems, including VirtualBox, Vagrant, Ansible, Docker. Installation instructions for Mac.
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).
Install with Homebrew’s Cask.
brew cask install virtualbox
brew cask install vagrant
brew cask install vagrant-manager
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
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 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 Files are just lists of hosts. Check what hosts are available with ansible all -m ping
.
/etc/ansible/hosts
or /usr/local/etc/ansible/hosts
export ANSIBLE_INVENTORY=~/ansible_hosts
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
With just an inventory file, you can run one off ansible commands directly like:
ansible all -a /bin/date
ansible all -m ping
Playbooks declare configurations, orchestrate steps across machines, and launch tasks (asynchronously and synchronously).
ansible-playbook site.yml
where site.yml is the playbook.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
Each play contains a list of tasks that are executed in order, one at a time.
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