Skip to content

Commit d0cbe7b

Browse files
committed
Added support for multiple private_network adapters
1 parent 4845bcc commit d0cbe7b

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ You currently only need the `hostname` and a `:private_network` network with a f
3838

3939
This IP address and the hostname will be used for the entry in the `/etc/hosts` file.
4040

41+
### Multiple private network adapters
42+
43+
If you have multiple network adapters i.e.:
44+
45+
config.vm.network :private_network, ip: "10.0.0.1"
46+
config.vm.network :private_network, ip: "10.0.0.2"
47+
48+
you can specify which hostnames are bound to which IP by passing a hash mapping the IP of the network to an array of hostnames to create, e.g.:
49+
50+
config.hostsupdater.aliases = {
51+
'10.0.0.1' => ['foo.com', 'bar.com'],
52+
'10.0.0.2' => ['baz.com', 'bat.com']
53+
}
54+
55+
This will produce `/etc/hosts` entries like so:
56+
57+
10.0.0.1 foo.com
58+
10.0.0.1 bar.com
59+
10.0.0.2 baz.com
60+
10.0.0.2 bat.com
61+
4162
### Skipping hostupdater
4263

4364
To skip adding some entries to the /etc/hosts file add `hostsupdater: "skip"` option to network configuration:

lib/vagrant-hostsupdater/HostsUpdater.rb

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,45 @@ def getIps
1616
return ips
1717
end
1818

19-
def getHostnames
20-
hostnames = Array(@machine.config.vm.hostname)
21-
if @machine.config.hostsupdater.aliases
22-
hostnames.concat(@machine.config.hostsupdater.aliases)
19+
# Get a hash of hostnames indexed by ip, e.g. { 'ip1': ['host1'], 'ip2': ['host2', 'host3'] }
20+
def getHostnames(ips)
21+
hostnames = Hash.new { |h, k| h[k] = [] }
22+
23+
case @machine.config.hostsupdater.aliases
24+
when Array
25+
# simple list of aliases to link to all ips
26+
ips.each do |ip|
27+
hostnames[ip] += @machine.config.hostsupdater.aliases
28+
end
29+
when Hash
30+
# complex definition of aliases for various ips
31+
@machine.config.hostsupdater.aliases.each do |ip, hosts|
32+
hostnames[ip] += Array(hosts)
33+
end
2334
end
35+
36+
# handle default hostname(s) if not already specified in the aliases
37+
Array(@machine.config.vm.hostname).each do |host|
38+
if hostnames.none? { |k, v| v.include?(host) }
39+
ips.each do |ip|
40+
hostnames[ip].unshift host
41+
end
42+
end
43+
end
44+
2445
return hostnames
2546
end
2647

27-
def addHostEntries()
48+
def addHostEntries
2849
ips = getIps
29-
hostnames = getHostnames
50+
hostnames = getHostnames(ips)
3051
file = File.open(@@hosts_path, "rb")
3152
hostsContents = file.read
3253
uuid = @machine.id
3354
name = @machine.name
3455
entries = []
3556
ips.each do |ip|
36-
hostnames.each do |hostname|
57+
hostnames[ip].each do |hostname|
3758
entryPattern = hostEntryPattern(ip, hostname)
3859

3960
if hostsContents.match(/#{entryPattern}/)

0 commit comments

Comments
 (0)