Vagrant - 将IP分配给阵列中的盒子

时间:2016-10-11 21:23:35

标签: arrays networking vagrantfile

我是Ruby编程中的总菜鸟,即使我(希望我)理解匿名代码块的想法,我也不能说我明白我在下面的地方失败了:

MACHINE_MASTER_IP = ENV['MACHINE_MASTER_IP'] || '10.0.3.10'
MACHINE_IPS = ENV['MACHINES_IPS'] || "#{MACHINE_MASTER_IP},10.0.3.20"

$MACHINE_IPS = MACHINE_IPS.split(',')

Vagrant.configure(2) do |config|

$i = 0
while $i < $MACHINE_IPS.length

    $MACHINE_IP = $MACHINE_IPS[$i]
    $MACHINE_NAME="worker_#$1"

    config.vm.define $MACHINE_NAME do |box|

    # --provider virtualbox 
    box.vm.provider :virtualbox do |virtualbox, override|
        override.vm.network "private_network", ip: "#$MACHINE_IP"
        # virtualbox.memory = 2048
        # virtualbox.cpus = 1
        override.vm.box = "ubuntu/trusty64" # 14.04
        # override.vm.box = "ubuntu/trusty64" # 14.04
    end

    # config.vm.define
    end

    $i += 1

# while $i < $MACHINE_IPS.length
end

end

我想要做的是动态生成并运行一组机器,我想在这些机器上应用从命令行给出的一组ips。 Smth喜欢:

MACHINE_IPS="192.168.0.1,192.168.0.2,192.168.0.3" vagrant up

我上面的代码中的问题是我创建的每台机器(是的,它创建了所有3台)都将使用最后一个ip(192.168.0.3)进行设置。

说实话,我真的不知道该怎么办,所以我正在寻求你的帮助。提前谢谢。

2 个答案:

答案 0 :(得分:0)

似乎我必须为自己回答。在我问了几天后找到了答案。

我使用1..n。每个块代码语法,而不是使用while。

MACHINE_MASTER_IP = ENV['MACHINE_MASTER_IP'] || '10.0.3.10'
MACHINE_IPS = ENV['MACHINES_IPS'] || "#{MACHINE_MASTER_IP},10.0.3.20"

$MACHINE_IPS = MACHINE_IPS.split(',')

Vagrant.configure(2) do |config|

(1..$MACHINE_IPS.length).each do |i|

    $MACHINE_IP = $MACHINE_IPS[$i]
    $MACHINE_NAME="worker_#$1"

    config.vm.define $MACHINE_NAME do |box|

    # --provider virtualbox 
    box.vm.provider :virtualbox do |virtualbox, override|
        override.vm.network "private_network", ip: "#$MACHINE_IP"
        # virtualbox.memory = 2048
        # virtualbox.cpus = 1
        override.vm.box = "ubuntu/trusty64" # 14.04
        # override.vm.box = "ubuntu/trusty64" # 14.04
    end

    # config.vm.define
    end

    $i += 1

# while $i < $MACHINE_IPS.length
end

end

通过一些调整,一切都像魅力一样。

答案 1 :(得分:0)

Vagratfile遵循ruby语法,下面对我来说很好用

# -*- mode: ruby -*-
# vi: set ft=ruby :

users = ["puser", "guser", "kuser"]
uid_start = 10000
Vagrant.configure("2") do |config|
  config.vm.define "machinea" do |machinea|
    machinea.vm.box = "ubuntu/bionic64"
    machinea.vm.box_version = "20200618.0.0"
    machinea.vm.box_check_update = false
    machinea.vm.boot_timeout = 300
    machinea.vm.hostname = "machinea"
    machinea.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", auto_config: false
    machinea.vm.provision "shell", run: "always", inline: "ifconfig enp0s8 192.168.0.17 netmask 255.255.255.0 up"
    machinea.ssh.insert_key = false
    machinea.ssh.private_key_path = ['~/.vagrant.d/insecure_private_key', '/myworks/vagrant/.ssh/vagrant_rsa']
    machinea.vm.provision "file", source: "/myworks/vagrant/.ssh/vagrant_rsa.pub", destination: "~/.ssh/authorized_keys"
    machinea.vm.provision "shell", inline: <<-EOC
      sudo sed -i -e "\\#PasswordAuthentication yes# s#PasswordAuthentication yes#PasswordAuthentication no#g" /etc/ssh/sshd_config
      sudo systemctl restart sshd.service
      echo "finished"
    EOC
    users.each do |user|
        uid_start += 1
        machinea.vm.provision "shell", run: "once", privileged: true, inline: <<-CREATEUSER
        sudo useradd -m -s /bin/bash -U #{user} -u #{uid_start}
        sudo mkdir /home/#{user}/.ssh
        sudo cp /vagrant/.ssh/#{user}_rsa.pub /home/#{user}/.ssh/authorized_keys
        sudo chown -R #{user}:#{user} /home/#{user}
        sudo su
        echo "%#{user} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/#{user}
        exit
      CREATEUSER
    end
  end
end