0

I am using Vagrant and Openstack as my provider. I could create successfully create openstack instances using my Vagrant script. I am using

os.floating_ip  = :auto

for assigning floating IP address to my machine. How can I get newly created machine info within the same Vagrant script after it has been created. Here I want to get the floating IP address that has been assigned automatically to that machine.

Following is my script

Vagrant.configure("2") do |config|

config.vm.box = "dummy"
config.vm.synced_folder '.', '/vagrant', disabled: true

config.ssh.private_key_path = "/home/xxxxx"

config.vm.provider :openstack do |os|
os.username     = "xxx"
os.api_key      = "xxx"
os.flavor       = "m1.medium"
os.image        = "ubuntu14.04"
os.endpoint     = "http://xxx"
os.keypair_name = "xxx"
os.floating_ip  = :auto
os.floating_ip_pool   = "xxx"
os.ssh_username = "ubuntu"
os.network      = "xxx"
os.server_name  = "TestInstant"
end
end
Malintha
  • 4,512
  • 9
  • 48
  • 82

1 Answers1

1

I guess you mean to get the information from the host so the most simple is probably to run vagrant ssh-config, this gives you ssh related information including the host name:

Host default
  HostName 172.16.42.206

If you want to get this information while you boot the machine, you can add a shell provisioner:

config.vm.provision "shell", inline: "hostname", run: "always"
config.vm.provision "shell", inline: "ip addr show", run: "always" 

you will get those information

==> default: Running provisioner: shell...
    default: Running: inline script
==> default: stdin: is not a tty
==> default: precise32
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: stdin: is not a tty
==> default: 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
==> default:     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
==> default:     inet 127.0.0.1/8 scope host lo
==> default:     inet6 ::1/128 scope host
==> default:        valid_lft forever preferred_lft forever
==> default: 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
==> default:     link/ether 08:00:27:12:96:98 brd ff:ff:ff:ff:ff:ff
==> default:     inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
==> default:     inet6 fe80::a00:27ff:fe12:9698/64 scope link
==> default:        valid_lft forever preferred_lft forever

Obviously you can run simple command or run a script (read more on shell provisioner if needed) to get the information you need

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Yeah that terminal command gives all details about the instance. I have a another part in my question. I want to get this information just after the machine creation completion. So is there anyway to use this command in my Vagrantfile ? – Malintha Sep 15 '15 at 06:04
  • 1
    I added an example with shell provisioner to output the information when you boot the vm, you should be able to run the command needed to get the information needed without running vagrant ssh-config – Frederic Henri Sep 15 '15 at 07:30
  • Can we do this in ruby script. I can use vagrant ssh-config to get the IP address and other details. But there I have to use shell script. Is there anyway to use this in ruby script ? – Malintha Sep 15 '15 at 09:53
  • you can add `system "vagrant ssh-config" if ARGV[0] == 'up'` but it will try to execute before the machine if fully ready, if you really want that you can look at vagrant plugin - [read also this answer](http://stackoverflow.com/a/31746868/4296747) – Frederic Henri Sep 15 '15 at 10:01