Vagrant - Workflow création de 2 boxs
Alasta 2 Août 2021 vagrant vagrant admin template automate cli
Description : Workflow de création d'un template de 2 boxs.
Contexte :
Nous allons créer 2 boxs avec un réseau publique pour que les boxs soient joignables sur le réseau.
Création des boxs :
Création de l'environnement :
1 mkdir ~/.my_vagrant/demo2box
2 cd ~/.my_vagrant/demo2box
Création du Vagrantfile :
1 $ vi Vagrantfile
2 # Script systeme pour avoir des infos des boxs
3 # il est variabilisé pour être réutilisé
4 $systemcmd = <<-SCRIPT
5 echo "Infos machine :"
6 hostname
7 ip -4 addr sh eth1
8 date
9 SCRIPT
10
11 Vagrant.configure("2") do |config|
12 #Déclaration de la box 1
13 config.vm.define "vm1" do |vm1|
14 vm1.vm.box = "centos/7"
15 vm1.vm.hostname = 'vm1'
16 vm1.vm.box_url = "centos/7"
17 vm1.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", auto_config: false
18 vm1.vm.provision "shell", inline: $systemcmd
19 end
20
21
22 #Déclaration de la box 2
23 config.vm.define "vm2" do |vm2|
24 vm2.vm.box = "centos/7"
25 vm2.vm.hostname = 'vm2'
26 vm2.vm.box_url = "centos/7"
27 vm2.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", auto_config: false
28 vm2.vm.provision "shell", inline: $systemcmd
29 end
30 end
On a définit :
- le type box : centos 7
- le hostname de la VM
- le pont entre le réseau publique Vagrant et l'interface réseau de l'hôte
- un script post déploiement pour personnaliser la machine (ici donner des infos)
Déploiement des boxs :
1 $ vagrant up
2 Bringing machine 'vm1' up with 'virtualbox' provider...
3 Bringing machine 'vm2' up with 'virtualbox' provider...
4 ==> vm1: Importing base box 'centos/7'...
5 ==> vm1: Matching MAC address for NAT networking...
6 ==> vm1: Checking if box 'centos/7' version '2004.01' is up to date...
7 ==> vm1: Setting the name of the VM: demo2boxs_vm1_1627908909528_61845
8 ==> vm1: Clearing any previously set network interfaces...
9 ==> vm1: Preparing network interfaces based on configuration...
10 vm1: Adapter 1: nat
11 vm1: Adapter 2: bridged
12 ==> vm1: Forwarding ports...
13 vm1: 22 (guest) => 2222 (host) (adapter 1)
14 ==> vm1: Booting VM...
15 ==> vm1: Waiting for machine to boot. This may take a few minutes...
16 vm1: SSH address: 127.0.0.1:2222
17 vm1: SSH username: vagrant
18 vm1: SSH auth method: private key
19 vm1:
20 vm1: Vagrant insecure key detected. Vagrant will automatically replace
21 vm1: this with a newly generated keypair for better security.
22 vm1:
23 vm1: Inserting generated public key within guest...
24 vm1: Removing insecure key from the guest if it's present...
25 vm1: Key inserted! Disconnecting and reconnecting using new SSH key...
26 ==> vm1: Machine booted and ready!
27 ==> vm1: Checking for guest additions in VM...
28 vm1: No guest additions were detected on the base box for this VM! Guest
29 vm1: additions are required for forwarded ports, shared folders, host only
30 vm1: networking, and more. If SSH fails on this machine, please install
31 vm1: the guest additions and repackage the box to continue.
32 vm1:
33 vm1: This is not an error message; everything may continue to work properly,
34 vm1: in which case you may ignore this message.
35 ==> vm1: Setting hostname...
36 ==> vm1: Rsyncing folder: /home/alasta/.my_vagrant/demo2boxs/ => /vagrant
37 ==> vm1: Running provisioner: shell...
38 vm1: Running: inline script
39 vm1: Infos machine :
40 vm1: vm1
41 vm1: 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
42 vm1: inet 192.168.139.166/24 brd 192.168.139.255 scope global noprefixroute dynamic eth1
43 vm1: valid_lft 3597sec preferred_lft 3597sec
44 vm1: Mon Aug 2 12:52:17 UTC 2021
45 ==> vm2: Importing base box 'centos/7'...
46 ==> vm2: Matching MAC address for NAT networking...
47 ==> vm2: Checking if box 'centos/7' version '2004.01' is up to date...
48 ==> vm2: Setting the name of the VM: demo2boxs_vm2_1627908942769_30142
49 ==> vm2: Fixed port collision for 22 => 2222. Now on port 2200.
50 ==> vm2: Clearing any previously set network interfaces...
51 ==> vm2: Preparing network interfaces based on configuration...
52 vm2: Adapter 1: nat
53 vm2: Adapter 2: bridged
54 ==> vm2: Forwarding ports...
55 vm2: 22 (guest) => 2200 (host) (adapter 1)
56 ==> vm2: Booting VM...
57 ==> vm2: Waiting for machine to boot. This may take a few minutes...
58 vm2: SSH address: 127.0.0.1:2200
59 vm2: SSH username: vagrant
60 vm2: SSH auth method: private key
61 vm2:
62 vm2: Vagrant insecure key detected. Vagrant will automatically replace
63 vm2: this with a newly generated keypair for better security.
64 vm2:
65 vm2: Inserting generated public key within guest...
66 vm2: Removing insecure key from the guest if it's present...
67 vm2: Key inserted! Disconnecting and reconnecting using new SSH key...
68 ==> vm2: Machine booted and ready!
69 ==> vm2: Checking for guest additions in VM...
70 vm2: No guest additions were detected on the base box for this VM! Guest
71 vm2: additions are required for forwarded ports, shared folders, host only
72 vm2: networking, and more. If SSH fails on this machine, please install
73 vm2: the guest additions and repackage the box to continue.
74 vm2:
75 vm2: This is not an error message; everything may continue to work properly,
76 vm2: in which case you may ignore this message.
77 ==> vm2: Setting hostname...
78 ==> vm2: Rsyncing folder: /home/alasta/.my_vagrant/demo2boxs/ => /vagrant
79 ==> vm2: Running provisioner: shell...
80 vm2: Running: inline script
81 vm2: Infos machine :
82 vm2: vm2
83 vm2: 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
84 vm2: inet 192.168.139.241/24 brd 192.168.139.255 scope global noprefixroute dynamic eth1
85 vm2: valid_lft 3597sec preferred_lft 3597sec
86 vm2: Mon Aug 2 12:52:51 UTC 2021
Note :
- si une des boxs est déjà deployée, il ne déploiera que l'autre.
- on aurait pu lancer un vagrant up vm2 pour ne lancer que le débploiement de vm2.
Afficher les boxs de notre environnement :
1 $ vagrant status
2 Current machine states:
3
4 vm1 running (virtualbox)
5 vm2 running (virtualbox)
6
7 This environment represents multiple VMs. The VMs are all listed
8 above with their current state. For more information about a specific
9 VM, run 'vagrant status NAME'.
Dans le cas d'une box arrêtée :
1 $ vagrant halt vm1
2 ==> vm1: Attempting graceful shutdown of VM...
3
4
5 $ vagrant status
6 Current machine states:
7
8 vm1 poweroff (virtualbox)
9 vm2 running (virtualbox)
10
11 This environment represents multiple VMs. The VMs are all listed
12 above with their current state. For more information about a specific
13 VM, run 'vagrant status NAME'.
Connexion shell à une box
1 $ vagrant ssh vm1
Note : quand il y a plusieurs boxs il faut précéiser la box sur laquelle on souhaite se connecter.
ou
1 $ vagrant ssh-config > lab_ssh_config
2
3 $ ssh -F lab_ssh_config vm1
4 Last login: Mon Aug 2 14:05:12 2021 from 192.168.139.105
5 [vagrant@vm1 ~]$
Note : il est possible de modifier les IP et port dans le fichier de sortie du vagrant ssh-config pour utiliser l'IP de sont réseau publique (par défaut l'IP est localhost et le port > 2200).
Tests de connexions :
Récupération de l'IP de la box :
1 $ vagrant ssh vm1
2 Last login: Mon Aug 2 14:07:42 2021 from 10.0.2.2
3 [vagrant@vm1 ~]$ ip addr sh
4 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
5 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
6 inet 127.0.0.1/8 scope host lo
7 valid_lft forever preferred_lft forever
8 inet6 ::1/128 scope host
9 valid_lft forever preferred_lft forever
10 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
11 link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ff
12 inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
13 valid_lft 85152sec preferred_lft 85152sec
14 inet6 fe80::5054:ff:fe4d:77d3/64 scope link
15 valid_lft forever preferred_lft forever
16 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
17 link/ether 08:00:27:60:f2:25 brd ff:ff:ff:ff:ff:ff
18 inet 192.168.139.166/24 brd 192.168.139.255 scope global noprefixroute dynamic eth1
19 valid_lft 2353sec preferred_lft 2353sec
20 inet6 fe80::47c3:3077:2ed0:74aa/64 scope link noprefixroute
21 valid_lft forever preferred_lft forever
Ici :
- 10.0.2.15 est l'IP privée, joignable qu'entre les 2 boxs
- 192.168.139.166 est l'IP publique joignable par "tout le monde"
Test de ping depuis vm2 :
1 [vagrant@vm2 ~]$ ping -c 2 10.0.2.15
2 PING 10.0.2.15 (10.0.2.15) 56(84) bytes of data.
3 64 bytes from 10.0.2.15: icmp_seq=1 ttl=64 time=0.022 ms
4 64 bytes from 10.0.2.15: icmp_seq=2 ttl=64 time=0.048 ms
5
6 --- 10.0.2.15 ping statistics ---
7 2 packets transmitted, 2 received, 0% packet loss, time 1002ms
8 rtt min/avg/max/mdev = 0.022/0.035/0.048/0.013 ms
9 [vagrant@vm2 ~]$ ping -c 2 192.168.139.166
10 PING 192.168.139.166 (192.168.139.166) 56(84) bytes of data.
11 64 bytes from 192.168.139.166: icmp_seq=1 ttl=64 time=0.881 ms
12 64 bytes from 192.168.139.166: icmp_seq=2 ttl=64 time=0.787 ms
13
14 --- 192.168.139.166 ping statistics ---
15 2 packets transmitted, 2 received, 0% packet loss, time 1002ms
16 rtt min/avg/max/mdev = 0.787/0.834/0.881/0.047 ms
Test de ping depuis la machine hôte
1 $ ping -c2 10.0.2.15
2 PING 10.0.2.15 (10.0.2.15): 56 data bytes
3 Request timeout for icmp_seq 0
4
5 --- 10.0.2.15 ping statistics ---
6 2 packets transmitted, 0 packets received, 100.0% packet loss
7
8 $ ping -c 2 192.168.139.166
9 PING 192.168.139.166 (192.168.139.166): 56 data bytes
10 64 bytes from 192.168.139.166: icmp_seq=0 ttl=64 time=0.497 ms
11 64 bytes from 192.168.139.166: icmp_seq=1 ttl=64 time=0.519 ms
12
13 --- 192.168.139.166 ping statistics ---
14 2 packets transmitted, 2 packets received, 0.0% packet loss
15 round-trip min/avg/max/stddev = 0.497/0.508/0.519/0.011 ms