Vagrant dhcp:获取IP地址 [英] Vagrant dhcp: get ip addresses
问题描述
美好的一天!
我想使用 dhcp 增加几个节点.但我也想获取这个节点的 ip 地址并将它们写入文件.Vagrant 文档说IP 地址可以通过使用 vagrant ssh SSH 进入机器并使用适当的命令行工具查找 IP 来确定,例如 ifconfig".
Good day!
I want to up few nodes using dhcp. But I also want to get ip-addresses of this nodes and write them to file. Vagrant docs says "The IP address can be determined by using vagrant ssh to SSH into the machine and using the appropriate command line tool to find the IP, such as ifconfig".
所以我为 master 创建了一个简单的 bash 脚本
So I created a simple bash script for master
`vagrant ssh master -c 'ifconfig | grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | tail -n 2 | head -n 1'`
以及其他节点的相同脚本.
and the same scripts for another nodes.
我想把这个脚本放到 Vagrantfile 中.我应该使用什么插件?我尝试 https://github.com/emyl/vagrant-triggers.
I want to put this scripts into Vagrantfile. What plugin should I use? I tries https://github.com/emyl/vagrant-triggers.
config.trigger.after :up do
ipAddr = `vagrant ssh master -c 'ifconfig | grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | tail -n 2 | head -n 1'`
puts "master ipAddr #{ipAddr}"
ipAddr = `vagrant ssh slave01 -c 'ifconfig | grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | tail -n 2 | head -n 1'`
puts "slave01 ipAddr #{ipAddr}"
end
但是当其中一个节点启动时它会触发,而不是两个都启动.
But it fires when one of the nodes is up, rather then both.
推荐答案
我修改了您的方法以使用 vagrant-triggers 插件.以下是对我有用的方法:
I modified your approach to work on a multi-box setup with the vagrant-triggers plugin. Here's what worked for me:
# Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network :private_network, type: "dhcp"
config.vm.define "test-web"
config.vm.define "test-db"
config.vm.define "test-dual"
config.trigger.after :up, :stdout => false, :stderr => false do
get_ip_address = %Q(vagrant ssh #{@machine.name} -c 'ifconfig | grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | tail -n 2 | head -n 1')
@logger.debug "Running `#{get_ip_address}`"
output = `#{get_ip_address}`
@logger.debug "Output received:\n----\n#{output}\n----"
puts "==> #{@machine.name}: Available on DHCP IP address #{output.strip}"
@logger.debug "Finished running :after trigger"
end
end
# Console:
$ vagrant up test-web
Bringing machine 'test-web' up with 'virtualbox' provider...
==> test-web: Checking if box 'ubuntu/trusty64' is up to date...
==> test-web: Resuming suspended VM...
==> test-web: Booting VM...
==> test-web: Waiting for machine to boot. This may take a few minutes...
test-web: SSH address: 127.0.0.1:2222
test-web: SSH username: vagrant
test-web: SSH auth method: private key
test-web: Warning: Connection refused. Retrying...
==> test-web: Machine booted and ready!
==> test-web: Running triggers after up...
Connection to 127.0.0.1 closed.
==> test-web: Available on DHCP IP address 172.28.128.3
这篇关于Vagrant dhcp:获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!