If you’re dealing with multiple-VMs and you know the IP’s you want to use 
inside the config block, you can use Ruby to create your inventory file for you:

      File.open('myInventory' , 'a') do |f|
        f.write "#{hostname} ansible_ssh_host=#{private_ip} 
ansible_ssh_user=vagrant 
ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key\n"
      end

This will generate inventory lines to match your config: 

head0001 ansible_ssh_host=10.16.0.201 ansible_ssh_user=vagrant 
ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key


More complete example here (in particular, using a hash to clearly map & 
generate names & IPs):

Vagrantfile: https://gist.github.com/d669ada085e50fd64bd7
resulting inventory file: https://gist.github.com/474c1d6a0e759611e29e
Proof that it all works (: https://gist.github.com/6a32603b9a21974f8078 
(outside vagrant: https://gist.github.com/f1c91b95b7cbe80e1624)
https://github.com/simonmcc/multi-vm-ansible

HTH,

Simon.


-- 
Simon McCartney
+447710836915

On 4 August 2015 at 08:59:43, Joaquin Menchaca ([email protected]) wrote:

This requires editing the local system's hosts files.  I am trying to avoid 
that scenario and create a Vagrant + Ansible environment that works on any 
system (no hosts file needed) w/o need for root access, and I wanted to plug in 
the appropriate information to simulate a real world environment where users 
would have a DNS server that correspond to the inventory file.  To do this, I 
wanted to custom generate an inventory file on the fly, using the current ssh 
configuration.

My Vagrant would look something like this:

  config.vm.hostname = "wheezybox"
  config.vm.provision :ansible do |ansible|
    ansible.playbook = "../../provisioning/playbook.yml"
   #  ansible.inventory_path = "../../provisioning/inventory.ini"
   ansible.sudo = true
  end

I would like to have code before the # comment. 

In deep diving into Vagrant 1.7.4 code, I can see there's some 
machine.ssh_info, but I don't know how to get at this. 

On Monday, August 3, 2015 at 11:37:24 PM UTC-7, Alvaro Miranda Aguilera wrote:
Hello

Can you check these examples?

Since they don't require anything special:

# Enable provisioning with Ansible.
config.vm.provision "ansible" do |ansible|
ansible.playbook = "provisioning/main.yml"
end


https://github.com/geerlingguy/ansible-vagrant-examples/blob/master/docker/Vagrantfile



On Tue, Aug 4, 2015 at 6:17 PM, Joaquin Menchaca <[email protected]> wrote:
> Hello.
>
> I wanted to do host base configuration using hostname, and then dynamically
> populating inventory file with appropriate guest SSH port and private key
> path.
>
> Vagrant Ansible Provisioner doesn't work if you rely on it to auto-generate
> the inventory file, as it uses 127.0.0.1 for hostname.  For example, it'll
> create a file /path/to/vagrant/.vagrant/provisioners/ansible/inventory/:
>
> default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200
> ansible_ssh_private_key_file=/path/to/vagrantdir/.vagrant/machines/default/virtualbox/private_key
>
> I tried experimenting retrieving values (newbie working with vagrant ruby
> code), testing something like
>
>   puts "config.ssh.private_key_path=#{config.ssh.private_key_path}"
>   puts "config.ssh.guest_port=#{config.ssh.guest_port}"
>
> But his returns objects, and I am not sure what to do.  Any suggestions?
>
> --
> This mailing list is governed under the HashiCorp Community Guidelines -
> https://www.hashicorp.com/community-guidelines.html. Behavior in violation
> of those guidelines may result in your removal from this mailing list.
>
> GitHub Issues: https://github.com/mitchellh/vagrant/issues
> IRC: #vagrant on Freenode
> ---
> You received this message because you are subscribed to the Google Groups
> "Vagrant" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/vagrant-up/a7036744-42ff-4f66-b650-54e94a3b63cb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
This mailing list is governed under the HashiCorp Community Guidelines - 
https://www.hashicorp.com/community-guidelines.html. Behavior in violation of 
those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups 
"Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vagrant-up/2bce9691-b844-48f6-9c44-9c0fdbe1319d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
This mailing list is governed under the HashiCorp Community Guidelines - 
https://www.hashicorp.com/community-guidelines.html. Behavior in violation of 
those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
--- 
You received this message because you are subscribed to the Google Groups 
"Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vagrant-up/etPan.55c08370.b65460f.1153%40Simons-MacBook-Pro.local.
For more options, visit https://groups.google.com/d/optout.

Reply via email to