vagrant icon indicating copy to clipboard operation
vagrant copied to clipboard

Setting for the ansible provisioner to specify the location of generated inventory file

Open discopatrick opened this issue 9 years ago • 6 comments

This is a suggested enhancement, that I'm willing to contribute to.

I use the vagrant ansible provisioner. My projects usually begin with me using the vagrant boxes only, at which time I set the generated inventory file at .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory as my inventory (in ansible.cfg). This is because I often like to run my playbooks manually via ansible-playbook, rather than automatically via vagrant up or vagrant provision.

Later in the project, I begin to add remote servers into my ansible project. What this usually means is creating my remote server inventory files in a hosts/ directory, and then changing my ansible.cfg setting to point to this new directory. However, I sometimes want to run playbooks against both my vagrant and remote hosts, and so I still need the information in the generated inventory file. The problem is, you can't specify multiple inventory locations in ansible - you can only specify a single file or directory.

Therefore, the enhancement I'm suggesting is that there should be a setting for the vagrant ansible provisioner that allows you to specify the location of your generated inventory file - which I would then set to my ansible hosts/ dir.

For perspective, I have also come up 4 other possible solutions:

  1. Duplicate the generated file into my hosts/ dir - not ideal, as it's not very DRY, and my duplicate won't update itself if the information changes.
  2. Create a symlink inside my hosts/ dir pointing to the generated file - this has worked well in most cases, but there is an edge case where running the same ansible project on a remote host via ansible pull will complain that the symlinked file doesn't exist (because vagrant provision hasn't been run there).
  3. Write a dynamic inventory script (placed in my hosts/ dir) that observes the generated file and returns the correct values based on that.
  4. Extend ansible to allow for multiple inventory directories (it currently only allows you to set one file or dir).

I'm posting here to gauge interest and get feedback on this possible enhancement. I'm happy to get stuck in myself and produce a pull request, if the vagrant team would be open to including this feature. I don't have a lot of experience with ruby, but it seems quite a simple feature to get started with, and I'd be happy to follow guidance from the team to make sure the code is of acceptable quality.

discopatrick avatar Jul 21 '16 14:07 discopatrick

I have a similar problem; but have existing inventories for real deployments made with ansible that I wan to be able to re-create as vagrant images (to test before we deploy to Live). The ansible inventories contain key information like the groups that the host belongs to meaning that we need to go through a lengthy and error prone process of copying the inventory config into a Vagrant file - and given the number of deployments we are working with; this needs automation.

My solution to this is to extend the ansible.force_remote_user concept with ansible.force_remote_host and ansible.force_remote_port; meaning that all settings are taken from the inventory; but the provisioning is done via the default vagrant adapter. (127.0.0.1, with an allocated port forwarded to ssh (22)).

ghost avatar Dec 17 '16 23:12 ghost

:+1: The solution with the symlink would work if only vagrant would keep generating the inventory file if i manually specify the inventory_path. So one could create a symlink inside his inventory_path to the vagrant's inventory file. He's now able to do more than only the inventroy (like group_vars) inside the inventory path.

mmack avatar Jan 10 '17 12:01 mmack

I need something like this as well.

In my instance, I utilize 3 different Vagrantfiles, one for each environment I have…

  • Dev (local)
  • Beta (remote)
  • Production (remote)

I've made simple bash scripts that call vagrant with the proper ENV set for Vagrantfile, like so...

#!/bin/bash
# Allows use of separate Vagrant files — one per RAILS_ENV / hosting config
export VAGRANT_VAGRANTFILE="Vagrantfile-production";
vagrant $@;

...But the problem with this setup is, I have to call vagrant provision or v-beta provision (beta/staging env) to re-generate the dynamic inventory each time. I've taken the shortcut like you have, by symlinking the dynamic inventory hostfile, but that doesn't give me much confidence I might run an ansible-playbook call on the wrong hosts.

I've also got a pretty extensive Ansible playbook setup, with plays for deployment, configuration, provisioning, networking, etc — so generally I'm calling ansible-playbook -i ansible/hosts.txt ansible/deploy.yml or whatever a bunch of different times.

To make this process more transparent I think there are a few things that could be done.

  1. Allow a custom path to be set for location of generated Ansible inventory host file (as you suggest)
  2. Inventory file generation should happen on every Vagrant call (up, down, status, provision...) this would mean it's always up to date and you could have peace of mind simply by calling "vagrant status" before "ansible-playbook -i ansible/hosts.txt ansible/yourplay.yml"
  3. Instead of the above, provide us with a new command for Ansible provisioner to generate a custom hostfile? Perhaps: vagrant provision --ansible-write-inventory=(path)

I was digging around in the code today, to see if I could possibly call VagrantPlugins::Ansible::Provisioner::Base.new().generate_inventory from the Vagrantfile and write those files myself, but to my knowledge it's impossible to do that. Anyone have tips or ideas?

subimage avatar May 25 '17 23:05 subimage

This is a duplicate of this issue:

https://github.com/mitchellh/vagrant/issues/7552

DerekTBrown avatar Sep 07 '17 05:09 DerekTBrown

@DerekTBrown That's not a duplicate. @discopatrick is describing a very different use case.

I'm actually facing the same challenges as Patrick and would love to see either a Vagrantfile option or a commandline parameter to have Vagrant put the generated inventory in a different filesystem location.

megamorf avatar Sep 14 '18 17:09 megamorf

The issue description says that the requirement is to be able to have both the automatically-generated inventory and an additional inventory at the same time, as far as I can tell. I was able to get this to work by following the suggestion here. Essentially, tell Ansible to use the additional inventory source without telling the Vagrant Ansible provisioner to use it as an inventory (setting ansible.inventory_path causes the automatically-generated inventory to be suppressed):

ansible.raw_arguments = ["--inventory", YOUR_INVENTORY_PATH ]

ianhinder avatar May 12 '25 22:05 ianhinder