User Tools

Site Tools


технология_vagrant

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
технология_vagrant [2022/08/17 14:31]
val
технология_vagrant [2022/09/22 11:50]
val [Provision с использованием ansible]
Line 11: Line 11:
 ===== Установка ===== ===== Установка =====
  
-  * [[https://​val.bmstu.ru/​unix/​HashiCorp/​vagrant_2.2.10_x86_64.msi]]+  * [[https://​val.bmstu.ru/​unix/​HashiCorp/​vagrant_2.2.10_x86_64.msi]] ​ 
 +  * Можно не перезагружать систему,​ но нужно перезапустить Cmder
  
 ===== Подготовка командной строки ===== ===== Подготовка командной строки =====
  
-==== win cmd === 
  
-<​code>​ 
-C:​\Users\Administrator>​ C:​\HashiCorp\Vagrant\bin\vagrant.exe 
-</​code>​ 
  
 ==== Cmder ==== ==== Cmder ====
  
   * [[Cmder]]   * [[Cmder]]
- 
 <​code>​ <​code>​
-λ alias vagrant=C:​\HashiCorp\Vagrant\bin\vagrant.exe $* 
- 
 λ bash λ bash
  
-$ alias vagrant=C:/​\HashiCorp/​\Vagrant/​\bin/​\vagrant.exe $* +λ cd
-или +
-$ alias vagrant=/​c/​HashiCorp/​Vagrant/​bin/​vagrant.exe+
 </​code>​ </​code>​
  
-==== mobaxterm ==== +
-<​code>​ +
-mobaxterm>​ alias vagrant=/​drives/​c/​HashiCorp/​Vagrant/​bin/​vagrant.exe $* +
-</​code>​+
  
  
Line 45: Line 34:
   * [[https://​oracle-base.com/​articles/​vm/​create-a-vagrant-base-box-virtualbox|Create a Vagrant Base Box (VirtualBox)]] + [[https://​bbs.archlinux.org/​viewtopic.php?​id=270005|[SOLVED] Latest sshd not accepting key algorithms]]   * [[https://​oracle-base.com/​articles/​vm/​create-a-vagrant-base-box-virtualbox|Create a Vagrant Base Box (VirtualBox)]] + [[https://​bbs.archlinux.org/​viewtopic.php?​id=270005|[SOLVED] Latest sshd not accepting key algorithms]]
   * [[https://​vagrantcloud.com/​debian/​bullseye64]]   * [[https://​vagrantcloud.com/​debian/​bullseye64]]
 +
 +  * [[https://​val.bmstu.ru/​unix/​HashiCorp/​ubuntu_20.04.box]]
 +
 +  * [[Переменные окружения]]
 +  * [[https://​stackoverflow.com/​questions/​19872591/​how-to-use-vagrant-in-a-proxy-environment|How to use vagrant in a proxy environment?​]]
  
 <​code>​ <​code>​
Line 58: Line 52:
  
 $ vagrant box list $ vagrant box list
 +
 +$ ### vagrant box remove specialist/​ubuntu20
 </​code>​ </​code>​
  
Line 80: Line 76:
 $ vagrant global-status $ vagrant global-status
  
-$ vagrant ​halt+$ vagrant ​global-status --prune
  
-$ vagrant destroy+### vagrant halt 
 + 
 +$ ### vagrant destroy
 </​code>​ </​code>​
  
Line 111: Line 109:
   config.vm.network "​private_network",​ ip: "​192.168."​ + X + "​.201"​   config.vm.network "​private_network",​ ip: "​192.168."​ + X + "​.201"​
  
-#  ​config.vm.provider "​virtualbox"​ do |vb| +  ​config.vm.provider "​virtualbox"​ do |vb| 
-   ​vb.memory = "​2048"​ +    vb.memory = "​2048"​ 
-   ​vb.cpus = "​2"​+    vb.cpus = "​2"​
  
 #    file_to_disk = config.vm.hostname + '​_disk2.vdi'​ #    file_to_disk = config.vm.hostname + '​_disk2.vdi'​
Line 120: Line 118:
 #    end #    end
 #    vb.customize ['​storageattach',​ :id, '​--storagectl',​ '​SATA',​ '​--port',​ 2, '​--device',​ 0, '​--type',​ '​hdd',​ '​--medium',​ file_to_disk] #    vb.customize ['​storageattach',​ :id, '​--storagectl',​ '​SATA',​ '​--port',​ 2, '​--device',​ 0, '​--type',​ '​hdd',​ '​--medium',​ file_to_disk]
- end+ 
 +  end
   ​   ​
 ### /My config ### ### /My config ###
Line 164: Line 163:
  
 <​code>​ <​code>​
-λ npp provision_once.sh ​&+λ touch provision_once.sh
 </​code><​code>​ </​code><​code>​
-#sed -i '​s/​PasswordAuthentication no/​PasswordAuthentication yes/g' /​etc/​ssh/​sshd_config 
 #echo '​root:​strongpassword'​ | chpasswd #echo '​root:​strongpassword'​ | chpasswd
 +#sed -i '​s/​PasswordAuthentication no/​PasswordAuthentication yes/g' /​etc/​ssh/​sshd_config
 +#service ssh restart
  
 echo '​vagrant:​strongpassword'​ | chpasswd echo '​vagrant:​strongpassword'​ | chpasswd
 +
 +(echo '"​\e[A":​ history-search-backward';​ echo '"​\e[B":​ history-search-forward'​) >> /​etc/​inputrc
  
 timedatectl set-timezone Europe/​Moscow timedatectl set-timezone Europe/​Moscow
  
-apt-get update +apt update 
-#apt-get install -y net-tools+#apt install -y docker.io
 </​code><​code>​ </​code><​code>​
-λ npp provision_onstart.sh ​&+λ touch provision_onstart.sh
 </​code><​code>​ </​code><​code>​
 X=$1 X=$1
Line 192: Line 194:
 echo "​net.ipv4.ip_forward=1"​ > /​etc/​sysctl.d/​20-my-forward.conf && sysctl -p --system echo "​net.ipv4.ip_forward=1"​ > /​etc/​sysctl.d/​20-my-forward.conf && sysctl -p --system
 </​code><​code>​ </​code><​code>​
-λ npp Vagrantfile+λ cat Vagrantfile
 </​code><​code>​ </​code><​code>​
 ... ...
Line 198: Line 200:
  
   config.vm.provision "​provision_once",​ type: "​shell",​ run: "​once",​ path: "​provision_once.sh"​   config.vm.provision "​provision_once",​ type: "​shell",​ run: "​once",​ path: "​provision_once.sh"​
 +
   config.vm.provision "​provision_onstart",​ type: "​shell",​ run: "​always", ​   config.vm.provision "​provision_onstart",​ type: "​shell",​ run: "​always", ​
     args: X + " 2_arg 3_arg", ​     args: X + " 2_arg 3_arg", ​
Line 217: Line 220:
 λ cat Vagrantfile λ cat Vagrantfile
 ... ...
-  config.vm.provision "​provision_docker",​ type: "​ansible_local",​ run: "always" do |ansible|+  config.vm.provision "​provision_docker",​ type: "​ansible_local",​ run: "once" do |ansible|
     ansible.playbook = "​provision_docker.yml"​     ansible.playbook = "​provision_docker.yml"​
   end   end
 ... ...
 </​code><​code>​ </​code><​code>​
-$ vagrant provision --provision-with provision_docker+time vagrant provision --provision-with provision_docker 
 +real    11m2,127s
 </​code>​ </​code>​
  
 ===== Multi-Machine Vagrant Environments ===== ===== Multi-Machine Vagrant Environments =====
  
-==== Внутри Vagrantfile ==== +  ​* [[https://blog.scottlowe.org/2014/10/22/​multi-machine-vagrant-with-yaml/​|Multi-Machine ​Vagrant with YAML]] 
- +  * [[https://​stackoverflow.com/​questions/​16708917/​how-do-i-include-variables-in-my-vagrantfile|How do I include variables in my VagrantFile?​]]
-  ​* [[https://www.vagrantup.com/docs/​multi-machine|Multi-Machine]]+
  
 <​code>​ <​code>​
-λ npp Vagrantfile +λ vagrant destroy
-</​code><​code>​ +
-... +
-Vagrant.configure("​2"​) do |config|+
  
-### My config ### +λ touch nodes.yaml
- +
-  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 ### +
-... +
-</​code>​ +
- +
-==== С использованием структур данных ruby ==== +
- +
-  * [[https://​github.com/​hashicorp/​vagrant/​issues/​9200|Vagrant is assigning the same MAC to two different VMs?]] +
- +
-<​code>​ +
-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 +
-</​code>​ +
- +
-==== С использованием json ==== +
- +
-  * [[Формат JSON]] +
-  * [[https://​blog.scottlowe.org/​2016/​01/​18/​multi-machine-vagrant-json/​|Multi-Machine Vagrant Environments with JSON]] +
- +
-<​code>​ +
-λ npp nodes.json &+
 </​code><​code>​ </​code><​code>​
-+name: "​node1"​ 
-    { +  ip: "​201"​ 
-        "name": "​node1"​, +- name: "​node2"​ 
-        "​ip": "192.168.X.201", +  ip: "​202"​ 
-        "​vbox_config":​ [ +- name: "​node3"​ 
-            { "--cpus":​ "​2"​ }, +  ip: "​203"​
-            { "​--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"​ } +
-        ] +
-    } +
-]+
 </​code><​code>​ </​code><​code>​
-$ cat Vagrantfile+λ npp Vagrantfile
 </​code><​code>​ </​code><​code>​
-require 'json+... 
-boxes JSON.parse(File.read('​./nodes.json'))+require 'yaml
 +nodes YAML.load_file("./nodes.yaml")
  
 Vagrant.configure("​2"​) do |config| Vagrant.configure("​2"​) do |config|
  
 ### My config ### ### My config ###
- +  nodes.each do |opts| ​
-  boxes.each do |opts|+
     config.vm.define opts["​name"​] do |config|     config.vm.define opts["​name"​] do |config|
- +      ​config.vm.network "​private_network",​ ip: "​192.168."​ + X + "​."​ + opts["​ip"​]
-      ​config.vm.network "​private_network",​ ip: opts["​ip"​]+
       config.vm.hostname = opts["​name"​]       config.vm.hostname = opts["​name"​]
 +   ​
 +      config.vm.provider :virtualbox do |vb|
 +        # vb.name = opts["​name"​]
 +        vb.memory = "​2048"​
 +        vb.cpus = "​2"​
  
-     ​config.vm.provider "​virtualbox"​ do | vb | +        ​# file_to_disk = opts["​name"​] + '​_2.vdi'​ 
-#        ​file_to_disk = opts["​name"​] + '​_2.vdi'​ +        ​unless File.exist?​(file_to_disk) 
-       ​unless File.exist?​(file_to_disk) +          ​vb.customize ['​createhd',​ '​--filename',​ file_to_disk,​ '​--size',​ 4 * 1024] 
-         ​vb.customize ['​createhd',​ '​--filename',​ file_to_disk,​ '​--size',​ 4 * 1024] +        ​end 
-       end +        ​vb.customize ['​storageattach',​ :id, '​--storagectl',​ '​SATA',​ '​--port',​ 2, '​--device',​ 0, '​--type',​ '​hdd',​ '​--medium',​ file_to_disk]
-       ​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     end
   end   end
- 
 ### /My config ### ### /My config ###
- 
 ... ...
 +</​code><​code>​
 +λ ### cd ~/​conf/​vagrant/​nodes ​ #!!! set X in Vagrantfile
  
-end 
-</​code>​ 
-<​code>​ 
 λ vagrant up node1 λ vagrant up node1
  
 λ vagrant up λ vagrant up
 +
 +λ vagrant up --parallel
  
 λ vagrant ssh node2 λ vagrant ssh node2
  
-λ vagrant destroy ​node3+λ ### vagrant destroy ​node1
  
-λ vagrant destroy -f+λ ### vagrant destroy -f
 </​code>​ </​code>​
  
 ===== Дополнительные возможности ===== ===== Дополнительные возможности =====
- 
- 
  
 ==== Использование синхронизируемых каталогов ==== ==== Использование синхронизируемых каталогов ====
Line 436: Line 319:
 </​code>​ </​code>​
  
-====Multi-Machine Vagrant Environments ​=====+==== Еще варианты ​Multi-Machine Vagrant Environments ====
  
-==== Внутри Vagrantfile ​====+=== Внутри Vagrantfile ===
  
   * [[https://​www.vagrantup.com/​docs/​multi-machine|Multi-Machine]]   * [[https://​www.vagrantup.com/​docs/​multi-machine|Multi-Machine]]
Line 466: Line 349:
 </​code>​ </​code>​
  
-==== С использованием структур данных ruby ====+=== С использованием структур данных ruby ===
  
   * [[https://​github.com/​hashicorp/​vagrant/​issues/​9200|Vagrant is assigning the same MAC to two different VMs?]]   * [[https://​github.com/​hashicorp/​vagrant/​issues/​9200|Vagrant is assigning the same MAC to two different VMs?]]
Line 531: Line 414:
 </​code>​ </​code>​
  
-==== С использованием json ====+=== С использованием json ===
  
   * [[Формат JSON]]   * [[Формат JSON]]
Line 580: Line 463:
       config.vm.network "​private_network",​ ip: opts["​ip"​]       config.vm.network "​private_network",​ ip: opts["​ip"​]
       config.vm.hostname = opts["​name"​]       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|       opts["​vbox_config"​].each do |hash|
технология_vagrant.txt · Last modified: 2024/03/11 19:00 by val