Qui*_*ido 2 virtualbox dhcp vagrant
我正在用 Vagrant 创建一个 VirtualBox 机器。在配置中,我需要知道 VirtualBox DHCP 分配的框的 IP 地址。有没有办法在 Vagrantfile 中使用分配的 IP 地址?
Vagrant.configure(2) do |config|
# ... some more config
config.vm.network "private_network", type: "dhcp"
# ... some more config
config.vm.provision "shell", inline: <<-SHELL
sudo -i /vagrant/my_provisioning_script.sh <insert ip here>
SHELL
end
Run Code Online (Sandbox Code Playgroud)
你可以ifconfig
在 vagrant box 里面做一个来获取你的盒子的 ips;应该是 eth0 中的 ip,但是由于您在配置中需要它并且您正在使用 shell,您可以执行以下操作:
/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
Run Code Online (Sandbox Code Playgroud)
在我的流浪箱中,这个输出
10.0.2.15
Run Code Online (Sandbox Code Playgroud)
所以你的命令可能是
config.vm.provision "shell", inline: <<-SHELL
sudo -i /vagrant/my_provisioning_script.sh $(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
SHELL
Run Code Online (Sandbox Code Playgroud)
我用 ping 尝试了上面的方法。
如果需要,您也可以定义 ip,编辑您的 Vagrantfile 并添加:
config.vm.network "public_network", ip: "192.168.0.23"
Run Code Online (Sandbox Code Playgroud)
这会将那个 ip 分配给你的盒子。如果你这样做ifconfig
,它将被列在 eth1 上。所以你可以在你的脚本上硬编码 IP。
归档时间: |
|
查看次数: |
8892 次 |
最近记录: |