Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1095584
  • 博文数量: 186
  • 博客积分: 4939
  • 博客等级: 上校
  • 技术积分: 2075
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 17:15
文章分类

全部博文(186)

文章存档

2018年(1)

2017年(3)

2016年(11)

2015年(42)

2014年(21)

2013年(9)

2012年(18)

2011年(46)

2010年(35)

分类: 系统运维

2016-08-27 12:10:59


  1. ### ENC
  2. ---
  3. classes:
  4.   apache::final:

  5. environment: production
  6. parameters:
  7.  
  8. # hiera 配置
  9. ---
    :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"

  10. ####### apache module #########


  11. ::::::::::::::
  12. final.pp
  13. ::::::::::::::
  14. class apache::final{

  15.     $vhost = hiera('apache::vhost', {})
  16.     create_resources('apache::vhost', $vhost)

  17. }
  18. ::::::::::::::
  19. init.pp
  20. ::::::::::::::
  21. class apache {

  22.    include apache::install
  23.    include apache::service

  24. }
  25. ::::::::::::::
  26. install.pp
  27. ::::::::::::::
  28. class apache::install {

  29.  include apache::params

  30.  package { 'apache':
  31.  
  32.         ensure => present,
  33.         name => $apache::params::apache_name,

  34.         }
  35. }
  36. ::::::::::::::
  37. params.pp
  38. ::::::::::::::
  39. class apache::params {

  40. # install http/apace2 on redhat series and debian respectively.

  41.   if $::osfamily == 'RedHat' {
  42.     $apache_name = 'httpd'
  43.     $conf_dir = '/etc/httpd/conf.d'
  44.     $template = 'vhost-rh.conf.erb'

  45.   }elsif $::osfamily == 'Debian' {
  46.     $apache_name = 'apache2'
  47.     $conf_dir = '/etc/apache2/conf.d'
  48.     $template = 'vhost-deb.conf.erb'

  49.   }else {
  50.     fail( "This is not a supported distro.")
  51.   }

  52. }
  53. ::::::::::::::
  54. service.pp
  55. ::::::::::::::
  56. class apache::service {
  57.   include apache::params

  58.   service {$apache::params::apache_name :

  59.      ensure => running,
  60.      require => Class['apache::install'],
  61.  }
  62. }
  63. ::::::::::::::
  64. vhost.pp
  65. ::::::::::::::
  66. define apache::vhost(
  67.   $docroot,
  68.   $httpd_port,
  69.   $ssl=true,
  70.   $serveralias,
  71. ){

  72.  include apache
  73.  
  74.  $conf_dir = $apache::params::conf_dir
  75.  $template = $apache::params::template

  76.  file { "${conf_dir}/${name}.conf":

  77.     content => template("apache/$template"),
  78.     mode => "0644",
  79.     owner => root,
  80.     group => root,
  81.     require => Class['apache::install'],
  82.     notify => Class['apache::service'],

  83.     }

  84.   }


  85. Hiera 的数据

  86. ---
  87. apache::vhost:
  88.   www.example.com:
  89.     ssl: true
  90.     httpd_port: 8080
  91.     docroot: /var/www/html/www.example.com
  92.     ssl : true
  93.     serveralias:
  94.       - test.example.com
  95.       - hfu.example.com

  96.   test.example.com:
  97.     ssl: false
  98.     httpd_port: 808
  99.     docroot: /var/www/html/abc.example.com
  100.     ssl : true
  101.     serveralias: test.example.com



  102. ###templates/vhost-rh.conf.erb   #下边的erb是随便写的,未必准确,同时省略debian的conf


  103. ### NameVirtualHost *:<%= @httpd_port %>>
  104. ServerName <%= @name %>
  105. <%if @serveraliases.is_a? Array -%>
  106. <% @serveraliases.each do |name| -%>
  107. <%= " ServerAlias #{@name}\n" %>
  108. <%- end -%>
  109. <%- elsif @serveraliases != '' -%>
  110. <%= "ServerAlias #{@serveraliases}" -%>
  111. <%- end -%>
  112.     DocumentRoot <%= @docroot %>
  113.     Options Indexes FollowSymLinks MultiViews
  114.     AllowOverride None
  115.     Order allow,deny
  116.     allow from all

  117. <%# This is a comment -%>

  118. <= if @ssl == true -%>
  119.     ssl on ### For test

  120. <%% print a <% %> section %%>
  121.  
  122.   ErrorLog /var/log/httpd/<%= @name %>_error.log
  123.   LogLevel warn
  124.   CustomLog /var/log/httpd/<%= @name %>_access.log combined
  125.   ServerSignature On
  126. ...
题外话,create_resources的接受必须是hash. 现在用hiera 查一下

  1. hiera -d apache::vhost ::environment=production ::hostname=`hostname -s`

  2. DEBUG: 2016-08-27 00:08:14 -0400: Hiera YAML backend starting
  3. DEBUG: 2016-08-27 00:08:14 -0400: Looking up apache::vhost in YAML backend
  4. DEBUG: 2016-08-27 00:08:14 -0400: Looking for data source nodes/ip-10-0-10-78
  5. DEBUG: 2016-08-27 00:08:14 -0400: Found apache::vhost in nodes/ip-10-0-10-78
  6. {""=>
  7.   {"ssl"=>true,
  8.    "httpd_port"=>8080,
  9.    "docroot"=>"/var/www/html/",
  10.    "serveralias"=>["test.example.com", "hfu.example.com"]},
  11.  "test.example.com"=>
  12.   {"ssl"=>true,
  13.    "httpd_port"=>808,
  14.    "docroot"=>"/var/www/html/abc.example.com",
  15.    "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) |
给主人留下些什么吧!~~