Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
303 lines (187 sloc) 6.65 KB

Vagrant, hypervisors, debian automated install and other rants

What is Vagrant?

  • Vagrant provides easy to configure, reproducible, and portable work environments.

Why Vagrant is useful?

  • Drafting and testing ansible scripts

Installing Vagrant

# apt-get install vagrant
  • This command will also pull virtualbox

Getting a Vagrant box

Downloading a box on the system

$ vagrant box add debian/jessie64

Starting up a Vagrant box

  • Add file named Vagrantfile with the following content
Vagrant.configure("2") do |config| = "debian/jessie64"

Starting up a Vagrant box

  • From the directory where Vagrant file is located
  • Bring up the box:
$ vagrant up


Display vagrant machine ssh configuration

$ vagrant ssh-config
Host default
    User vagrant
    Port 2222
    UserKnownHostsFile /dev/null
    StrictHostKeyChecking no
    PasswordAuthentication no
    IdentityFile /home/alex/git/gtalug/2016-10-11-ansible/2-testing/01-base/.vagrant/machines/default/virtualbox/private_key
    IdentitiesOnly yes
    LogLevel FATAL

Log into vagrant box

$ vagrant ssh

Ansible machine configuration ansible.cfg

hostfile = hosts
remote_user = vagrant
private_key_file = .vagrant/machines/default/virtualbox/private_key
host_key_checking = False

Ansible hosts file

testserver ansible_ssh_host= ansible_ssh_port=2222

Testing SSH connectivity

$ ansible test -m ping
testserver | SUCCESS => {
    "changed": false,
    "ping": "pong"

Running script on vagrant box

$ ansible-playbook -vvv base-packages.yaml

Using different hypervisors

  • This works however virtualbox is not a preferred hypervisor.
  • It is not possible to run more than one hypervisor at a time.

Installing OpenSource hypervisor providers

# apt-get install vagrant-libvirt vagrant-mutate
  • vagrant-libvirt -- libvirt available in stretch and newer
  • vagrant-mutate -- convert original images to libvirt

If packages aren't available

$ vagrant plugin install vagrant-libvirt vagrant-mutate

Download vagrant libvirt box

Install libvirt version of the machine

# apt-get install vagrant-libvirt
$ vagrant box add debian/jessie64 --provider=libvirt

libvirt Vagrantfile configuration

Vagrant.configure("2") do |config|
    config.vm.provider :libvirt do |libvirt|
        libvirt.uri = 'qemu+unix:///system' = 'localhost'
        libvirt.username = 'alex'
        libvirt.connect_via_ssh = true
    config.vm.define :libvirt_vm do |machine| = "debian/jessie64"

Start up vagrant libvirt box

$vagrant up --provider=libvirt


Vagrant ssh config for virtualbox is different

$ vagrant ssh-config
Host libvirt_vm
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/alex/git/gtalug/2016-10-11-ansible/2-testing/02-vagrant-libvirt/.vagrant/machines/libvirt_vm/libvirt/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ProxyCommand ssh 'localhost' -l 'alex' -i '/home/alex/.ssh/id_rsa' nc %h %p

Update ansible.cfg settings

  • Set a new path to private_key_file
private_key_file = .vagrant/machines/libvirt_vm/libvirt/private_key

Update hosts settings

vagrant_libvirt ansible_ssh_host= ansible_ssh_port=22

Run ansible ping command

$ ansible test -m ping
testserver | SUCCESS => {
    "changed": false,
    "ping": "pong"

Creating preseed file

  • Install debian system answering all the install questions
  • Install debian-installer package on the system
  • Extract the answers

Preseed Answers extraction

# debconf-get-selections --installer > ${HOME}/preseed.cfg
# debconf-get-selections >> ${HOME}/preseed.cfg

Getting from bare metal to Ansible with Debian automated install

  • Ugly shell script in *
  • Creates iso with preseed file based on debian-8.6.0-amd64-CD-1.iso

Running an installation with preseed.cfg

  • See the video.

Getting foot in the door

  • First run copy ssh pubkey and some other things on a new system
$ ansible-playbook --ask-pass --ask-become-pass --ssh_port=2222 -i<hostname>, -vvv base_packages.yaml
  • Backup command if script fails and you need to debug it
$ ansible-playbook -i<hostname>, -vvv base_packages.yaml

Comma after hostname is important

Loading Debian Preseed in USB/CD images

You can’t perform that action at this time.