OkieOth
OkieOth

Reputation: 3704

How can I customize the output while 'vagrant up' runs?

I want to inform the vagrant user what ip address the used machine has. My first idea was to use '/etc/rc.local' and print the output of ifconfig to a file in the '/vagrant' directory, but it seems this directory is mounted after rc.local is called. So I need another way to inform the user without any ssh login.

My second idea is to write the ifconfig output to some "place" where it is shown in the vagrant start up output ... like in sample below.

...
default: Guest Additions Version: 4.3.10
default: VirtualBox Version: 5.0
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /home/user/vagrant/test
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
# start of desired output
Adresses found:
10.0.2.15
172.28.128.3
# end of desired output
==> default: flag to force provisioning. Provisioners marked to run always will still run.
...

All ideas are welcome.

Upvotes: 0

Views: 2023

Answers (3)

Brian Brownton
Brian Brownton

Reputation: 1331

You may be interested in this SO answer, which attempts to achieve the same thing of outputting the network interface's IP to terminal on vagrant up.

Relevant bits from the answer -

On the Guest:

/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}

Should get you something like this:

10.0.2.15

Which could then be used in your Vagrantfile like so:

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

The trick here is knowing which interface (eth0 in the example above) has the IP you want. Of course if you are great with grep or awk you could modify that first command to check the IPs on all the interfaces... but that's beyond my abilities.

# content of Vagrantfile
$infoScript = <<SCRIPT
  echo 'IP-addresses of the vm ...'
  ifconfig | grep 'inet A' | grep Bcast | awk '{print $2}' | sed 's/addr://'
SCRIPT

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.box_check_update = false
  config.vm.network "private_network", type: "dhcp"
  config.vm.network "forwarded_port", guest: 80, host: 8888
  config.vm.provider "virtualbox" do |vb|
     vb.name = "demo"
     vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
     vb.memory = "2048"
     vb.cpus = 2
  end
  # normal provision to set up the vm
  config.vm.provision "shell", path: "scripts/bootstrap.sh"
  # extra provision to print all the ip's of the vm
  config.vm.provision "shell", inline: $infoScript,
      run: "always"
end

EDIT You may also be interested in the vagrant-hostmanager plugin if the purpose of echoing the IP is just so you can connect to the box, this plugin can modify your /etc/hosts on your guest or host so you don't need to worry about the IP and instead use something like http://mydevbox.local

Upvotes: 1

OkieOth
OkieOth

Reputation: 3704

Because the other answer don't describes the way how to publish dynamic data from the inside of the vm, I write my solution.

Step 1: I put a webserver in the vagrant vm and publish guest port 80 to host port 8888

Step 2: prepare /etc/rc.local of guest so it gathers all needed dynamic vm information and write the output to webserver root in a file 'info.txt'

Step 3: add a second provision entry to vagrantfile that runs every 'vagrant up' and informs the user where he can get more information

# Vagrantfile
...
config.vm.provision "shell", path: "scripts/bootstrap.sh"
config.vm.provision "shell", inline: "echo 'check http://localhost:8888/info.txt for more information'",
  run: "always"
...

Upvotes: 1

Dup Step
Dup Step

Reputation: 218

First you will need to determine how the IP address is acquired (i.e. DHCP or static). Then based on that you could essentially just add the private networking code to the vargrantfile as such:

DHCP:

Vagrant.configure("2") do |config|
  config.vm.network "private_network", type: "dhcp"

end

Static:

Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.50.4"

end

Then you could add a mixture of shell and ruby:

$script = <<SCRIPT
  echo private_network...
  ip: "192.168.50.4"
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: $script

end

Hope that helps enough to get you going.

Upvotes: 1

Related Questions