Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1095792
  • 博文数量: 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)

分类: 系统运维

2015-02-12 13:47:25


  1. # ... puppet/modules/splunkforwarder/


  2. $ tree
  3. .
  4. |-- README.md
  5. |-- files
  6. | `-- props.conf
  7. |-- manifests
  8. | |-- init.pp
  9. | |-- input.pp
  10. | `-- params.pp
  11. `-- templates
  12.     |-- inputs
  13.     | |-- ascrub.erb
  14.     | |-- os.erb
  15.     | |-- production.erb
  16.     | |-- sandbox.erb
先看实例,Module结构如上:

  1. $ more params.pp
  2. class splunkforwarder::params {
  3. $admin_password = 'ngVQW1xxxxxxx’
  4. $basedir = '/opt/splunkforwarder'
  5. $inputs = [ 'os,stage_os,splunkforwarder/inputs/os.erb', 'user,stage_tungsten,splunkforwarder/inputs/zapp.erb' ]
  6. $cloudspl = undef
  7. }
定义了子类params,这里也类似namespace,

  1. $ more input.pp
  2. define splunkforwarder::input (
  3. $basedir,
  4. ) {
  5. $value = split($name, ',')
  6. $app_name = $value[0]
  7. $index_name = $value[1]
  8. $template_name = $value[2]
  9. $dirs = [
  10. "${basedir}/etc/apps/${app_name}",
  11. "${basedir}/etc/apps/${app_name}/local",
  12. ]
  13. file { $dirs:
  14. ensure => directory,
  15. mode => '0755',
  16. owner => 'splunk',
  17. group => 'splunk',
  18. }
  19. file { "${basedir}/etc/apps/${app_name}/local/inputs.conf":
  20. ensure => present,
  21. mode => '0644',
  22. owner => 'splunk',
  23. group => 'splunk',
  24. require => File["${basedir}/etc/apps/${app_name}/local"],
  25. notify => Service['splunk'],
  26. content => template($template_name),
  27. }
  28. }
这里调用了puppet的内置函数split, 跟include/require类似,不过它需要参数。在这里可以看到


  1. class splunkforwarder (
  2. $admin_password = $splunkforwarder::params::admin_password,
  3. $basedir = $splunkforwarder::params::basedir,
  4. $inputs = $splunkforwarder::params::inputs,
  5. $limits_thruput = $splunkforwarder::params::limits_thruput,
  6. $splunk_servers = $splunkforwarder::params::splunk_servers,
  7. $version = $splunkforwarder::params::version,
  8. $cloudspl = $splunkforwarder::params::cloudspl,
  9. ) inherits splunkforwarder::params {
  10. $splunk_cmd = "${basedir}/bin/splunk"
  11. package {'splunkforwarder':
  12. ensure => $version,
  13. }
  14. exec { 'splunk_create_service':
  15. command => "${basedir}/bin/splunk enable boot-start --accept-license --answer-yes --no-prompt",
  16. subscribe => Package['splunkforwarder'],
  17. }
  18. service { 'splunk':
  19. ensure => 'running',
  20. enable => true,
  21. hasstatus => true,
  22. pattern => 'splunkd',
  23. require => Exec['splunk_create_service'],
  24. }
  25. # Change of admin password
  26. exec { 'splunk_change_admin_password':
  27. command => "${splunk_cmd} edit user admin -password ${admin_password} -role admin -auth admin:changeme",
  28. refreshonly => true,
  29. require => Service['splunk'],
  30. }
  31. if $cloudspl {
  32. exec { 'retrieve_spl':
  33. command => "/usr/bin/wget -q ${cloudspl} -O ${basedir}/splunkclouduf.spl",
  34. creates => "${basedir}/splunkclouduf.spl",
  35. require => Exec['splunk_change_admin_password'],
  36. }
  37. file { 'splunkclouduf.spl':
  38. path => "${basedir}/splunkclouduf.spl",
  39. mode => 0640,
  40. require => Exec["retrieve_spl"],
  41. }
  42. ~>
  43. exec { 'install_spl':
  44. command => "${splunk_cmd} install app ${basedir}/splunkclouduf.spl -auth admin:changeme",
  45. refreshonly => true,
  46. }
  47. }
  48. if $cloudspl == undef {
  49. file {
  50. 'outputs.conf':
  51. ensure => present,
  52. path => "${basedir}/etc/system/local/outputs.conf",
  53. mode => '0644',
  54. owner => 'splunk',
  55. group => 'splunk',
  56. require => Package['splunkforwarder'],
  57. notify => Service['splunk'],
  58. content => template('splunkforwarder/outputs.conf.erb');
  59. }
  60. }
  61. file {
  62. 'limits.conf':
  63. ensure => present,
  64. path => "${basedir}/etc/system/local/limits.conf",
  65. mode => '0644',
  66. owner => 'splunk',
  67. group => 'splunk',
  68. require => Package['splunkforwarder'],
  69. notify => Service['splunk'],
  70. content => template('splunkforwarder/limits.conf.erb');
  71. }
  72. file {
  73. 'props.conf':
  74. ensure => present,
  75. path => "${basedir}/etc/apps/SplunkUniversalForwarder/default/props.conf",
  76. mode => '0644',
  77. owner => 'splunk',
  78. group => 'splunk',
  79. require => Package['splunkforwarder'],
  80. notify => Service['splunk'],
  81. source => "puppet:///modules/splunkforwarder/props.conf";
  82. }
  83. splunkforwarder::input { $inputs:
  84. basedir => $basedir,
  85. require => Package['splunkforwarder'],
  86. notify => Service['splunk'],
  87. }
  88. }
分析: splunkforwarder入口init.pp调用了input.pp, 并且$name(也叫$title,就是资源的名字)的值是$input, 它来自什么地方?看下
  1. $inputs = $splunkforwarder::params::inputs,
  2. 这个class含有参数,$inputs,而且inherits了params.pp的值,也就是params.pp的参数值可以被override。由于这里没定义,那么直接继承就是列表
  3. [ 'os,stage_os,splunkforwarder/inputs/os.erb', 'user,stage_tungsten,splunkforwarder/inputs/zapp.erb' ]
  4. 那么最后splunk::input这个被包含后,$input的值就是$name了。
    1. define splunkforwarder::input (
    2. $basedir,
    3. ) {
    4. $value = split($name, ',')
    5. $app_name = $value[0]
    6. $index_name = $value[1]
    7. $template_name = $value[2]
    8. $dirs = [
    9. "${basedir}/etc/apps/${app_name}",
    10. "${basedir}/etc/apps/${app_name}/local",
    11. ]
    12. file { $dirs:
    13. ensure => directory,
    14. mode => '0755',
    15. owner => 'splunk',
    16. group => 'splunk',
    17. }
    看下这个define, 定义了参数basedir。然后是变量赋值,$value=split($name,",")用了split函数,$name就是刚说的List,值如下,
    1. [ 'os,stage_os,splunkforwarder/inputs/os.erb', 'user,stage_tungsten,splunkforwarder/inputs/zapp.erb' ]

    关于define,官方Docs很详细:Defined resource types (also called defined types or defines) are blocks of Puppet code that can be evaluated multiple times with different parameters. 
    就是一个code可以传递不同参数这在配置文件非常实用,典型例子就是apache的vhost.



    为什么要使用define以及注意事项

阅读(2579) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~