puppet的配置清单书写

时间:2023-03-09 17:08:26
puppet的配置清单书写

puppet的配置清单书写

1使用数组,合并同类的

例如你想安装很多软件,如果分开来写的话,很麻烦,不简洁,这时我们可以使用数组来完成

 以前我们这样来写
class packages{
package { "sudo" :
ensure =>installed
}
package { "unzip" :
ensure => installed
}
package { "locate" : ensure => installed }
package { "lsof" : ensure => installed }
package { "cron" : ensure => installed }
package { "rubygems" : ensure => installed }
}

使用数组后就简单了

 class  packages{
package { [ "cron",
"locate",
"lsof",
"rubygems"
"screen",
"sudo"
"unzip" ]:
ensure => installed,
}
}

或者这样来写数组

 $packages = [ "ruby1.8-dev",
"ruby1.8",
"ri1.8",
"rdoc1.8",
"irb1.8",
"libreadline-ruby1.8",
"libruby1.8",
"libopenssl-ruby" ] package { $packages: ensure => installed }

2,当你有一组资源拥有一些公用的参数而其中一些资源确有不同的参数时, 就需要使用 define 资源将它们组合在一起。

使用define来定义资源

 会在tep目录下生成三个文件
define tmpfile() {
file { "/tmp/$name":
content => "Hello, world",
}
} tmpfile { ["a", "b", "c"]: }

结果如下:

 [root@agent1 etc]# puppet agent --test --noop
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for agent1.pup.yxnu
Info: Applying configuration version ''
Notice: /Stage[main]/Motd/Motd::Tmpfile[b]/File[/tmp/b]/ensure: current_value absent, should be file (noop)
Notice: Motd::Tmpfile[b]: Would have triggered 'refresh' from events
Notice: /Stage[main]/Motd/Motd::Tmpfile[a]/File[/tmp/a]/ensure: current_value absent, should be file (noop)
Notice: Motd::Tmpfile[a]: Would have triggered 'refresh' from events
Notice: /Stage[main]/Motd/Motd::Tmpfile[c]/File[/tmp/c]/ensure: current_value absent, should be file (noop)
Notice: Motd::Tmpfile[c]: Would have triggered 'refresh' from events
Notice: Class[Motd]: Would have triggered 'refresh' from events
Notice: Stage[main]: Would have triggered 'refresh' from events
Notice: Finished catalog run in 0.83 seconds

多个参数

 define webapp( $domain, $path, $platform ) {
...
} webapp { "mywizzoapp":
domain => "mywizzoapp.com",
path => "/var/www/apps/mywizzoapp",
platform => "Rails",
} #你可以使用逗号间隔的列表同时声明多个参数:

3资源的依赖关系

使用两个元参数来解决,require,notify

元参数require告诉puppet这个被指定的类中的所有资源必须在当前资源之前被处理。
notify 创建了一个通知关系,如果当前资源(服务器的配置发生改变)puppet就会通知服务重启,我们可以定义service类,让服务资源重启

实例如下:ntp服务的安装重启,会按照如下方式来执行,这个例子可以使用到其他服务,例如apache,nginx等

Package["ntp"] -> File["/etc/ntp.conf"] ~> Service["ntp"]

cat  ntp.pp

 class admin::ntp {
package { "ntp":
ensure => installed,
} service { "ntpd":
ensure => running,
require => Package["ntp"],
} file { "/etc/ntp.conf":
source => "puppet:///modules/admin/ntp.conf",
notify => Service["ntpd"],
require => Package["ntp"],
}
}

你也可以指定一个资源依赖于某个类:

require => Class["my-apt-repo"]

你不仅可以指定资源和类之间的依赖关系,甚至可以指定 collections 之间的依赖关系:

Yumrepo <| |> -> Package <| provider == yum |>

这是一种功能强大的表达方式,所有 provider 是 yum 的 package 资源被应用之前, 所有的 yumrepo 资源首先都应该被应用。

4继承inherits

当一个节点继承自另一个节点,它会应用父节点的所有配置。 然后你可以添加任何代码,从而使得这个节点成为有别于其他节点的特殊节点。

你可以配置一个节点继承自另外一个节点,而另外一个节点也可以继承自其它节点等。 但是你不能继承自多个节点(即不能多重继承),因此不能使用如下方式定义节点:

简单实例

1创建一个基类(一个节点),让其包含其他节点都包含的类

 node server {
include admin::basics
include admin::ssh
include admin::ntp
include puppet::client
include backup::client
}

2然后,继承这个server节点

 node wreckspace_server inherits server {
$provider = "WreckSpace"
} node gododgy_server inherits server {
$provider = "GoDodgy"
} node veryslow_server inherits server {
$provider = "VerySlow"
}

类的继承和重载

参考这里,很详细

5给类传递参数

有时对一个类的某些方面进行 参数化parameterize)是很有用的。例如, 你可能需要管理不同版本的 gem 软件包,既可以为每一种版本创建分离的单独的类, 也可以使用继承和覆盖,为一个类传递一个版本号作为参数。

实力如下,只是在传递参数的时候不同

声明一个类

class eventmachine( $version ) {
package { "eventmachine":
provider =>t gem,
ensure => $version,
}
}

然后在site.pp文件里面某个接地单下面包含这个类,这种写法只是同时为参数 $version 指定了一个值

 class { "eventmachine": version => "0.12.8" }

上面这句就相当于以前我们写的include

 include   eventmachine

说明下;

与 define 不同,一个节点上只能存在一个参数化的类实例。 所以当你需要针对一个资源创建多个不同的实例时,应该使用 define 取代类的参数化。

在3.x版本中,在定义变量的时候,一定要加上$,像这种都是不生效的port=3306,要写成$port=3306

6可重用的跨平台配置必含配置

根据操作系统来安装服务

 [root@pup manifests]# cat ssh.pp
class admin::ssh{
$ssh_service = $operatingsystem? {
/Ubuntu|Debian/ => "ssh",
default => "sshd",
}
service { $ssh_service:
ensure => running,
}
}

7获得系统环境信息

facter命令可以获取全部系统的信息

 要查看关于你的系统中可用的完整的 facts 列表,请运行如下命令:
facter

上面那些变量,你可以在你的 Puppet 配置清单中访问这些 facts

一个简单实例:

在你的.pp文件中加入它,它只是作为一个通知,没有实际意义

 class admin::ntp {
package { "ntp":
ensure => installed,
} service { "ntpd":
ensure => running,
require => Package["ntp"],
} file { "/etc/ntp.conf":
source => "puppet:///modules/admin/ntp.conf",
notify => Service["ntpd"],
require => Package["ntp"],
} notify { "This is $operatingsystem version $operatingsystemrelease, on $architecture architecture, kernel version $kernelversion": }
} 一般放在最后的最外边一层,没有实际意义

客户端运行,看到,截取部分

 Info: Retrieving plugin
Info: Caching catalog for agent1.pup.yxnu
Info: Applying configuration version ''
Notice: /Stage[main]/Admin::Ntp/Notify[This is CentOS version 6.5, on x86_64 architecture, kernel version 2.6.]/message: current_value absent, should be This is CentOS version 6.5, on x86_64 architecture, kernel version 2.6. (noop)

另外说一点:

你也可以在 ERB 模板中使用 facts。例如,你可能会在一个文件中插入一个节点的主机名, 或者基于一个节点的内存大小改变一个应用的配置设置。 当你在模板中使用 fact 的名字时,它们不需要前导的美元符号