vagrant.md 3.9 KB
Newer Older
Vincent Mazenod's avatar
Vincent Mazenod committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# vagrant

![vagrant](images/vagrant.png "vagrant")<!-- .element width="30%" -->

**By HashiCorp**


## vagrant

* création / configuration d'environnements virtuels

* écrit en ruby
* [<i class="fa fa-github" aria-hidden="true"></i> hashicorp/vagrant](https://github.com/hashicorp/vagrant)
Vincent Mazenod's avatar
Vincent Mazenod committed
14
  * [<i class="fa fa-gavel" aria-hidden="true"></i> The MIT License](https://github.com/hashicorp/vagrant/blob/master/LICENSE)
Vincent Mazenod's avatar
Vincent Mazenod committed
15

Vincent Mazenod's avatar
Vincent Mazenod committed
16
* outil en ligne de commande
Vincent Mazenod's avatar
Vincent Mazenod committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  * se voit dans l'hyperviseur utilisé


## vagrant

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


## Installation

Vincent Mazenod's avatar
Vincent Mazenod committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
```
Vincent Mazenod's avatar
Vincent Mazenod committed
56
57
58
59
60


## Initialisation du projet

```
Vincent Mazenod's avatar
Vincent Mazenod committed
61
62
$ mkdir project && cd project
$ vagrant init
Vincent Mazenod's avatar
Vincent Mazenod committed
63
64
65
66
```

génère un Vagrantfile

Vincent Mazenod's avatar
Vincent Mazenod committed
67
```ruby
Vincent Mazenod's avatar
Vincent Mazenod committed
68
69
70
71
72
Vagrant.configure("2") do |config|
  config.vm.box = "base"
end
```

Vincent Mazenod's avatar
Vincent Mazenod committed
73
Beaucoup de commentaires ... laisser vous guider
Vincent Mazenod's avatar
Vincent Mazenod committed
74
75
76
77
78
79
80
81
82
83
84


## Box

* Machines virtuelles préconfigurées (templates)
* [vagrant cloud](https://app.vagrantup.com/boxes/search)
* [vagrantbox.es](https://www.vagrantbox.es/)
* mis en cache
* nommage à la github "développeur/Box"

```
Vincent Mazenod's avatar
Vincent Mazenod committed
85
86
87
88
$ vagrant box add "ubuntu/xenial64"
$ vagrant box add "http://aka.ms/vagrant-win7-ie11"
$ vagrant box list
$ vagrant box remove "ubuntu/xenial64"
Vincent Mazenod's avatar
Vincent Mazenod committed
89
90
91
92
93
94
```
* [Creating a Base Box](https://www.vagrantup.com/docs/boxes/base.html)
  * [<i class="fa fa-github" aria-hidden="true"></i> veewee](https://github.com/jedi4ever/veewee)
  * [<i class="fa fa-github" aria-hidden="true"></i> How to Create a CentOS Vagrant Base Box](https://github.com/ckan/ckan/wiki/How-to-Create-a-CentOS-Vagrant-Base-Box)


Vincent Mazenod's avatar
Vincent Mazenod committed
95
## cycle de vie
Vincent Mazenod's avatar
Vincent Mazenod committed
96
97

```
Vincent Mazenod's avatar
Vincent Mazenod committed
98
99
100
101
102
103
104
$ vagrant init "ubuntu/bionic64"
$ vagrant up #--provider=virtualbox
$ vagrant ssh
$ vagrant halt
$ vagrant suspend
$ vagrant reload
$ vagrant destroy #--force
Vincent Mazenod's avatar
Vincent Mazenod committed
105
106
107
```


Vincent Mazenod's avatar
Vincent Mazenod committed
108
## points de montage
Vincent Mazenod's avatar
Vincent Mazenod committed
109

Vincent Mazenod's avatar
Vincent Mazenod committed
110
montage automatique de `.` dans `/vagrant`
Vincent Mazenod's avatar
Vincent Mazenod committed
111

Vincent Mazenod's avatar
Vincent Mazenod committed
112
113
114
115
116
117
118
119
120
121
```
$ 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
```
Vincent Mazenod's avatar
Vincent Mazenod committed
122

Vincent Mazenod's avatar
Vincent Mazenod committed
123
autre possibilité à partir de `Vagrantfile`
Vincent Mazenod's avatar
Vincent Mazenod committed
124

Vincent Mazenod's avatar
Vincent Mazenod committed
125
126
127
128
129
130
131
```ruby
Vagrant.configure("2") do |config|
 ...
 config.vm.provision "file", source: "~/.gitconfig", destination: "~/.gitconfig"
 ...
end
```
Vincent Mazenod's avatar
Vincent Mazenod committed
132
133
134
135


## provisioning

Vincent Mazenod's avatar
Vincent Mazenod committed
136
via l'entrée standard
Vincent Mazenod's avatar
Vincent Mazenod committed
137

Vincent Mazenod's avatar
Vincent Mazenod committed
138
139
140
141
```ruby
config.vm.provision "shell", inline: <<-SHELL
   sudo apt install -y python openssh-server
   SHELL
Vincent Mazenod's avatar
Vincent Mazenod committed
142
```
Vincent Mazenod's avatar
Vincent Mazenod committed
143
144
145
146

via un script

```ruby
Vincent Mazenod's avatar
Vincent Mazenod committed
147
148
149
150
151
152
153
154
155
156
157
Vagrant.configure("2") do |config|
 config.vm.box = "hashicorp/precise32"
 config.vm.provision "shell", path: "script.sh"
end
```


## provisioning

via [ansible](ansible.html)

Vincent Mazenod's avatar
Vincent Mazenod committed
158
```ruby
Vincent Mazenod's avatar
Vincent Mazenod committed
159
160
161
Vagrant.configure("2") do |config|
 config.vm.provision "ansible" do |ansible|    
  ansible.playbook = "playbook.yml"
Vincent Mazenod's avatar
Vincent Mazenod committed
162
163
164
165
166
167
168
169
  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"
Vincent Mazenod's avatar
Vincent Mazenod committed
170
171
172
173
174
175
176
 end
end
```
### idéal pour tester les playbooks


## vagrant pour tester apache
Vincent Mazenod's avatar
Vincent Mazenod committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

```ruby
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.network "forwarded_port", guest: 80, host: 8000
  config.vm.provider "virtualbox" do |vb|
     vb.gui = false
     vb.memory = "4096"
  end
  config.vm.provision "shell", inline: <<-SHELL
     sudo apt install -y apache2
     SHELL
end
```


## conclusion

### laissez un petit Vagrantfile dans vos playbooks est toujours une bonne idée!