Инструменты пользователя

Инструменты сайта


технология_vagrant

Технология Vagrant

Getting Start

λ C:\HashiCorp\Vagrant\bin\vagrant.exe box add debian/buster64

λ alias vagrant=C:\HashiCorp\Vagrant\bin\vagrant.exe $*

λ dir /x C:\

λ alias npp="C:\PROGRA~2\Notepad++\notepad++.exe" $*

λ vagrant box list

λ mkdir C:\Users\student\vagrant\node1

λ cd C:\Users\student\vagrant\node1

C:\Users\student\vagrant\node1 (master)
λ vagrant init debian/buster64

λ vagrant up

λ vagrant ssh

λ vagrant global-status

λ vagrant halt

Provision

Внутри Vagrantfile

λ npp Vagrantfile
...
  config.vm.provision "shell", inline: <<-SHELL
    sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config    
    systemctl restart sshd.service

    apt-get update
    apt-get install -y net-tools
  SHELL
  
  config.vm.provision "shell",
    run: "always",
    inline: "route add default gw 192.168.13.1"

  config.vm.provision "shell",
    run: "always",
    inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
...
λ vagrant reload --provision

С использованием внешних скриптов

λ npp provision_once.sh
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config  

apt-get update
apt-get install -y net-tools
λ npp provision_restart.sh
route add default gw 192.168.13.1

eval `route -n | awk '{ if ($8 =="eth0" && $2 != "0.0.0.0") print "route del default gw " $2; }'`
λ npp Vagrantfile
...
  config.vm.provision "provision_once", type: "shell", run: "once", path: "provision_once.sh"
  config.vm.provision "provision_onstart", type: "shell", run: "always", path: "provision_onstart.sh"
...
λ vagrant provision --provision-with provision_once

λ vagrant provision --provision-with provision_onstart

Использование синхронизируемых каталогов

λ cat Vagrantfile
...
#  config.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__exclude: ".git/"
...
λ npp SomeFile
...

λ vagrant rsync

λ vagrant ssh

vagrant@node1:~$ ls /vagrant/
...
Vagrantfile
...
SomeFile
...

С использованием ansible

* Сервис Ansible Использование playbook

λ cat Vagrantfile
...
  config.vm.provision "provision_always", type: "ansible_local", run: "always" do |ansible|
    ansible.playbook = "provision_always.yml"
  end

Multi-Machine Vagrant Environments

Внутри Vagrantfile

λ npp Vagrantfile
...
Vagrant.configure("2") do |config|

  config.vm.define "node1" do |node1|
    node1.vm.network "private_network", ip: "192.168.13.10"
  end

  config.vm.define "node2" do |node2|
    node2.vm.network "private_network", ip: "192.168.13.20"
  end

  config.vm.define "node3" do |node3|
    node3.vm.network "private_network", ip: "192.168.13.30"
  end
...

С использованием структур данных ruby

boxes = [
    {
        :name => "node1",
        :ip => "192.168.13.10",
        :vbox_config => [
            { "--cpus" => "1" },
            { "--memory" => "1024" }
        ],
    },
    {
        :name => "node2",
        :ip => "192.168.13.20",
        :vbox_config => [
            { "--cpus" => "1" },
            { "--memory" => "1024" }
        ],
    },
    {
        :name => "node3",
        :ip => "192.168.13.30",
        :vbox_config => [
            { "--cpus" => "1" },
            { "--memory" => "1024" }
        ],
    }
]

Vagrant.configure("2") do |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', 5 * 1024]
        end
        vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--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
C:\Users\student\vagrant\nodes (master)

λ vagrant up

λ vagrant ssh node2

λ vagrant destroy node3

С использованием json

[
    {
        "name": "node1",
        "ip": "192.168.13.10",
        "vbox_config": [
            { "--cpus": "1" },
            { "--memory": "1024" }
        ]
    },
    {
        "name": "node2",
        "ip": "192.168.13.20",
        "vbox_config": [
            { "--cpus": "1" },
            { "--memory": "1024" }
        ]
    }
]
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'

require 'json'
boxes = JSON.parse(File.read('./nodes.json'))

Vagrant.configure("2") do |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', 5 * 1024]
        end
        vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--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
  config.vm.box = "debian/buster64"
end
технология_vagrant.txt · Последние изменения: 2020/10/16 16:40 — val