puppet的配置清单书写

puppet的配置清单书写

1使用数组,合并同类的

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

 1 以前我们这样来写
 2 class   packages{
 3     package { "sudo" : 
 4     ensure =>installed 
 5                   }
 6 package { "unzip" : 
 7    ensure => installed 
 8              }
 9 package { "locate" : ensure => installed }
10 package { "lsof" : ensure => installed }
11 package { "cron" : ensure => installed }
12 package { "rubygems" : ensure => installed }
13 }

使用数组后就简单了

 1 class  packages{
 2           package { [ "cron",
 3                       "locate",
 4                        "lsof",
 5                        "rubygems"
 6                        "screen",
 7                        "sudo"
 8                        "unzip" ]:
 9                      ensure => installed,
10 }
11 }

 或者这样来写数组

 1 $packages = [ "ruby1.8-dev",
 2               "ruby1.8",
 3               "ri1.8",
 4               "rdoc1.8",
 5               "irb1.8",
 6               "libreadline-ruby1.8",
 7               "libruby1.8",
 8               "libopenssl-ruby" ]
 9 
10 package { $packages: ensure => installed }
View Code

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

使用define来定义资源

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

结果如下:

 1 [root@agent1 etc]# puppet agent --test --noop
 2 Notice: Ignoring --listen on onetime run
 3 Info: Retrieving pluginfacts
 4 Info: Retrieving plugin
 5 Info: Caching catalog for agent1.pup.yxnu
 6 Info: Applying configuration version '1484806313'
 7 Notice: /Stage[main]/Motd/Motd::Tmpfile[b]/File[/tmp/b]/ensure: current_value absent, should be file (noop)
 8 Notice: Motd::Tmpfile[b]: Would have triggered 'refresh' from 1 events
 9 Notice: /Stage[main]/Motd/Motd::Tmpfile[a]/File[/tmp/a]/ensure: current_value absent, should be file (noop)
10 Notice: Motd::Tmpfile[a]: Would have triggered 'refresh' from 1 events
11 Notice: /Stage[main]/Motd/Motd::Tmpfile[c]/File[/tmp/c]/ensure: current_value absent, should be file (noop)
12 Notice: Motd::Tmpfile[c]: Would have triggered 'refresh' from 1 events
13 Notice: Class[Motd]: Would have triggered 'refresh' from 3 events
14 Notice: Stage[main]: Would have triggered 'refresh' from 1 events
15 Notice: Finished catalog run in 0.83 seconds
View Code

 多个参数

 1 define webapp( $domain, $path, $platform ) {
 2     ...
 3 }
 4 
 5 webapp { "mywizzoapp":
 6     domain   => "mywizzoapp.com",
 7     path     => "/var/www/apps/mywizzoapp",
 8     platform => "Rails",
 9 }
10 
11 
12 
13 #你可以使用逗号间隔的列表同时声明多个参数:
View Code

 3资源的依赖关系

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

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

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

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

cat  ntp.pp

 1 class admin::ntp {
 2     package { "ntp":
 3         ensure => installed,
 4     }
 5 
 6     service { "ntpd":
 7         ensure  => running,
 8         require => Package["ntp"],
 9     }
10 
11     file { "/etc/ntp.conf":
12         source  => "puppet:///modules/admin/ntp.conf",
13         notify  => Service["ntpd"],
14         require => Package["ntp"],
15     }
16 }
View Code

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

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

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

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

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

 4继承inherits

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

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

简单实例

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

1 node server {
2     include admin::basics
3     include admin::ssh
4     include admin::ntp
5     include puppet::client
6     include backup::client
7 }
View Code

2然后,继承这个server节点

 1 node wreckspace_server inherits server {
 2     $provider = "WreckSpace"
 3 }
 4 
 5 node gododgy_server inherits server {
 6     $provider = "GoDodgy"
 7 }
 8 
 9 node veryslow_server inherits server {
10     $provider = "VerySlow"
11 }
View Code

 类的继承和重载

参考这里,很详细

5给类传递参数

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

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

声明一个类

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

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

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

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

1 include   eventmachine

说明下;

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

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

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

根据操作系统来安装服务

 1 [root@pup manifests]# cat ssh.pp 
 2 class admin::ssh{
 3    $ssh_service = $operatingsystem? {
 4      /Ubuntu|Debian/ => "ssh",
 5      default => "sshd",
 6 }
 7 service { $ssh_service:
 8     ensure => running,
 9 }
10 }

 7获得系统环境信息

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

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

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

一个简单实例:

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

 1 class admin::ntp {
 2     package { "ntp":
 3         ensure => installed,
 4     }
 5 
 6     service { "ntpd":
 7         ensure  => running,
 8         require => Package["ntp"],
 9     }
10 
11     file { "/etc/ntp.conf":
12         source  => "puppet:///modules/admin/ntp.conf",
13         notify  => Service["ntpd"],
14         require => Package["ntp"],
15     }
16        
17 notify { "This is $operatingsystem version $operatingsystemrelease, on $architecture architecture, kernel version $kernelversion": }
18 }
19 
20 一般放在最后的最外边一层,没有实际意义

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

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

 另外说一点:

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

posted @ 2017-01-19 17:44  Dicky_Zhang  阅读(445)  评论(1编辑  收藏  举报