title: Dynamic inventory for Vagrant's Ansible provisioner?
body: <p>I am currently trying to write a dynamic inventory into the run portion of my Vagrant script, but am unable to enumerate things such as <code>vm_name</code> and <code>ip_address</code>. I know this is simply because I am trying to <code>f.write</code> the wrong lookup, unfortunately I do not know the variable I should be calling? You can see what I am attempting in the commented out section of my <code>Vagrantfile</code> towards the bottom.</p>
  
  <pre class="lang-rb prettyprint-override"><code>Vagrant.configure(2) do | config |
    N = 2
  
    ANSIBLE_RAW_SSH_ARGS  = []
    ANSIBLE_INVENTORY_DIR = "provisioner/inventory"
    VAGRANT_VM_PROVIDER   = "virtualbox"
  
    (1..N-1).each do | machine_id |
      ANSIBLE_RAW_SSH_ARGS &lt;&lt; "-o IdentityFile=.vagrant/machines/machine#{machine_id}/#{VAGRANT_VM_PROVIDER}/private_key"
    end
  
    (1..N).each do | machine_id |
      config.vm.define "machine#{machine_id}" do | machine |
  
        machine.vm.box      = "ubuntu/trusty64"
        machine.vm.hostname = "machine#{machine_id}"
  
        machine.vm.network "private_network",
          ip: "192.168.77.#{10+machine_id-1}"
  
        # only execute once the Ansible provisioner, when all the machines are up and ready.
        if machine_id == N
          machine.vm.provision :ansible do | ansible |
            # Disable default limit to connect to all the machines
            ansible.limit           = "all"
            ansible.playbook        = "provisioner/test.yml"
            ansible.inventory_path  = "#{ANSIBLE_INVENTORY_DIR}/vagrant"
            ansible.verbose         = "-v"
  
            ansible.raw_ssh_args    = ANSIBLE_RAW_SSH_ARGS
          end
          # dynamically create the Ansible inventory file
          Dir.mkdir(ANSIBLE_INVENTORY_DIR) unless Dir.exist?(ANSIBLE_INVENTORY_DIR)
          # File.open("#{ANSIBLE_INVENTORY_DIR}/vagrant" ,'w') do | f |
          #   f.write "[#{machine_id['vm_name']}]\n"
          #   f.write "#{machine_id['ip_address']}\n"
          # end
        end
      end
    end
  end
  </code></pre>
  
  <p><em>test.yml</em> if anyone cares</p>
  
  <pre><code>---
  - hosts: all
    gather_facts: false
    tasks:
    - command: hostname -f
  </code></pre>