Implementing DevOps Using Vagrant with Ansible
Understanding DevOps
Provisioning Vagrant Machines
Integrating Vagrant with Ansible
Creating a Vagrant Development Environment
Completing the Vagrant DevOps Environment
2. Table of contents
( part A - Basics )
1. History
2. Introduction
a. Why ansible
b. Understanding YAML
3. Basics getting started
a. Setting up ansible
b. Managing configuration and Inventory
c. Ad-hoc commands
d. Working with modules
e. Understanding playbooks
f. Variables, includes, imports and facts
g. Understanding Jinja2 templates
4. Working with roles
○ Understanding role structure
○ Creating roles
○ Deploying roles with ansible galaxy
○ Using the ansible galaxy CLI utility
Iman Darabi
https://www.linkedin.com/in/imandarabi/
3. Table of contents
( part B - DevOps )
1. Implementing DevOps Using Vagrant with Ansible
2. Understanding DevOps
3. Provisioning Vagrant Machines
4. Integrating Vagrant with Ansible
5. Creating a Vagrant Development Environment
6. Completing the Vagrant DevOps Environment
7.
Iman Darabi
https://www.linkedin.com/in/imandarabi/
4. Understanding DevOps
● DevOps tends to integrate developers and operations departments
● The goal is to build and manage essential components with automated programmatic
procedures
● Infrastructure as Code (IaC) is a key component
●
5. Infrastructure as a Code
● infrastructure as code replaces manual infrastructure operations
● Manual operations are replaced with code
● As a result it’s easy to consistently deploy and replicate operations throughout an
environment
● Ansible is an IaC solution, which automates server configuration and software installation
● A challenge is how to manage this infrastructure code
●
6. Managing IaC
● A version control system such as Git should be used to manage different versions of
Ansible code
● Using version control, the different states of infrastructure code can be managed
○ Development
○ Production
● Doing so allows administrators to test code before taking it to production
7. Vagrant
● It is essential to ensure that code in the test environment is the same as code in a
production environment
● Vagrant can be used to streamline creation and configuration of virtual development
environments
● Using its own language, Vagrant manages virtualization software such as KVM,
VirtualBox, and VMware through providers
● Vagrant can also interact with Ansible, Puppet, Salt, and Chef
● Vagrant automate VM creation, including hardware settings, software installation and
system configuration
● Vagrant can be used to create the basic Ansible managed environment
8. Vagrant Components
● Vagrant consist of two major components: Vagrant and Box
● Vagrant is what automates the build and configuration of virtual machines
● It’s available as a separate download at vagrantup.com
● A box is a tar file that contains a VM image
○ The image should contain just a base OS install
○ The base image can be used as a starting point for creating different VMs
● Public preconfigured Box images are available at vagrantcloud.com
● The Vagrantfile is a plain text file containing the instructions for creating the Vagrant
environment
●
9. Creating Vagrantfile
● A Vagrantfile always contains the following minimal contents
○ The Box image file to use
○ The URL where the Box image file is found
○ The target hostname
● Vagrant file example:
○ Vagrant.configure(2) do |config| config.vm.box = “rhel7.1”
○ Config.vm.box_url =
○ “http://vagrant.example.com/ansible2.0/x86_64/dvd/vagrant/rhel-server-libvirt-7.1x86_64.box”
○ Config.vm.hostname = “vagrant1.example.com”
○ end
10. Managing Vagrant Machines
● vagrant up : from the root of the project directory to bring up the target VM
● vagrant ssh: connects as the vagrant user on the target machine
● vagrant halt: stop VM
● Vagrant destroy: stop and cleanup a VM
11. Vagrant Synced folders
● A synced folder copies the contents of the project directory to a directory ~/sync/ on the
Vagrant machine
● Make sure that rsync is installed for this to work
12. Vagrant provisioning
● Vagrant provisioning uses the base
Box image and applies software
installation and configuration
updates as an overlay to that
● Provisioners are called from
Vagrantfile, using
“configvm.provision”
● Different types of provisioners are
available, the shell provisioner is the
most basic one
Vagrant.configure(2) do |config|
… Configuration omitted …
Config.vm.provision “shell”, inline: <<- SHELL
Sudo cp
/home/vagrant/sync/etc/yum.repos.d/*
/etc/yum.repos.d
SHELL
… Configuration omitted …
14. Example
● Create a project directory:
○ # mkdir -p ~/vagrant/test1
● In this directory, create a Vagrantfile with the following contents
○ Vagrant.configure(“2”) do |config|
○ config.vm.box = “ubuntu/2004”
○ End
● Start VM: # vagrant up [--provider=libvirt]
● Ssh to VM: # vagrant ssh <image>
● Stop VM: # vagrant destroy
16. Understanding Provisioner Types
● The bash provisioner can be used to run shell code
● The ansible provisioner runs Ansible on the Vagrant host
○ Ansible works as control node
○ Vagrant machines work as managed hosts
● The ansible_local provisioner runs Ansible on the Vgrant machines
●
17. Configuring Vagrant for Ansible Provisioning
● The example below shows how the ansible provisioner is used to run the playbook.yml file
after deploying the base OS
● Make sure that the playbook.yml is available in the current directory
○ Vagrant.configure (2) do |config|
○ …
○ config.vm.provision “ansible” do | ansible”
○ ansible.playbook = ‘playbook.yml’
○ end
○ …
○ End
● If you want to run playbooks again without rebuilding VM run:
○ # vagrant provision
19. Understanding Further Integration
● In a development environment, you need code versions to be managed on the Vagrant
machine
● Ansible has different source control modules that work with version control software like
Git and Subversion
● You can use it, for instance, to populate a web application Document root using the git
module in Ansible
21. Using Forwarded Ports
● To make a deployed application accessible, Vagrant can use forwarded ports
● Forwarded ports map network ports on the host system to ports on the Vagrant machine
and thus make it accessible
● Vagrant.configure(2) do |config|
● …
● config.vm.network: forwarded_port, guest: 80, host: 8080
● end
22. Version Control and IaC
● Place Vagrant + Ansible in a version control system to manage it in an easy way
● Just put the Vagrant project directory in Git to make it happen
● New administrators then just have to use git clone to make the software locally available,
followed by vagrant up to recreate the Vagrant development environment
● Just make sure the Git repository has the most recent version of the code
● This solution allows for a clean separation of tasks, where developers take care of code,
and operations takes care of new software versions
23. Lab: combining Vagrant and Ansible
● Create a Vagrantfile to install the latest version of ubuntu as a machine with the name
lab.example.com
● From the Vagrantfile, call an Ansible playbook that installs the Apache webserver and
opens the firewall to allow access to the webserver
● Configure Vagrant port forwarding such that port 8000 on the Vagrant host forwards
traffic to the Apache process on the Vagrant installed machine