Help: Autogenerating Ansible Inventory advice

298 views
Skip to first unread message

Joaquin Menchaca

unread,
Aug 4, 2015, 2:17:59 AM8/4/15
to Vagrant
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?

Alvaro Miranda Aguilera

unread,
Aug 4, 2015, 2:37:24 AM8/4/15
to vagra...@googlegroups.com
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
> --
> 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 vagrant-up+...@googlegroups.com.
> 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.

Joaquin Menchaca

unread,
Aug 4, 2015, 3:59:39 AM8/4/15
to Vagrant
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. 

Simon McCartney

unread,
Aug 4, 2015, 5:19:11 AM8/4/15
to vagra...@googlegroups.com, Joaquin Menchaca
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):


HTH,

Simon.


-- 
Simon McCartney
+447710836915

Joaquin Menchaca

unread,
Aug 4, 2015, 11:01:48 AM8/4/15
to Vagrant, joaqu...@gmail.com
Simon.  I must say I really like this one, I may use this idea in the future.

My req for this one is a single system, but I may have separate Vagrant directories per distro/version (wheezy, precise, centos6).  I could then handcraft the inventory, just like you did, but need to at least feed in the values from the Vagrantfile, but not sure how to use their library.

In looking through the Vagrant's code, it seems the values are write-only?  It has Vagrant.configure(version, &block), which later I guess parses and sets appropriately outside of this block.  This gives me an idea, if I just manually set the port, I can then refer to it elsewhere.

Reply all
Reply to author
Forward
0 new messages