Skip to content

Commit c492b2b

Browse files
committed
Allow multiple IP addresses per vhost
To make a vhost reachable over 2 IP addresses we need to configure 2 similar vhosts which differ in the IP address. This change allows to use an array of IPs.
1 parent 22ed027 commit c492b2b

File tree

5 files changed

+104
-7
lines changed

5 files changed

+104
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,16 @@ apache::vhost { 'ip.example.com':
399399
}
400400
~~~
401401

402+
It is also possible to configure more than one IP address per vhost by using an array of IP addresses for the [`ip`][] parameter:
403+
404+
~~~ puppet
405+
apache::vhost { 'ip.example.com':
406+
ip => ['127.0.0.1','169.254.1.1'],
407+
port => '80',
408+
docroot => '/var/www/ip',
409+
}
410+
~~~
411+
402412
To configure a virtual host with [aliased servers][], refer to the aliases using the [`serveraliases`][] parameter:
403413

404414
~~~ puppet

manifests/vhost.pp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@
339339

340340
if $ip {
341341
if $port {
342-
$listen_addr_port = "${ip}:${port}"
343-
$nvh_addr_port = "${ip}:${port}"
342+
$listen_addr_port = suffix(any2array($ip),":${port}")
343+
$nvh_addr_port = suffix(any2array($ip),":${port}")
344344
} else {
345345
$listen_addr_port = undef
346346
$nvh_addr_port = $ip
@@ -364,13 +364,13 @@
364364
if $ip and defined(Apache::Listen["${port}"]) {
365365
fail("Apache::Vhost[${name}]: Mixing IP and non-IP Listen directives is not possible; check the add_listen parameter of the apache::vhost define to disable this")
366366
}
367-
if ! defined(Apache::Listen["${listen_addr_port}"]) and $listen_addr_port and $ensure == 'present' {
368-
::apache::listen { "${listen_addr_port}": }
367+
if $listen_addr_port and $ensure == 'present' {
368+
ensure_resource('apache::listen', $listen_addr_port)
369369
}
370370
}
371371
if ! $ip_based {
372-
if ! defined(Apache::Namevirtualhost[$nvh_addr_port]) and $ensure == 'present' and (versioncmp($apache_version, '2.4') < 0) {
373-
::apache::namevirtualhost { $nvh_addr_port: }
372+
if $ensure == 'present' and (versioncmp($apache_version, '2.4') < 0) {
373+
ensure_resource('apache::namevirtualhost', $nvh_addr_port)
374374
}
375375
}
376376

spec/acceptance/vhost_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,59 @@ class { 'apache': }
190190
end
191191
end
192192

193+
context 'new vhost with multiple IP addresses on port 80' do
194+
it 'should configure one apache vhost with 2 ip addresses' do
195+
pp = <<-EOS
196+
class { 'apache':
197+
default_vhost => false,
198+
}
199+
apache::vhost { 'example.com':
200+
port => '80',
201+
ip => ['127.0.0.1','::1'],
202+
ip_based => true,
203+
docroot => '/var/www/html',
204+
}
205+
host { 'ipv4.example.com': ip => '127.0.0.1', }
206+
host { 'ipv6.example.com': ip => '::1', }
207+
file { '/var/www/html/index.html':
208+
ensure => file,
209+
content => "Hello from vhost\\n",
210+
}
211+
EOS
212+
apply_manifest(pp, :catch_failures => true)
213+
end
214+
215+
describe service($service_name) do
216+
it { is_expected.to be_enabled }
217+
it { is_expected.to be_running }
218+
end
219+
220+
describe file("#{$vhost_dir}/25-example.com.conf") do
221+
it { is_expected.to contain '<VirtualHost 127.0.0.1:80 ::1:80>' }
222+
it { is_expected.to contain "ServerName example.com" }
223+
end
224+
225+
describe file($ports_file) do
226+
it { is_expected.to be_file }
227+
it { is_expected.to contain 'Listen 127.0.0.1:80' }
228+
it { is_expected.to contain 'Listen ::1:80' }
229+
it { is_expected.not_to contain 'NameVirtualHost 127.0.0.1:80' }
230+
it { is_expected.not_to contain 'NameVirtualHost ::1:80' }
231+
end
232+
233+
it 'should answer to ipv4.example.com' do
234+
shell("/usr/bin/curl ipv4.example.com:80", {:acceptable_exit_codes => 0}) do |r|
235+
expect(r.stdout).to eq("Hello from vhost\n")
236+
end
237+
end
238+
239+
it 'should answer to ipv6.example.com' do
240+
shell("/usr/bin/curl ipv6.example.com:80", {:acceptable_exit_codes => 0}) do |r|
241+
expect(r.stdout).to eq("Hello from vhost\n")
242+
end
243+
end
244+
end
245+
193246
context 'apache_directories' do
194247
describe 'readme example, adapted' do
195248
it 'should configure a vhost with Files' do

spec/defines/vhost_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,40 @@
459459
it { is_expected.to contain_concat__fragment('rspec.example.com-limits').with(
460460
:content => /^\s+LimitRequestFieldSize\s54321$/)}
461461
end
462+
context 'vhost with multiple ip addresses' do
463+
let :params do
464+
{
465+
'port' => '80',
466+
'ip' => ['127.0.0.1','::1'],
467+
'ip_based' => true,
468+
'servername' => 'example.com',
469+
'docroot' => '/var/www/html',
470+
'add_listen' => true,
471+
'ensure' => 'present'
472+
}
473+
end
474+
let :facts do
475+
{
476+
:osfamily => 'RedHat',
477+
:operatingsystemrelease => '7',
478+
:concat_basedir => '/dne',
479+
:operatingsystem => 'RedHat',
480+
:id => 'root',
481+
:kernel => 'Linux',
482+
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
483+
:kernelversion => '3.6.2',
484+
:is_pe => false,
485+
}
486+
end
487+
488+
it { is_expected.to compile }
489+
it { is_expected.to contain_concat__fragment('rspec.example.com-apache-header').with(
490+
:content => /[.\/m]*<VirtualHost 127.0.0.1:80 ::1:80>[.\/m]*$/ ) }
491+
it { is_expected.to contain_concat__fragment('Listen 127.0.0.1:80') }
492+
it { is_expected.to contain_concat__fragment('Listen ::1:80') }
493+
it { is_expected.to_not contain_concat__fragment('NameVirtualHost 127.0.0.1:80') }
494+
it { is_expected.to_not contain_concat__fragment('NameVirtualHost ::1:80') }
495+
end
462496
context 'set only aliases' do
463497
let :params do
464498
{

templates/vhost/_file_header.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Managed by Puppet
44
# ************************************
55

6-
<VirtualHost <%= @nvh_addr_port %>>
6+
<VirtualHost <%= [@nvh_addr_port].flatten.compact.join(' ') %>>
77
ServerName <%= @servername %>
88
<% if @serveradmin -%>
99
ServerAdmin <%= @serveradmin %>

0 commit comments

Comments
 (0)