Skip to content
Snippets Groups Projects

vagrant

vagrant

By HashiCorp

vagrant

vagrant

  • il peut être considéré comme un wrapper d'hyperviseur
    • VirtualBox
    • libvirt
    • VMware
    • Amazon EC2
  • supporte nativement docker depuis la 1.6

Installation

pré-requis

$ sudo apt install virtualbox

via gem

$ sudo apt install rubygems-integration
$ sudo gem install vagrant

via apt

$ sudo apt install vagrant

afficher la version de vagrant

$ vagrant --version

Initialisation du projet

$ mkdir project && cd project
$ vagrant init

génère un Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "base"
end

beaucoup de commentaires ... laisser vous guider

Box

  • machines virtuelles préconfigurées (templates)
  • vagrant cloud
  • vagrantbox.es
  • mis en cache
  • nommage à la github "développeur/Box"
$ vagrant box add "ubuntu/xenial64"
$ vagrant box add "http://aka.ms/vagrant-win7-ie11"
$ vagrant box list
$ vagrant box remove "ubuntu/xenial64"

cycle de vie

$ vagrant init "ubuntu/bionic64"
$ vagrant up #--provider=virtualbox
$ vagrant provision
$ vagrant ssh
$ vagrant halt
$ vagrant suspend
$ vagrant reload
$ vagrant destroy #--force

réseau

mapping de port

Vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8004
end

vm

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.provider "virtualbox" do |vb|
     vb.gui = false
     vb.memory = "4096"
  end
end

points de montage

montage automatique de . dans /vagrant

$ vagrant ssh
$ ll /vagrant
total 60
drwxr-xr-x  1 vagrant vagrant  4096 Dec 14 19:46 ./
drwxr-xr-x 24 root    root     4096 Dec 14 19:47 ../
drwxr-xr-x  1 vagrant vagrant  4096 Dec 14 19:46 .vagrant/
-rw-r--r--  1 vagrant vagrant   155 Dec 14 19:49 Vagrantfile
-rw-------  1 vagrant vagrant 44198 Dec 14 19:47 ubuntu-bionic-18.04-cloudimg-console.log

autre possibilité à partir de Vagrantfile

Vagrant.configure("2") do |config|
 ...
 config.vm.provision "file", source: "~/.gitconfig", destination: "~/.gitconfig"
 ...
end

provisioning

via l'entrée standard

config.vm.provision "shell", inline: <<-SHELL
   sudo apt install -y python openssh-server
   SHELL

via un script

Vagrant.configure("2") do |config|
 config.vm.box = "hashicorp/precise32"
 config.vm.provision "shell", path: "script.sh"
end
if ENV['VAGRANT_OS']
  os = ENV['VAGRANT_OS']
else
  os = "ubuntu"
end
Vagrant.configure("2") do |config|
  if os == "centos"
    config.vm.box = "centos/7"
  else
    config.vm.box = "ubuntu/bionic64"
  end
  if os == "debian"
    config.vm.provision "shell", inline: <<-SHELL
    sudo yum install httpd
    SHELL
  else
    config.vm.provision "shell", inline: <<-SHELL
    sudo apt install -y apache2
    SHELL
  end
end

provisioning

via ansible

Vagrant.configure("2") do |config|
 config.vm.provision "ansible" do |ansible|    
  ansible.playbook = "playbook.yml"
  ansible.host_key_checking = false
  ansible.playbook = "vault.yml"
  ansible.extra_vars = { is_vagrant: true }
  ansible.tags = ['initialize']
  ansible.skip_tags = ["vagrant_context"]
  ansible.inventory_path = "./my-inventory"
  ansible.raw_arguments  = ["--private-key=~/.ssh/id/id_rsa"]
  ansible.verbose = "vvv"
 end
end

Shared Ansible Options

conclusion

laissez un Vagrantfile dans vos roles ansible est toujours une bonne idée!