Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable configuring the machine's hostname #14

Closed
wants to merge 1 commit into from

Conversation

untoldone
Copy link

This enables a vagrant user to create a more realistic test environment if they use the name of their hosts in the inventory file (e.g. if they use the inventory file to create bind9 config)

This enables a vagrant user to create a more realistic test environment if they use the name of their hosts in the inventory file (e.g. if they use the inventory file to create bind9 config)
@command-tab
Copy link

👍 Patch works great for me.

@leucos
Copy link

leucos commented Mar 14, 2013

Is there any reason not to use env[:vm].config.vm.host_name and set config.vm.host_name = "..." in the Vagrantfile instead of adding a new option in the provisionning block ?

@mavimo : As far as tried, and regarding testing "real life" playbooks, it doesn't help for group_vars & host_vars since the Temp inventory is in /tmp. Line 61 could be changed to

file = Tempfile.new('inventory', tmpdir = '.')

for this to work, assuming Vagrantfile lies in the same directory as the usual inventory (or better, configuring that in the provisionning block).

Otherwise, it's pretty cool !

@untoldone
Copy link
Author

Leucos: Honestly, no reason beyond me not realizing that was easily doable -- I would agree that using existing Vagrant metadata that on the surface seems to mean the same thing would probably be better than my solution (does it mean the same thing?). I wrote this patch as a quick fix for testing a bind9 playbook that translates the inventory to zone files and this fix satisfied those needs.

Honestly, I think a better approach to creating the temporary inventory file would be to read the existing hosts file and output a new one with just a targeted group overwritten by the config present in vagrant (either to /tmp or in the same directory etc) -- leaving var sections and other groups intact.

@leucos
Copy link

leucos commented Mar 14, 2013

@untoldone

  • "does it mean the same thing?" : yes, I was wondering too. I'm not that sure... I mean there might be implications on the vagrant side. We might want to dig vagrants docs first.
  • "a better approach to creating the temporary inventory file would be to read the existing hosts file and output a new one with just a targeted group overwritten by the config present in vagrant" : sure, it would be much better, and avoid adding a sometimes long list of group for the host in the provisionning block.

It could be nice to hear @dsander 's opinion on these topics.

@mavimo
Copy link

mavimo commented Mar 15, 2013

@leucos some example:

---
- hosts: webserver
  vars_files:
    - [ "vars/${inventory_hostname}/apache2.yml",    "vars/apache2.yml" ]

  handlers:
  - include: handlers/apache2.yml

  tasks:
  - include: handlers/apache2.yml

this require to have a inventory_hostname set to a valid value (eg foo.bar.com) and having a inventory_hostname that maatch the production domain make possible to reproduce it with right settings.
My actually forkflow is to create a symlink: foo.bar.local -> foo.bar.com and set in /etc/hosts the IP for foo.bar.local, but is just a trick.
Using Vagrant wwith possibility to set inventory_hostname i can by-pass this and use the same hostname for production and test environment.

@leucos
Copy link

leucos commented Mar 16, 2013

@mavimo agreed. However, since temporary inventory is create in /tmp, we can't actually use group_vars/foogroup and host_vars/barhost. I suppose the best way to solve this is to use the actual inventory, and override ansible_ssh_host/ansible_ssh_port using --extra-vars for instance

Here's my attempt, just in case you want to try :
https://github.com/leucos/vagrant-ansible/tree/executing-reallife-playbooks

I can run real life playbooks without modifications.
You can clone, git checkout executing-reallife-playbooks, rake gem and vagrant gem install pkg/vagrant-ansible-0.0.5.gem to test it. I will update the README asap.

Note : of course network related things (e.g. firewall rules) and user authorization stuff (e.g. configuring alloed ssh users) are still tricky since we're running on a different IP and using a different user. But with this in mind, playbooks can be written to cope with this upfront.

@command-tab
Copy link

I ran into the same issue using vagrant-ansible almost right away; By using the temporary inventory file, none of my group_vars and host_vars got used, making my playbooks (which use them heavily) rather useless. I ended up having to pass in my existing inventory file to inventory_file and setting hosts. The resulting block in my Vagrantfile ended up like so:

config.vm.provision :ansible do |ansible|
  ansible.playbook = "config/provisioning/site.yml"
  ansible.inventory_file = "config/provisioning/ansible_hosts"
  ansible.hosts = "vagrant"
  ansible.sudo = true
end

The --limit=#{@hostname} in your fork is a good idea — I wasn't aware of that option in Ansible!

@dsander dsander closed this May 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants