This is an old revision of the document!
C:\Users\Administrator> C:\HashiCorp\Vagrant\bin\vagrant.exe
λ alias vagrant=C:\HashiCorp\Vagrant\bin\vagrant.exe $* λ bash $ alias vagrant=C:/\HashiCorp/\Vagrant/\bin/\vagrant.exe $* или $ alias vagrant=/c/HashiCorp/Vagrant/bin/vagrant.exe
mobaxterm> alias vagrant=/drives/c/HashiCorp/Vagrant/bin/vagrant.exe $*
$ vagrant box list $ vagrant box add debian/bullseye64 $ vagrant box list $ vagrant package --base ubuntu_20.04_03 --output /c/distrs/ubuntu_20.04.box $ vagrant box add /c/distrs/ubuntu_20.04.box --name specialist/ubuntu20 $ vagrant box list
$ mkdir ~/nodes $ cd ~/nodes $ vagrant init specialist/ubuntu20 $ less Vagrantfile ... config.vm.box = "specialist/ubuntu20" ... $ vagrant up $ vagrant ssh $ vagrant global-status $ vagrant halt $ vagrant destroy
λ npp Vagrantfile &
... X = "13" Vagrant.configure("2") do |config| ### My config ### config.vm.post_up_message = "This is X: " + X config.vm.hostname = "node1.corp" + X + ".un" config.vm.network "private_network", ip: "192.168." + X + ".201" config.vm.provider "virtualbox" do |vb| vb.memory = "2048" vb.cpus = "2" # file_to_disk = config.vm.hostname + '_disk2.vdi' # unless File.exist?(file_to_disk) # vb.customize ['createhd', '--filename', file_to_disk, '--size', 4 * 1024] # end # vb.customize ['storageattach', :id, '--storagectl', 'SATA', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', file_to_disk] end ### /My config ### ### My provision ### config.vm.provision "provision_once", type: "shell", inline: <<-SHELL apt-get update timedatectl set-timezone Europe/Moscow SHELL config.vm.provision "provision_onstart", run: "always", type: "shell", env: { "X" => X, }, inline: <<-SHELL eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'` route add default gw 192.168.$X.1 cat <<EOF >/etc/resolv.conf search corp$X.un nameserver 192.168.$X.10 EOF SHELL ### /My provision ### ... end
λ vagrant reload --provision
λ npp provision_once.sh &
#sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config #echo 'root:strongpassword' | chpasswd echo 'vagrant:strongpassword' | chpasswd timedatectl set-timezone Europe/Moscow apt-get update #apt-get install -y net-tools
λ npp provision_onstart.sh &
X=$1 echo $X route add default gw 192.168.$X.1 eval `route -n | awk '{ if ($8 =="eth0" && $2 != "0.0.0.0") print "route del default gw " $2; }'` chattr -i /etc/resolv.conf echo -e "search corp$X.un\nnameserver 192.168.$X.10" > /etc/resolv.conf chattr +i /etc/resolv.conf echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/20-my-forward.conf && sysctl -p --system
λ npp Vagrantfile
... ### My provision ### config.vm.provision "provision_once", type: "shell", run: "once", path: "provision_once.sh" config.vm.provision "provision_onstart", type: "shell", run: "always", args: X + " 2_arg 3_arg", path: "provision_onstart.sh" ### /My provision ### ...
λ vagrant provision --provision-with provision_once,provision_onstart vagrant@node1:~$ ls /vagrant/
λ cat Vagrantfile ... config.vm.provision "provision_docker", type: "ansible_local", run: "always" do |ansible| ansible.playbook = "provision_docker.yml" end ...
$ vagrant provision --provision-with provision_docker
λ mkdir ~/nodes λ cd ~/nodes λ npp nodes.yaml &
- name: "node1" ip: "201" - name: "node2" ip: "202" - name: "node3" ip: "203"
λ npp Vagrantfile
... require 'yaml' nodes = YAML.load_file("./nodes.yaml") Vagrant.configure("2") do |config| ### My config ### nodes.each do |opts| config.vm.define opts["name"] do |config| config.vm.network "private_network", ip: "192.168." + X + "." + opts["ip"] config.vm.hostname = opts["name"] config.vm.provider :virtualbox do |vb| # vb.name = opts["name"] vb.memory = "2048" vb.cpus = "2" # file_to_disk = opts["name"] + '_2.vdi' # unless File.exist?(file_to_disk) # vb.customize ['createhd', '--filename', file_to_disk, '--size', 4 * 1024] # end # vb.customize ['storageattach', :id, '--storagectl', 'SATA', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', file_to_disk] end end end ### /My config ### ...
λ vagrant up node1 λ vagrant up λ vagrant ssh node2 λ vagrant destroy node3 λ vagrant destroy -f
λ cat Vagrantfile
... # config.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__exclude: ".git/" ...
λ npp SomeFile ... λ vagrant rsync λ vagrant ssh vagrant@node3:~$ ls /vagrant/
... Vagrantfile ... SomeFile ...
λ npp Vagrantfile
... Vagrant.configure("2") do |config| ### My config ### config.vm.define "node1" do |node1| node1.vm.network "private_network", ip: "192.168.X.201" end config.vm.define "node2" do |node2| node2.vm.network "private_network", ip: "192.168.X.202" end config.vm.define "node3" do |node3| node3.vm.network "private_network", ip: "192.168.X.203" end ### /My config ### ...
boxes = [ { :name => "node1", :ip => "192.168.X.201", :vbox_config => [ { "--cpus" => "2" }, { "--memory" => "2048" } ], }, { :name => "node2", :ip => "192.168.X.202", :vbox_config => [ { "--cpus" => "2" }, { "--memory" => "2048" } ], }, { :name => "node3", :ip => "192.168.X.203", :vbox_config => [ { "--cpus" => "2" }, { "--memory" => "2048" } ], } ] Vagrant.configure("2") do |config| ### My config ### boxes.each do |opts| config.vm.define opts[:name] do |config| config.vm.network "private_network", ip: opts[:ip] config.vm.hostname = opts[:name] # config.vm.provider "virtualbox" do | vb | # file_to_disk = opts[:name] + '_2.vdi' # unless File.exist?(file_to_disk) # vb.customize ['createhd', '--filename', file_to_disk, '--size', 4 * 1024] # end # vb.customize ['storageattach', :id, '--storagectl', 'SATA', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', file_to_disk] # end opts[:vbox_config].each do |hash| hash.each do |key, value| config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, key, value] end end end end end ### /My config ### ... end
λ npp nodes.json &
[ { "name": "node1", "ip": "192.168.X.201", "vbox_config": [ { "--cpus": "2" }, { "--memory": "2048" } ] }, { "name": "node2", "ip": "192.168.X.202", "vbox_config": [ { "--cpus": "2" }, { "--memory": "2048" } ] }, { "name": "node3", "ip": "192.168.X.203", "vbox_config": [ { "--cpus": "2" }, { "--memory": "2048" } ] } ]
$ cat Vagrantfile
require 'json' boxes = JSON.parse(File.read('./nodes.json')) Vagrant.configure("2") do |config| ### My config ### boxes.each do |opts| config.vm.define opts["name"] do |config| config.vm.network "private_network", ip: opts["ip"] config.vm.hostname = opts["name"] # config.vm.provider "virtualbox" do | vb | # file_to_disk = opts["name"] + '_2.vdi' # unless File.exist?(file_to_disk) # vb.customize ['createhd', '--filename', file_to_disk, '--size', 4 * 1024] # end # vb.customize ['storageattach', :id, '--storagectl', 'SATA', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', file_to_disk] # end opts["vbox_config"].each do |hash| hash.each do |key, value| config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, key, value] end end end end end ### /My config ### ... end