Contexte :

Vagrant est un outil opensource pour construire et manager des VM au travers d’un workflow d’automatisation.
Par défaut Vagrant s’interface avec différents hyperviseurs (providers) comme VirtualBox, VMware, Hyper-V, Docker.

Les VM sont des images pré-packagées pour Vagrant, elles sont appelées box.
Le catalogue de box est disponible ICI.

Je ne sais pas s’il est utilisable en production, mais pour du dev/démo c’est parfait.
Il faut avoir un VirtualBox d’installé.

Premiers pas:

Nous allons créer une box CentOS 7 basique (à la première installation, il y aura le téléchargement de la box).

Création de l’environnement de lab Vagrant :

mkdir ~/.my_vagrant/

Création de la premières box :

Dans notre démo, Vagrant va découvrir et utiliser l’hyperviseur VirtualBox.

$ mkdir ~/.my_vagrant/demo

$ cd ~/.my_vagrant/demo

$ vagrant init centos/7
A 'Vagrantfile' has been placed in this directory. You are now
ready to 'vagrant up' your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
'vagrantup.com' for more information on using Vagrant.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Setting the name of the VM: demo_default_1627896418926_26742
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /home/alasta/.my_vagrant/demo/ => /vagrant

Liste de box :

$ vagrant box list
centos/7 (virtualbox, 2004.01)

Connexion à la box :

$ vagrant ssh
[vagrant@localhost ~]$ ip add sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 86354sec preferred_lft 86354sec
    inet6 fe80::5054:ff:fe4d:77d3/64 scope link 
       valid_lft forever preferred_lft forever
[vagrant@localhost ~]$ logout
Connection to 127.0.0.1 closed.

Envers du décor :

Lors du vagrant init centos/7, vagrant va générer un Vagrantfile qui contient la configuration de notre box.
Nous allons rester simple dans notre démo et ne pas afficher les commentaires..

$ grep -Ev "(#|^$)" Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end

Destruction de la box :

$ vagrant destroy 
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...