-
### ENC
-
---
-
classes:
-
apache::final:
-
-
environment: production
-
parameters:
-
-
# hiera 配置
-
---
:backends:
- yaml
:hierarchy:
- "nodes/%{::hostname}"
- common
:yaml:
# - /etc/puppetlabs/code/environments/%{environment}/hieradata on *nix
# - %CommonAppData%\PuppetLabs\code\environments\%{environment}\hieradata on Windows
# When specifying a datadir, make sure the directory exists.
:datadir: "/etc/puppetlabs/code/environments/%{::environment}/hieradata"
-
-
####### apache module #########
-
-
-
::::::::::::::
-
final.pp
-
::::::::::::::
-
class apache::final{
-
-
$vhost = hiera('apache::vhost', {})
-
create_resources('apache::vhost', $vhost)
-
-
}
-
::::::::::::::
-
init.pp
-
::::::::::::::
-
class apache {
-
-
include apache::install
-
include apache::service
-
-
}
-
::::::::::::::
-
install.pp
-
::::::::::::::
-
class apache::install {
-
-
include apache::params
-
-
package { 'apache':
-
-
ensure => present,
-
name => $apache::params::apache_name,
-
-
}
-
}
-
::::::::::::::
-
params.pp
-
::::::::::::::
-
class apache::params {
-
-
# install http/apace2 on redhat series and debian respectively.
-
-
if $::osfamily == 'RedHat' {
-
$apache_name = 'httpd'
-
$conf_dir = '/etc/httpd/conf.d'
-
$template = 'vhost-rh.conf.erb'
-
-
}elsif $::osfamily == 'Debian' {
-
$apache_name = 'apache2'
-
$conf_dir = '/etc/apache2/conf.d'
-
$template = 'vhost-deb.conf.erb'
-
-
}else {
-
fail( "This is not a supported distro.")
-
}
-
-
}
-
::::::::::::::
-
service.pp
-
::::::::::::::
-
class apache::service {
-
include apache::params
-
-
service {$apache::params::apache_name :
-
-
ensure => running,
-
require => Class['apache::install'],
-
}
-
}
-
::::::::::::::
-
vhost.pp
-
::::::::::::::
-
define apache::vhost(
-
$docroot,
-
$httpd_port,
-
$ssl=true,
-
$serveralias,
-
){
-
-
include apache
-
-
$conf_dir = $apache::params::conf_dir
-
$template = $apache::params::template
-
-
file { "${conf_dir}/${name}.conf":
-
-
content => template("apache/$template"),
-
mode => "0644",
-
owner => root,
-
group => root,
-
require => Class['apache::install'],
-
notify => Class['apache::service'],
-
-
}
-
-
}
-
-
-
Hiera 的数据
-
-
---
-
apache::vhost:
-
www.example.com:
-
ssl: true
-
httpd_port: 8080
-
docroot: /var/www/html/www.example.com
-
ssl : true
-
serveralias:
-
- test.example.com
-
- hfu.example.com
-
-
test.example.com:
-
ssl: false
-
httpd_port: 808
-
docroot: /var/www/html/abc.example.com
-
ssl : true
-
serveralias: test.example.com
-
-
-
-
###templates/vhost-rh.conf.erb #下边的erb是随便写的,未必准确,同时省略debian的conf
-
-
-
### NameVirtualHost *:<%= @httpd_port %>>
-
ServerName <%= @name %>
-
<%if @serveraliases.is_a? Array -%>
-
<% @serveraliases.each do |name| -%>
-
<%= " ServerAlias #{@name}\n" %>
-
<%- end -%>
-
<%- elsif @serveraliases != '' -%>
-
<%= "ServerAlias #{@serveraliases}" -%>
-
<%- end -%>
-
DocumentRoot <%= @docroot %>
-
Options Indexes FollowSymLinks MultiViews
-
AllowOverride None
-
Order allow,deny
-
allow from all
-
-
<%# This is a comment -%>
-
-
<= if @ssl == true -%>
-
ssl on ### For test
-
-
<%% print a <% %> section %%>
-
-
ErrorLog /var/log/httpd/<%= @name %>_error.log
-
LogLevel warn
-
CustomLog /var/log/httpd/<%= @name %>_access.log combined
-
ServerSignature On
-
...
题外话,create_resources的接受必须是hash. 现在用hiera 查一下
-
hiera -d apache::vhost ::environment=production ::hostname=`hostname -s`
-
-
DEBUG: 2016-08-27 00:08:14 -0400: Hiera YAML backend starting
-
DEBUG: 2016-08-27 00:08:14 -0400: Looking up apache::vhost in YAML backend
-
DEBUG: 2016-08-27 00:08:14 -0400: Looking for data source nodes/ip-10-0-10-78
-
DEBUG: 2016-08-27 00:08:14 -0400: Found apache::vhost in nodes/ip-10-0-10-78
-
{""=>
-
{"ssl"=>true,
-
"httpd_port"=>8080,
-
"docroot"=>"/var/www/html/",
-
"serveralias"=>["test.example.com", "hfu.example.com"]},
-
"test.example.com"=>
-
{"ssl"=>true,
-
"httpd_port"=>808,
-
"docroot"=>"/var/www/html/abc.example.com",
-
"serveralias"=>"test.example.com"}}
为什么要加2个冒号? 因为命令行下必须传递标准的puppet能识别的变量名字,没有它,environment/hostname均为空。
题外话,为什么install.pp里面 name => $apache::params::apache_name, 这又涉及到变量的scope的问题了,params和install分别class, params的变量作用于是Local的,外加要用其值必须用 ${class}::${var}这种方式了。
阅读(993) | 评论(0) | 转发(0) |