How to capture the IP address of the guest machine in Vagrant - vagrant

I am working on a project where I am creating rackspace cloud instances using Vagrant. I am using vagrant-rackspace plugin to do it. Everything works fine for me except the part that I need to capture the IP of the new rackspace instance created in the vagrantfile.
I need this IP address in order to add it to the Ansible inventory file (/etc/ansible/hosts)
Here is my Vagrantfile:
Vagrant.configure(2) do |config|
config.ssh.private_key_path = "rackspace_rsa"
config.vm.provider :rackspace do |rs|
rs.username = "rackspace-user"
rs.api_key = "a98weqrq3r34ewfadsf43rffa4697cc0036"
rs.admin_password = "Password1"
rs.flavor = /1 GB Performance/
rs.image = /Ubuntu 12.04/
rs.rackspace_region = :dfw
rs.server_name = "ansible_server_1"
rs.public_key_path = "rackspace_rsa.pub"
end
end
Once the instance is created I am going to provision the machine using Shell provisioning to install ansible. After installing ansible I will update the inventory file to reflect the IP of the newly created rackspace instance.

You could use the Vagrant Host Manager plugin using the Custom IP Resolver
Custom IP resolver gives you oportunity to calculate IP address for each machine by yourself, giving You also access to the machine that is updating /etc/hosts. For example:
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if hostname = (vm.ssh_info && vm.ssh_info[:host])
`host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
end
end

#FrédéricHenri I am adding this as an answer since it is too long for a comment.
Well, my plan was to spin up three virtual machines on rackspace (VM-ONE,VM-TWO and VM-THREE).
-> I installed vagrant on my macbook
-> Wrote a vagrantfile to spinup the first box(VM-ONE)
-> install vagrant and ansible on VM-ONE using vagrant shell provisioning
-> Write vagrantfile to spin up VM-TWO and VM-THREE
-> Write playbook to provision VM-TWO and VM-THREE
-> Run "vagrant up" on VM-ONE
-> Use vagrant ansible to provision VM-TWO and VM-THREE.
I hit a roadblock here.
I needed to create an inventory file which contains IPs of VM-TWO and VM-THREE.
I figured that Vagrant provides a default inventory file that contains the IPs of the two instances it creates and ansible will use that inventory file while running the playbook.
The inventory file auto generated by vagrant sits at .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory.
So my original problem is resolved now. Thank you.

Related

How do you set the VM name via Vagrant?

I've set up 4 VMs in Vagrant and now trying to set the name of a VM in Vagrant as I don't just want to ssh into the default VM.
I can't find any docs on the vagrant website but found this:
How to change Vagrant 'default' machine name?
However, when I try:
config.vm.define "foohost"
and do a vagrant up I get:
There are errors in the configuration of this machine. Please fix
the following errors and try again:
vm:
* The following settings shouldn't exist: define
I suspect you actually wrote
config.vm.define = "foohost"
rather than
config.vm.define "foohost"
as that would explain the error message. (Best to show several lines of actual source if you can.)
as I don't just want to ssh into the default VM.
so when you use multiple VM, you can tell vagrant which VM you want to ssh by default, using the following
config.vm.define "foohost", primary: true do |foohost|
...
end
then when you run vagrant ssh you will ssh by default in this VM.
To ssh into the other MV, you will need to specify the VM name

Ubuntu - vagrant access from another computer

I have recently learned how to setup a virtual machine using vagrant (virtualbox), and I know how to access it from a web browser via the local adress such as 192.168.x.x:9292 since 9292 is the default gate. But I would like to access the server from another computer locally.
My question is if I could access it via cmd as I connect to it on my own with the command vagrant ssh but from another computer via a command like vagrant ssh 192.168.x.x?
As far as I understand you have to use external programs or setup something inside the vagrant environment? Is there a simple way to access the server or is it password protected? If I setup a website I can access that for example from another local machine but can I access files on the server (from another computer)?
I found how to connect to another computer locally by entering the vagrant environment, vagrant up --> vagrant ssh. And connected to another computer by typing in ssh vagrant#192.168.x.x where 192.168.x.x is the local address to the computer. The vagrant asks for a password and you type in the default password which I believe is vagrant and then you are connected to the other vagrant instance.
Assuming you have virtualbox provider for Vagrant you can achieve this by doing bridging the VBox network interface with Host.
Use this in Vagratfile (for public/private)
For Public
Vagrant.configure("2") do |config|
config.vm.network "public_network",
use_dhcp_assigned_default_route: true
end
For Private
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
end
above code will create IP in the range of Host network and using this you can access the file server from another machine.

Is it possible to provision vagrant boxes from puppet modules located outside of the vagrant folder?

I have a production server architecture that is quite complex that involves several types and tiers of servers including: Web, Varnish, HAProxy, ELK, Database, Sensu, etc. The production architecture is provisioned with puppet masters and the puppet code for the masters is provisioned with Git through many, submodules.
I want to connect these Git repos to VM's locally to further develop pieces of the puppet architecture. However each Vagrant VM requires a complete self contained copy of all of the Git repos in order to stand up just one VM. This seems like a very inefficient use of drive space for local development on what will be potentially a dozen different types of servers.
Is there a way to point all of the Vagrant VM's VagrantFiles to a common local folder outside of the vagrant directory such that each Vagrant instance can still read the folder and provision the server?
Edited --
Based on comment from #Treminio, here is the portion of my VagrantFile showing the attempt to declare an absolute path from host computers root:
config.vm.provision "puppet" do |puppet|
puppet.manifest_file = "init.pp"
puppet.manifests_path = "/Users/jdugger/vm/puppet/manifests"
puppet.module_path = "/Users/jdugger/vm/puppet/modules"
puppet.hiera_config_path = "/Users/jdugger/vm/puppet/hieradata"
end
... and the error response by Vagrant:
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /Users/jdugger/vm/pupt/vagrant
default: /vagrant_data/scripts => /Users/jdugger/scripts
default: /tmp/vagrant-puppet/modules-169f1d27ef31a534405e2e9fcde2eedf => /Users/jdugger/vm/puppet/modules
default: /tmp/vagrant-puppet/manifests-be5a69bfb646cf9329b8921f221ffab8 => /Users/jdugger/vm/puppet/manifests
==> default: Running provisioner: puppet...
The `puppet` binary appears not to be in the PATH of the guest. This
could be because the PATH is not properly setup or perhaps Puppet is not
installed on this guest. Puppet provisioning can not continue without
Puppet properly installed.
This response may not be because of the puppet path - not sure - It appears that puppet is not on the guest box (isn't it supposed to be running from Vagrant?). No version comes up when with:
[vagrant#localhost ~]$ puppet --version
Response is:
-bash: puppet: command not found
Update ---
#Treminio is correct. I have been able to provision with puppet manifests and modules external to the vagrant/ directory. The Path problem appears to be because Puppet is not installed on the guest VM. To resolve this I added a shell script found here:
http://garylarizza.com/blog/2013/02/01/repeatable-puppet-development-with-vagrant/
This was added just before the puppet provisioning declaration. Just as a note there doesn't seem to be a lot of advanced examples that demonstrate the external file capability or that you need to install puppet outside of the puppet provisioner.
However each Vagrant VM requires a complete self contained copy of all of the Git repos in order to stand up just one VM.
This is incorrect.
What you want to change is the puppet.module_path value.
It can be any location on your host's disk, and Vagrant will automatically mount it inside your VM.

Switch IP's in browser

For my development environment I'm looking for a way to change/override IP addresses used in DNS lookup for a domain, so I can point the domain to my local vagrant box with ease and without changing the hostfile everytime I want to switch back and forth from the live env to the dev env. I couldn't find a way to do this so I'm hoping there is a solution for this. I'm using Mac OS X.
Perhaps the Vagrant Hostsupdater plugin can be of help. It automatically adds entries to your local hosts file, when you include the following config in your Vagrantfile:
config.vm.network :private_network, ip: "192.168.3.10"
config.vm.hostname = "www.testing.de"
config.hostsupdater.aliases = ["alias.testing.de", "alias2.somedomain.com"]
Source

Using Vagrant, access app on host machine from inside Vagrant

I am running a Wordpress app inside a Vagrant box that needs to access a Rails app running on my host machine (using Pow). How do I do that?
I can access the Rails app on my host machine using myapp.dev and would like to access it from inside Vagrant as well, in the best case using the same domain.
All info I could find on that topic is about the other way around, where you want to access an app running inside Vagrant from the host machine.
So it just sounds like you need a host entry on your guest machine pointing back your host machine?
You could do something like the following:
Vagrant.configure("2") do |config|
require 'socket'
local_ip = UDPSocket.open {|s| s.connect("64.233.187.99", 1); s.addr.last}
config.vm.provision "shell", inline: "echo \"#{local_ip} myapp.dev\" >> /etc/hosts"
end
This should use a socket to get your host machines IP address and then using a shell provisioner it will append that IP address to your hosts file which should allow your VM to use the domain name you want to communicate with your host OS.

Resources