2013-03-27 19:29:21 +00:00
|
|
|
Vagrant Host Manager
|
|
|
|
====================
|
2015-07-27 13:09:14 +00:00
|
|
|
|
|
|
|
[![Gem](https://img.shields.io/gem/dt/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager)
|
2015-07-27 14:10:36 +00:00
|
|
|
[![Gem](https://img.shields.io/gem/dtv/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager)
|
2015-07-27 13:41:50 +00:00
|
|
|
[![Twitter](https://img.shields.io/twitter/url/https/github.com/smdahlen/vagrant-hostmanager.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20this%20awesome%20Vagrant%20plugin%21&url=https%3A%2F%2Fgithub.com%2Fsmdahlen%2Fvagrant-hostmanager&hashtags=vagrant%hostmanager&original_referer=)
|
2015-07-27 13:09:14 +00:00
|
|
|
|
2013-03-27 19:29:21 +00:00
|
|
|
`vagrant-hostmanager` is a Vagrant 1.1+ plugin that manages the `/etc/hosts`
|
2013-06-20 16:54:20 +00:00
|
|
|
file on guest machines (and optionally the host). Its goal is to enable
|
|
|
|
resolution of multi-machine environments deployed with a cloud provider
|
|
|
|
where IP addresses are not known in advance.
|
2013-03-27 19:29:21 +00:00
|
|
|
|
2013-08-22 21:12:33 +00:00
|
|
|
*NOTE:* Version 1.1 of the plugin prematurely introduced a feature to hook into
|
2014-05-30 16:52:45 +00:00
|
|
|
commands other than `vagrant up` and `vagrant destroy`. Version 1.1 broke support
|
|
|
|
for some providers. Version 1.2 reverts this feature until a suitable implementation
|
|
|
|
supporting all providers is available.
|
|
|
|
|
|
|
|
***Potentially breaking change in v1.5.0:*** the running order on `vagrant up` has changed
|
|
|
|
so that hostmanager runs before provisioning takes place. This ensures all hostnames are
|
|
|
|
available to the guest when it is being provisioned
|
|
|
|
(see [#73](https://github.com/smdahlen/vagrant-hostmanager/issues/73)).
|
|
|
|
Previously, hostmanager would run as the very last action. If you depend on the old behavior,
|
|
|
|
see the [provisioner](#provisioner) section.
|
2013-08-22 21:12:33 +00:00
|
|
|
|
2013-03-27 19:29:21 +00:00
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
Install the plugin following the typical Vagrant 1.1 procedure:
|
|
|
|
|
2013-04-06 12:59:07 +00:00
|
|
|
$ vagrant plugin install vagrant-hostmanager
|
2013-03-27 19:29:21 +00:00
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
2013-04-27 12:10:45 +00:00
|
|
|
To update the `/etc/hosts` file on each active machine, run the following
|
|
|
|
command:
|
2013-04-06 12:59:07 +00:00
|
|
|
|
2013-04-08 17:20:22 +00:00
|
|
|
$ vagrant hostmanager
|
2013-03-27 19:29:21 +00:00
|
|
|
|
2013-08-22 21:12:33 +00:00
|
|
|
The plugin hooks into the `vagrant up` and `vagrant destroy` commands
|
|
|
|
automatically.
|
2013-08-12 16:16:40 +00:00
|
|
|
When a machine enters or exits the running state , all active
|
2013-04-27 12:10:45 +00:00
|
|
|
machines with the same provider will have their `/etc/hosts` file updated
|
|
|
|
accordingly. Set the `hostmanager.enabled` attribute to `true` in the
|
|
|
|
Vagrantfile to activate this behavior.
|
|
|
|
|
2013-06-20 16:54:20 +00:00
|
|
|
To update the host's `/etc/hosts` file, set the `hostmanager.manage_host`
|
|
|
|
attribute to `true`.
|
|
|
|
|
2013-03-27 19:29:21 +00:00
|
|
|
A machine's IP address is defined by either the static IP for a private
|
2013-04-08 17:20:22 +00:00
|
|
|
network configuration or by the SSH host configuration. To disable
|
2014-08-21 14:37:19 +00:00
|
|
|
using the private network IP address, set `config.hostmanager.ignore_private_ip`
|
2013-04-08 17:20:22 +00:00
|
|
|
to true.
|
2013-03-27 19:29:21 +00:00
|
|
|
|
|
|
|
A machine's host name is defined by `config.vm.hostname`. If this is not
|
|
|
|
set, it falls back to the symbol defining the machine in the Vagrantfile.
|
|
|
|
|
2013-06-20 16:54:20 +00:00
|
|
|
If the `hostmanager.include_offline` attribute is set to `true`, boxes that are
|
|
|
|
up or have a private ip configured will be added to the hosts file.
|
2013-05-06 06:31:31 +00:00
|
|
|
|
2013-04-17 13:57:53 +00:00
|
|
|
In addition, the `hostmanager.aliases` configuration attribute can be used
|
|
|
|
to provide aliases for your host names.
|
2013-04-17 10:24:45 +00:00
|
|
|
|
|
|
|
Example configuration:
|
|
|
|
|
2013-04-17 13:57:53 +00:00
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
2013-04-27 12:10:45 +00:00
|
|
|
config.hostmanager.enabled = true
|
2013-06-20 16:54:20 +00:00
|
|
|
config.hostmanager.manage_host = true
|
2013-04-17 13:57:53 +00:00
|
|
|
config.hostmanager.ignore_private_ip = false
|
2013-05-06 06:31:31 +00:00
|
|
|
config.hostmanager.include_offline = true
|
2013-06-20 16:54:20 +00:00
|
|
|
config.vm.define 'example-box' do |node|
|
|
|
|
node.vm.hostname = 'example-box-hostname'
|
|
|
|
node.vm.network :private_network, ip: '192.168.42.42'
|
2013-04-17 13:57:53 +00:00
|
|
|
node.hostmanager.aliases = %w(example-box.localdomain example-box-alias)
|
2013-04-17 10:24:45 +00:00
|
|
|
end
|
2013-04-17 13:57:53 +00:00
|
|
|
end
|
|
|
|
```
|
2013-04-17 10:24:45 +00:00
|
|
|
|
2014-05-30 16:52:45 +00:00
|
|
|
### Provisioner
|
|
|
|
|
2014-08-27 01:45:28 +00:00
|
|
|
Starting at version 1.5.0, `vagrant up` runs hostmanager before any provisioning occurs.
|
|
|
|
If you would like hostmanager to run after or during your provisioning stage,
|
2014-05-30 16:52:45 +00:00
|
|
|
you can use hostmanager as a provisioner. This allows you to use the provisioning
|
|
|
|
order to ensure that hostmanager runs when desired. The provisioner will collect
|
|
|
|
hosts from boxes with the same provider as the running box.
|
2013-05-02 06:36:40 +00:00
|
|
|
|
2014-05-30 16:52:45 +00:00
|
|
|
Example:
|
2013-05-02 06:36:40 +00:00
|
|
|
|
|
|
|
```ruby
|
2014-05-30 16:52:45 +00:00
|
|
|
# Disable the default hostmanager behavior
|
|
|
|
config.hostmanager.enabled = false
|
|
|
|
|
|
|
|
# ... possible provisioner config before hostmanager ...
|
|
|
|
|
|
|
|
# hostmanager provisioner
|
2013-05-02 06:36:40 +00:00
|
|
|
config.vm.provision :hostmanager
|
2014-05-30 16:52:45 +00:00
|
|
|
|
|
|
|
# ... possible provisioning config after hostmanager ...
|
2013-05-02 06:36:40 +00:00
|
|
|
```
|
|
|
|
|
2013-06-08 11:46:15 +00:00
|
|
|
Custom IP resolver
|
|
|
|
------------------
|
|
|
|
|
|
|
|
You can customize way, how host manager resolves IP address
|
|
|
|
for each machine. This might be handy in case of aws provider,
|
|
|
|
where host name is stored in ssh_info hash of each machine.
|
|
|
|
This causes generation of invalid /etc/hosts file.
|
|
|
|
|
|
|
|
Custom IP resolver gives you oportunity to calculate IP address
|
2014-01-11 04:51:50 +00:00
|
|
|
for each machine by yourself, giving You also access to the machine that is
|
|
|
|
updating /etc/hosts. For example:
|
2013-06-08 11:46:15 +00:00
|
|
|
|
|
|
|
```ruby
|
2014-01-11 04:51:50 +00:00
|
|
|
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
|
2013-06-08 11:46:15 +00:00
|
|
|
if hostname = (vm.ssh_info && vm.ssh_info[:host])
|
|
|
|
`host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2014-05-24 22:18:25 +00:00
|
|
|
Passwordless sudo
|
|
|
|
-----------------
|
|
|
|
|
2015-10-13 08:53:00 +00:00
|
|
|
To avoid being asked for the password every time the hosts file is updated,
|
|
|
|
enable passwordless sudo for the specific command that hostmanager uses to
|
|
|
|
update the hosts file.
|
2014-05-24 22:18:25 +00:00
|
|
|
|
2015-10-13 08:53:00 +00:00
|
|
|
- Add the following snippet to the sudoers file (e.g.
|
|
|
|
`/etc/sudoers.d/vagrant_hostmanager`):
|
|
|
|
|
|
|
|
```
|
|
|
|
Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp <home-directory>/.vagrant.d/tmp/hosts.local /etc/hosts
|
|
|
|
%<admin-group> ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE
|
|
|
|
```
|
|
|
|
|
|
|
|
Replace `<home-directory>` with your actual home directory (e.g.
|
|
|
|
`/home/joe`) and `<admin-group>` with the group that is used by the system
|
|
|
|
for sudo access (usually `sudo` on Debian/Ubuntu systems and `wheel`
|
|
|
|
on Fedora/Red Hat systems).
|
|
|
|
|
|
|
|
- If necessary, add yourself to the `<admin-group>`:
|
|
|
|
|
|
|
|
```
|
|
|
|
usermod -aG <admin-group> <user-name>
|
|
|
|
```
|
|
|
|
|
|
|
|
Replace `<admin-group>` with the group that is used by the system for sudo
|
|
|
|
access (see above) and `<user-name>` with you user name.
|
2014-05-24 22:18:25 +00:00
|
|
|
|
2013-11-21 04:04:37 +00:00
|
|
|
Windows support
|
|
|
|
---------------
|
|
|
|
|
2014-05-24 22:18:25 +00:00
|
|
|
Hostmanager will detect Windows guests and hosts and use the appropriate
|
2013-11-21 04:04:37 +00:00
|
|
|
path for the ```hosts``` file: ```%WINDIR%\System32\drivers\etc\hosts```
|
|
|
|
|
|
|
|
By default on a Windows host, the ```hosts``` file is not writable without
|
|
|
|
elevated privileges. If hostmanager detects that it cannot overwrite the file,
|
2014-05-24 22:18:25 +00:00
|
|
|
it will attempt to do so with elevated privileges, causing the
|
2013-11-21 04:04:37 +00:00
|
|
|
[UAC](http://en.wikipedia.org/wiki/User_Account_Control) prompt to appear.
|
|
|
|
|
2015-06-06 20:58:49 +00:00
|
|
|
To avoid the UAC prompt, open ```%WINDIR%\System32\drivers\etc\``` in
|
|
|
|
Explorer, right-click the hosts file, go to Properties > Security > Edit
|
|
|
|
and give your user Modify permission.
|
|
|
|
|
2013-11-21 04:04:37 +00:00
|
|
|
### UAC limitations
|
|
|
|
|
|
|
|
Due to limitations caused by UAC, cancelling out of the UAC prompt will not cause any
|
|
|
|
visible errors, however the ```hosts``` file will not be updated.
|
|
|
|
|
2015-12-30 14:47:37 +00:00
|
|
|
|
2013-03-27 19:29:21 +00:00
|
|
|
Contribute
|
|
|
|
----------
|
2015-12-30 14:47:37 +00:00
|
|
|
To contribute, fork then clone the repository, and then the following:
|
|
|
|
|
|
|
|
**Developing**
|
|
|
|
|
|
|
|
1. Install [Bundler](http://bundler.io/)
|
|
|
|
2. Currently the Bundler version is locked to 1.6.9, please install this version.
|
|
|
|
* `sudo gem install bundler -v '1.6.9'`
|
|
|
|
3. Then install vagrant-hostmanager dependancies:
|
|
|
|
* `bundle _1.6.9_ install`
|
|
|
|
|
2016-01-15 17:53:49 +00:00
|
|
|
**Testing**
|
|
|
|
|
|
|
|
1. Build and package your newly developed code:
|
|
|
|
* `rake gem:build`
|
|
|
|
2. Then install the packaged plugin:
|
|
|
|
* `vagrant plugin install pkg/vagrant-hostmanager-*.gem`
|
|
|
|
3. Once you're done testing, roll-back to the latest released version:
|
|
|
|
* `vagrant plugin uninstall vagrant-hostmanager`
|
|
|
|
* `vagrant plugin install vagrant-hostmanager`
|
|
|
|
4. Once you're satisfied developing and testing your new code, please submit a pull request for review.
|
|
|
|
|
2015-12-30 14:47:37 +00:00
|
|
|
**Releasing**
|
|
|
|
|
|
|
|
To release a new version of vagrant-hostmanager you will need to do the following:
|
|
|
|
|
|
|
|
*(only contributors of the GitHub repo and owners of the project at RubyGems will have rights to do this)*
|
2013-03-27 19:29:21 +00:00
|
|
|
|
2015-12-30 14:47:37 +00:00
|
|
|
1. First, bump the version in ~/lib/vagrant-hostmanager/version.rb:
|
|
|
|
* Follow [Semantic Versioning](http://semver.org/).
|
|
|
|
2. Then, create a matching GitHub Release (this will also create a tag):
|
|
|
|
* Preface the version number with a `v`.
|
|
|
|
* https://github.com/smdahlen/vagrant-hostmanager/releases
|
|
|
|
3. You will then need to build and push the new gem to RubyGems:
|
|
|
|
* `rake gem:build`
|
|
|
|
* `gem push pkg/vagrant-hostmanager-1.6.1.gem`
|
|
|
|
4. Then, when John Doe runs the following, they will receive the updated vagrant-hostmanager plugin:
|
|
|
|
* `vagrant plugin update`
|
|
|
|
* `vagrant plugin update vagrant-hostmanager`
|