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 :

1 mkdir ~/.my_vagrant/

Création de la premières box :

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

 1 $ mkdir ~/.my_vagrant/demo
 2 
 3 $ cd ~/.my_vagrant/demo
 4 
 5 $ vagrant init centos/7
 6 A 'Vagrantfile' has been placed in this directory. You are now
 7 ready to 'vagrant up' your first virtual environment! Please read
 8 the comments in the Vagrantfile as well as documentation on
 9 'vagrantup.com' for more information on using Vagrant.
10 
11 $ vagrant up
12 Bringing machine 'default' up with 'virtualbox' provider...
13 ==> default: Importing base box 'centos/7'...
14 ==> default: Matching MAC address for NAT networking...
15 ==> default: Checking if box 'centos/7' version '2004.01' is up to date...
16 ==> default: Setting the name of the VM: demo_default_1627896418926_26742
17 ==> default: Clearing any previously set network interfaces...
18 ==> default: Preparing network interfaces based on configuration...
19     default: Adapter 1: nat
20 ==> default: Forwarding ports...
21     default: 22 (guest) => 2222 (host) (adapter 1)
22 ==> default: Booting VM...
23 ==> default: Waiting for machine to boot. This may take a few minutes...
24     default: SSH address: 127.0.0.1:2222
25     default: SSH username: vagrant
26     default: SSH auth method: private key
27     default:
28     default: Vagrant insecure key detected. Vagrant will automatically replace
29     default: this with a newly generated keypair for better security.
30     default:
31     default: Inserting generated public key within guest...
32     default: Removing insecure key from the guest if it's present...
33     default: Key inserted! Disconnecting and reconnecting using new SSH key...
34 ==> default: Machine booted and ready!
35 ==> default: Checking for guest additions in VM...
36     default: No guest additions were detected on the base box for this VM! Guest
37     default: additions are required for forwarded ports, shared folders, host only
38     default: networking, and more. If SSH fails on this machine, please install
39     default: the guest additions and repackage the box to continue.
40     default:
41     default: This is not an error message; everything may continue to work properly,
42     default: in which case you may ignore this message.
43 ==> default: Rsyncing folder: /home/alasta/.my_vagrant/demo/ => /vagrant

Liste de box :

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

Connexion à la box :

 1 $ vagrant ssh
 2 [vagrant@localhost ~]$ ip add sh
 3 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
 4     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 5     inet 127.0.0.1/8 scope host lo
 6        valid_lft forever preferred_lft forever
 7     inet6 ::1/128 scope host
 8        valid_lft forever preferred_lft forever
 9 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
10     link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ff
11     inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
12        valid_lft 86354sec preferred_lft 86354sec
13     inet6 fe80::5054:ff:fe4d:77d3/64 scope link
14        valid_lft forever preferred_lft forever
15 [vagrant@localhost ~]$ logout
16 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..

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

Destruction de la box :

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