Module: apache2
Purpose: This module shows how an ERB template can be used to create VirtualHost definitions
File: apache2/manifests/init.pp
Notes: This is the base class. It can just be included within a node definition to install a stock httpd from yum. Also sets up a site-specific define - see comments in file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# apache2/init.pp - base apache class # # Supports only CentOS at this stage. Installs latest httpd from yum and # copies standard httpd.conf into place. # # Removes welcome.conf # # Sets up site-specific define so you can do things like this: # # node "somenode" { # include apache2 # apache2::site-specific "node-special-config.conf" # apache2::site-specific "ssl.conf" # } # and so on. class apache2 { case $::operatingsystem { 'CentOS' : { $confdir = "/etc/httpd/conf.d" $logdir = "/var/log/httpd" package { "httpd": ensure => "latest", before => File["/etc/httpd/conf/httpd.conf"] } service { "httpd": ensure => running, enable => true, require => Package["httpd"] } file { "/etc/httpd/conf/httpd.conf": source => "puppet:///modules/apache2/default-httpd.conf", owner => "root", group => "root", mode => "0644", before => Service["httpd"], notify => Service["httpd"] } # get rid of baboon.conf file { "${confdir}/welcome.conf": ensure => absent, require => Package["httpd"], before => Service["httpd"] } exec { "/sbin/restorecon /etc/httpd/conf/httpd.conf": subscribe => File["/etc/httpd/conf/httpd.conf"], refreshonly => true } } default : { fail('Operating system unsupported by Apache class') } } define site-specific() { file { "${apache2::confdir}/${name}": source => "puppet:///modules/apache2/${name}", owner => "root", group => "root", mode => "0644", notify => Service["httpd"] } } } |
File: apache2/manifests/vhost.pp
Notes: Contains the virtual host definition. Supports only port 80 (else we’d need to ensure SELinux had the correct configuration for http_port_t, etc.), and fairly basic VirtualHost configuration. Anything more “fancy” can be implemented using the site-specific define set up in init.pp, or by using a custom $template.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# apache2/vhost.pp # # Define to set up a basic templated Apache 2 vhost. # # $document_root - Value for DocumentRoot # $template - Path to template file (defaults to standard port 80 # based template # $priority - Priority to use in filename prefix # $servername - Value for ServerName # $serveraliases - Value(s) for ServerAlias # $options - Value for Options # $port - Only supports port 80 at present (would need to take into # account SELinux, multiple defines of Listen would break things, # etc. # $vhost_name - Used in VirtualHost tag # # Example: # # apache2::vhost { "example2.com": # document_root => "/var/www/example2.com/htdocs", # serveraliases => [ "www.example2.com", "example2.com" ] # } define apache2::vhost ( $document_root = "", $template = "apache2/vhost-default.conf.erb", $priority = "10", $servername = "${name}", $serveraliases = "", $options = "Indexes FollowSymLinks MultiViews", $port = "80", $vhost_name = "${name}" ) { # doesn't matter if this gets included multiple times # for each vhost invocation - it's a singleton include apache2 # get the logdir from our parent class $logdir = $apache2::logdir # at the moment, this only supports port 80 else SELinux will # explode if $port != "80" { fail("You have specified a port other than 80 - don't do that") } # we already check for operating system in the core # apache2 class and deal with unsupported OSes there file { "${apache2::confdir}/${priority}-${name}.conf": content => template("$template"), owner => "root", group => "root", mode => "0644", require => Package["httpd"], notify => Service["httpd"] } } |
File: apache2/templates/vhost-default.conf.erb
Notes: ERB template for the VirtualHost configuration
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
NameVirtualHost <%= @vhost_name %>:<%= @port %> <VirtualHost <%= @vhost_name %>:<%= @port %>> ServerName <%= @servername %> <% if @serveraliases != "" -%> <% if @serveraliases.is_a? Array -%> ServerAlias <%= @serveraliases.flatten.join(" ") %> <% else -%> ServerAlias <%= @serveraliases %> <% end -%> <% end -%> DocumentRoot <%= @document_root %> <Directory <%= @document_root %>> Options <%= @options %> AllowOverride None Order allow,deny allow from all </Directory> ErrorLog <%= @logdir %>/<%= @servername %>_error.log CustomLog <%= @logdir %>/<%= @servername %>_access.log combined LogLevel warn ServerSignature Off </VirtualHost> |