Skip to content

Commit

Permalink
Add an extra_hosts option for additional entries such as VIP's.
Browse files Browse the repository at this point in the history
This is necessary if you want to add a VIP to each host, or if you want
to define a host that isn't part of your vagrant infrastructure, but
which needs to be listed in your "DNS".
  • Loading branch information
purpleidea committed Apr 17, 2015
1 parent f087051 commit 9d9d1a3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ that all the added host entries have the:
format. This is needed so that `hostname --fqdn` and `facter -p | grep fqdn`
both work.

If you set the `extra_hosts` configuration attribute, each hash in that list
will be used to add additional elements to each /etc/hosts file. This is useful
for defining a virtual IP, which doesn't correspond to a particular physical
host, but which requires a DNS entry. Each element in the `extra_hosts` list
should be a hash with three keys: `:ip`, `:host`, and `:aliases`. The first two
should be strings, while the third should be a list of strings.

Example configuration:

```ruby
Expand All @@ -77,6 +84,13 @@ Vagrant.configure("2") do |config|
config.hostmanager.include_offline = true
config.hostmanager.fqdn_friendly = true
config.hostmanager.domain_name = 'example.com'
config.hostmanager.extra_hosts = [
{
:host => 'vip.example.com',
:ip => '192.168.42.253',
:aliases => ['vip'],
},
]
config.vm.define 'example-box' do |node|
node.vm.hostname = 'example-box-hostname'
node.vm.network :private_network, ip: '192.168.42.42'
Expand Down
3 changes: 3 additions & 0 deletions lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Config < Vagrant.plugin('2', :config)
attr_accessor :ip_resolver
attr_accessor :fqdn_friendly
attr_accessor :domain_name
attr_accessor :extra_hosts

alias_method :enabled?, :enabled
alias_method :include_offline?, :include_offline
Expand All @@ -23,6 +24,7 @@ def initialize
@ip_resolver = UNSET_VALUE
@fqdn_friendly = UNSET_VALUE
@domain_name = UNSET_VALUE
@extra_hosts = UNSET_VALUE
end

def finalize!
Expand All @@ -34,6 +36,7 @@ def finalize!
@ip_resolver = nil if @ip_resolver == UNSET_VALUE
@fqdn_friendly = false if @fqdn_friendly == UNSET_VALUE
@domain_name = '' if @domain_name == UNSET_VALUE
@extra_hosts = [] if @extra_hosts == UNSET_VALUE

@aliases = [ @aliases ].flatten
end
Expand Down
10 changes: 8 additions & 2 deletions lib/vagrant-hostmanager/hosts_file/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,16 @@ def get_machines
end

def get_new_content(header, footer, body, old_content)

extras = ''
@config.hostmanager.extra_hosts.each do |x|
extras+= ("#{x[:ip]}\t#{x[:host]}\t" + x[:aliases].join("\t") + "\n")
end

if body.empty?
block = "\n"
block = "\n" + extras
else
block = "\n\n" + header + body + footer + "\n"
block = "\n\n" + header + body + extras + footer + "\n"
end
# Pattern for finding existing block
header_pattern = Regexp.quote(header)
Expand Down

0 comments on commit 9d9d1a3

Please sign in to comment.