Reputation: 994
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.
Upvotes: 1
Views: 2507
Reputation: 994
@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.
Upvotes: 0
Reputation: 53703
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
Upvotes: 1