Reputation: 684
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".
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.
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.
Upvotes: 1
Views: 3830
Reputation: 3554
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
Upvotes: 2