-
# ... puppet/modules/splunkforwarder/
-
$ tree
-
.
-
|-- README.md
-
|-- files
-
| `-- props.conf
-
|-- manifests
-
| |-- init.pp
-
| |-- input.pp
-
| `-- params.pp
-
`-- templates
-
|-- inputs
-
| |-- ascrub.erb
-
| |-- os.erb
-
| |-- production.erb
-
| |-- sandbox.erb
先看实例,Module结构如上:
-
$ more params.pp
-
class splunkforwarder::params {
-
$admin_password = 'ngVQW1xxxxxxx’
-
$basedir = '/opt/splunkforwarder'
-
$inputs = [ 'os,stage_os,splunkforwarder/inputs/os.erb', 'user,stage_tungsten,splunkforwarder/inputs/zapp.erb' ]
-
$cloudspl = undef
-
}
定义了子类params,这里也类似namespace,
-
$ more input.pp
-
-
define splunkforwarder::input (
-
$basedir,
-
) {
-
$value = split($name, ',')
-
$app_name = $value[0]
-
$index_name = $value[1]
-
$template_name = $value[2]
-
$dirs = [
-
"${basedir}/etc/apps/${app_name}",
-
"${basedir}/etc/apps/${app_name}/local",
-
]
-
-
file { $dirs:
-
ensure => directory,
-
mode => '0755',
-
owner => 'splunk',
-
group => 'splunk',
-
}
-
-
file { "${basedir}/etc/apps/${app_name}/local/inputs.conf":
-
ensure => present,
-
mode => '0644',
-
owner => 'splunk',
-
group => 'splunk',
-
require => File["${basedir}/etc/apps/${app_name}/local"],
-
notify => Service['splunk'],
-
content => template($template_name),
-
}
-
-
}
这里调用了puppet的内置函数split, 跟include/require类似,不过它需要参数。在这里可以看到
-
class splunkforwarder (
-
$admin_password = $splunkforwarder::params::admin_password,
-
$basedir = $splunkforwarder::params::basedir,
-
$inputs = $splunkforwarder::params::inputs,
-
$limits_thruput = $splunkforwarder::params::limits_thruput,
-
$splunk_servers = $splunkforwarder::params::splunk_servers,
-
$version = $splunkforwarder::params::version,
-
$cloudspl = $splunkforwarder::params::cloudspl,
-
) inherits splunkforwarder::params {
-
-
$splunk_cmd = "${basedir}/bin/splunk"
-
-
package {'splunkforwarder':
-
ensure => $version,
-
}
-
-
exec { 'splunk_create_service':
-
command => "${basedir}/bin/splunk enable boot-start --accept-license --answer-yes --no-prompt",
-
subscribe => Package['splunkforwarder'],
-
}
-
-
service { 'splunk':
-
ensure => 'running',
-
enable => true,
-
hasstatus => true,
-
pattern => 'splunkd',
-
require => Exec['splunk_create_service'],
-
}
-
-
# Change of admin password
-
exec { 'splunk_change_admin_password':
-
command => "${splunk_cmd} edit user admin -password ${admin_password} -role admin -auth admin:changeme",
-
refreshonly => true,
-
require => Service['splunk'],
-
}
-
-
if $cloudspl {
-
exec { 'retrieve_spl':
-
command => "/usr/bin/wget -q ${cloudspl} -O ${basedir}/splunkclouduf.spl",
-
creates => "${basedir}/splunkclouduf.spl",
-
require => Exec['splunk_change_admin_password'],
-
}
-
-
file { 'splunkclouduf.spl':
-
path => "${basedir}/splunkclouduf.spl",
-
mode => 0640,
-
require => Exec["retrieve_spl"],
-
}
-
~>
-
exec { 'install_spl':
-
command => "${splunk_cmd} install app ${basedir}/splunkclouduf.spl -auth admin:changeme",
-
refreshonly => true,
-
}
-
}
-
-
if $cloudspl == undef {
-
file {
-
'outputs.conf':
-
ensure => present,
-
path => "${basedir}/etc/system/local/outputs.conf",
-
mode => '0644',
-
owner => 'splunk',
-
group => 'splunk',
-
require => Package['splunkforwarder'],
-
notify => Service['splunk'],
-
content => template('splunkforwarder/outputs.conf.erb');
-
}
-
}
-
-
file {
-
'limits.conf':
-
ensure => present,
-
path => "${basedir}/etc/system/local/limits.conf",
-
mode => '0644',
-
owner => 'splunk',
-
group => 'splunk',
-
require => Package['splunkforwarder'],
-
notify => Service['splunk'],
-
content => template('splunkforwarder/limits.conf.erb');
-
}
-
-
file {
-
'props.conf':
-
ensure => present,
-
path => "${basedir}/etc/apps/SplunkUniversalForwarder/default/props.conf",
-
mode => '0644',
-
owner => 'splunk',
-
group => 'splunk',
-
require => Package['splunkforwarder'],
-
notify => Service['splunk'],
-
source => "puppet:///modules/splunkforwarder/props.conf";
-
}
-
-
splunkforwarder::input { $inputs:
-
basedir => $basedir,
-
require => Package['splunkforwarder'],
-
notify => Service['splunk'],
-
}
-
-
}
分析: splunkforwarder入口init.pp调用了input.pp, 并且$name(也叫$title,就是资源的名字)的值是$input, 它来自什么地方?看下
-
$inputs = $splunkforwarder::params::inputs,
-
这个class含有参数,$inputs,而且inherits了params.pp的值,也就是params.pp的参数值可以被override。由于这里没定义,那么直接继承就是列表
-
[ 'os,stage_os,splunkforwarder/inputs/os.erb', 'user,stage_tungsten,splunkforwarder/inputs/zapp.erb' ]
-
那么最后splunk::input这个被包含后,$input的值就是$name了。
-
-
define splunkforwarder::input (
-
$basedir,
-
) {
-
$value = split($name, ',')
-
$app_name = $value[0]
-
$index_name = $value[1]
-
$template_name = $value[2]
-
$dirs = [
-
"${basedir}/etc/apps/${app_name}",
-
"${basedir}/etc/apps/${app_name}/local",
-
]
-
-
file { $dirs:
-
ensure => directory,
-
mode => '0755',
-
owner => 'splunk',
-
group => 'splunk',
-
}
看下这个define, 定义了参数basedir。然后是变量赋值,$value=split($name,",")用了split函数,$name就是刚说的List,值如下,
-
[ '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以及注意事项
阅读(2567) | 评论(0) | 转发(0) |