Puppet条件语句,class,module的使用

简介:

一、Puppet条件语句和类

   Puppet2.7系列的版本支持使用3种条件判断语句,包括:if,case,selector,puppet也支持使用class是用于通用目标的一组资源,因此,它是命名的代码块,在某位置创建之后可在puppet全局使用,类似于其他编程语言中类的功能,puppet的class可以被继承,也可以包含子类。


二、Puppet模块

   到目前为止,资源申报、定义类、声明类等所有功能都只能再一个manifest文件中实现,但这却非最有效的基于puppet管理IT基础架构的方式,实践中,一般需要把manifest文件分解成易于理解的结构,例如将类文件,配置文件、模块文件等分类存放,并且通过某种机制在必要时将它们整合起来,这种机制就是“模块”,它有助于以结构化、层次化的方式使用puppet,而puppet则基于“模块自动装载器”完成模块装载,模块实际就是一个按约定的、预定义的结构存放了多个文件或子目录的目录,目录里的这些文件或子目录必须遵循其命名规范,puppet会按此种规范在特定位置查找所需要的模块文件,不过,这些特定的目录也可以通过puppet的配置参数modulepath定义。




首先来演示下if语句的使用,if又包括单分支,双分支,多分支语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@node1 ~] # vi test6.pp
if  $operatingsystem =~ /(?i-mx:^(centos|fedora|redhat))/ {
         $webserver =  'httpd'
} elsif operatingsystem  =~ /(?i-mx:^(debian|ubuntu))/ {
         $webserver =  'apache2'
else  {
         $webserver = undef
         notice( 'Unkown OS' )
}
package { "$webserver" :
         ensure => installed,
}
[root@node1 ~] # puppet apply test6.pp
notice:  /Stage [main] //Package [httpd] /ensure : created
notice: Finished catalog run  in  2.91 seconds
[root@node1 ~] # rpm -q httpd
httpd-2.2.15-30.el6.centos.x86_64
# 使用多分支if判断语句,operatingsystem顶级变量,根据当前系统安装所要安装的程序包


case语句,类似if语句,case语句会从多个代码块中选择一个分支执行,这跟其他编程语言中的case语句功能一致,case语句会接受一个控制表达式和一组case代码块,并执行第一个匹配到控制表达式的块

1
2
3
4
5
6
7
8
9
10
[root@node1 ~] # vi test7.pp
case  $operatingsystem {
         'Solaris' :              {notice( "Welcome to Solaris" )}
         'RedHat' , 'CentOS' :      {notice( "Welcome to RedHat OSFamily" )}
         /^(Debian|Ubuntu)$/:    {notice( "Welcome to $1 linux" )}
         default:                {notice( "Welcome,alien *_*" )}
}
[root@node1 ~] # puppet apply test7.pp
notice: Scope(Class[main]): Welcome to RedHat OSFamily
notice: Finished catalog run  in  0.01 seconds



selector语句,selector只能用于期望出现直接值的地方,这包括变量赋值,资源属性,函数参数,资源标题,其他selector的值及表达式,selector不能用于一个已经嵌套于selector的case中,也不能用于一个已经嵌套于case的case语句中

1
2
3
4
5
6
7
8
9
10
11
12
[root@node1 ~] # vi test8.pp
$webserver = $operatingsystem ? {
    /(?i-mx:ubuntu|debian)/        =>  'apache2' ,
    /(?i-mx:centos|fedora|redhat)/ =>  'httpd' ,
}
notify { "$webserver" :
         message =>  "install $webserver" ,
}
[root@node1 ~] # puppet apply test8.pp
notice:  install  httpd
notice:  /Stage [main] //Notify [httpd] /message : defined  'message'  as  'install httpd'
notice: Finished catalog run  in  0.02 seconds


class的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[root@node1 ~] # vi test9.pp
class nginx {
         package { 'nginx' :
                 ensure => installed,
                 before => File[ '/etc/nginx/nginx.conf' ],
                 }
         file  { '/etc/nginx/nginx.conf' :
                 ensure =>  file ,
                 mode => 0644,
                 owner =>  'root' ,
                 group =>  'root' ,
                 source  =>  '/tmp/nginx.conf' ,
                 notify => Service[ 'nginx' ],
              }
         service {  'nginx' :
                 ensure => running,
                 }
}
include nginx
# 使用include来调用class
[root@node1 ~] # puppet apply test9.pp
notice:  /Stage [main] /Nginx/Package [nginx] /ensure : created
notice:  /Stage [main] /Nginx/File [ /etc/nginx/nginx .conf] /content : content changed  '{md5}d9dfc198c249bb4ac341198a752b9458'  to  '{md5}95f45f10386878664af2b7ccd1536ea4'
notice:  /Stage [main] /Nginx/Service [nginx] /ensure : ensure changed  'stopped'  to  'running'
notice:  /Stage [main] /Nginx/Service [nginx]: Triggered  'refresh'  from 1 events
notice: Finished catalog run  in  2.16 seconds
# 也可以使用以下方式调用,这种方式可以传递参数
class nginx {
         package { 'nginx' :
                 ensure => installed,
                 before => File[ '/etc/nginx/nginx.conf' ],
                 }
         file  { '/etc/nginx/nginx.conf' :
                 ensure =>  file ,
                 mode => 0644,
                 owner =>  'root' ,
                 group =>  'root' ,
                 source  =>  '/tmp/nginx.conf' ,
                 notify => Service[ 'nginx' ],
              }
         service {  'nginx' :
                 ensure => running,
                 }
}
class { 'nginx' :}
[root@node1 ~] # puppet apply test9.pp
notice:  /Stage [main] /Nginx/Package [nginx] /ensure : created
notice:  /Stage [main] /Nginx/File [ /etc/nginx/nginx .conf] /content : content changed  '{md5}d9dfc198c249bb4ac341198a752b9458'  to  '{md5}95f45f10386878664af2b7ccd1536ea4'
notice:  /Stage [main] /Nginx/Service [nginx] /ensure : ensure changed  'stopped'  to  'running'
notice:  /Stage [main] /Nginx/Service [nginx]: Triggered  'refresh'  from 1 events
notice: Finished catalog run  in  2.06 seconds


使用class方法调用类,还可以传递参数实现,根据参数来安装不同的服务,比如想安装tengine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[root@node1 ~] # vi test9.pp
class webserver ($wbsvr= 'nginx' ) {
         package { "$wbsvr" :
                 ensure => installed,
                 before => File[ '/etc/nginx/nginx.conf' ],
                 }
         file  { '/etc/nginx/nginx.conf' :
                 ensure =>  file ,
                 mode => 0644,
                 owner =>  'root' ,
                 group =>  'root' ,
                 source  =>  '/tmp/nginx.conf' ,
                 notify => Service[ 'nginx' ],
              }
         service {  'nginx' :
                 ensure => running,
                 }
}
class { 'webserver' :
         wbsvr =>  'nginx' ,
}
[root@node1 ~] # puppet apply test9.pp
notice:  /Stage [main] /Webserver/Package [nginx] /ensure : created
notice:  /Stage [main] /Webserver/File [ /etc/nginx/nginx .conf] /content : content changed  '{md5}d9dfc198c249bb4ac341198a752b9458'  to  '{md5}95f45f10386878664af2b7ccd1536ea4'
notice:  /Stage [main] /Webserver/Service [nginx] /ensure : ensure changed  'stopped'  to  'running'
notice:  /Stage [main] /Webserver/Service [nginx]: Triggered  'refresh'  from 1 events
notice: Finished catalog run  in  2.09 seconds
[root@node1 ~] # vi test9.pp
class webserver ($wbsvr= 'nginx' ) {
         package { "$wbsvr" :
                 ensure => installed,
                 before => File[ '/etc/nginx/nginx.conf' ],
                 }
         file  { '/etc/nginx/nginx.conf' :
                 ensure =>  file ,
                 mode => 0644,
                 owner =>  'root' ,
                 group =>  'root' ,
                 source  =>  '/tmp/nginx.conf' ,
                 notify => Service[ 'nginx' ],
              }
         service {  'nginx' :
                 ensure => running,
                 }
}
class { 'webserver' :
         wbsvr =>  'tenginx' ,
}
[root@node1 ~] # puppet apply test9.pp
err:  /Stage [main] /Webserver/Package [tenginx] /ensure : change from absent to present failed: Execution of  '/usr/bin/yum -d 0 -e 0 -y install tenginx'  returned 1: Error: Nothing to  do
notice:  /Stage [main] /Webserver/File [ /etc/nginx/nginx .conf]: Dependency Package[tenginx] has failures:  true
warning:  /Stage [main] /Webserver/File [ /etc/nginx/nginx .conf]: Skipping because of failed dependencies
notice:  /Stage [main] /Webserver/Service [nginx]: Dependency Package[tenginx] has failures:  true
warning:  /Stage [main] /Webserver/Service [nginx]: Skipping because of failed dependencies
notice: Finished catalog run  in  0.41 second


类也可以被继承,实现根据不同情况调用不同的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
[root@node1 ~] # vi test10.pp
class nginx {
         package { 'nginx' :
                 ensure => installed,
         }
}
class nginx::websvr inherits nginx {
         file  { '/etc/nginx/nginx.conf' :
                 ensure =>  file ,
                 mode =>  '0644' ,
                 owner =>  'root' ,
                 group =>  'root' ,
                 require => Package[ 'nginx' ],
                 source  =>  '/tmp/nginx.conf' ,
         }
         service { 'nginx' :
                 ensure => running,
         }
}
class nginx::rproxy inherits nginx {
         file  { '/etc/nginx/nginx.conf' :
                 ensure =>  file ,
                 mode =>  '0644' ,
                 owner =>  'root' ,
                 group =>  'root' ,
                 require => Package[ 'nginx' ],
                 source  =>  '/tmp/nginx-proxy.conf' ,
         }
         service { 'nginx' :
                 ensure => running,
         }
}
include nginx::websvr
[root@node1 ~] # puppet apply test10.pp
notice:  /Stage [main] /Nginx/Package [nginx] /ensure : created
notice:  /Stage [main] /Nginx ::Websvr /File [ /etc/nginx/nginx .conf] /content : content changed  '{md5}d9dfc198c249bb4ac341198a752b9458'  to  '{md5}95f45f10386878664af2b7ccd1536ea4'
notice:  /Stage [main] /Nginx ::Websvr /Service [nginx] /ensure : ensure changed  'stopped'  to  'running'
notice: Finished catalog run  in  1.81 seconds
# 调用nginx::rproxy类
[root@node1 ~] # vi test10.pp
class nginx {
         package { 'nginx' :
                 ensure => installed,
         }
}
class nginx::websvr inherits nginx {
         file  { '/etc/nginx/nginx.conf' :
                 ensure =>  file ,
                 mode =>  '0644' ,
                 owner =>  'root' ,
                 group =>  'root' ,
                 require => Package[ 'nginx' ],
                 source  =>  '/tmp/nginx.conf' ,
         }
         service { 'nginx' :
                 ensure => running,
         }
}
class nginx::rproxy inherits nginx {
         file  { '/etc/nginx/nginx.conf' :
                 ensure =>  file ,
                 mode =>  '0644' ,
                 owner =>  'root' ,
                 group =>  'root' ,
                 require => Package[ 'nginx' ],
                 source  =>  '/tmp/nginx-proxy.conf' ,
         }
         service { 'nginx' :
                 ensure => running,
         }
}
include nginx::rproxy
[root@node1 ~] # puppet apply test10.pp
notice: Finished catalog run  in  0.15 seconds


puppet模块的创建和使用


1
2
# 安装puppet-server
[root@node1 ~] # rpm -ivh puppet-server-2.7.23-1.el6.noarch.rpm



1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建模块目录结构
[root@node1 ~] # mkdir -pv /etc/puppet/modules/nginx/{manifests,files,lib,templates,tests,spec}
mkdir : created directory ` /etc/puppet/modules/nginx '
mkdir : created directory ` /etc/puppet/modules/nginx/manifests '
mkdir : created directory ` /etc/puppet/modules/nginx/files '
mkdir : created directory ` /etc/puppet/modules/nginx/lib '
mkdir : created directory ` /etc/puppet/modules/nginx/templates '
mkdir : created directory ` /etc/puppet/modules/nginx/tests '
mkdir : created directory ` /etc/puppet/modules/nginx/spec '
[root@node1 ~] # puppet module list
/etc/puppet/modules
└── nginx (???)
/usr/share/puppet/modules  (no modules installed)


MODULE NAME:模块名称,只能以小写字母开头,也可以包含小写字母、数字和下划线,但不能使用“main”或者“settings”作为模块名;

manifests目录:包含当前模块的所有manifest文件;每个mainfest文件必须包含一个类或一个定义的类,此文件访问路径格式为"ModuleName::[SubDircetoryName::]ManifestFileName",注意manifest文件不需要其后缀.pp;

init.pp:只能包含一个单独的类定义,且类的名称必须与模块名称相同;

files目录:包含了一组静态文件,这些文件可被节点下载使用,每个文件的访问路径遵循

puppet:///modules/MODULE_NAME/filename路径格式;

lib目录:插件目录,常用于自定义fact及自定义资源类型等;

templates目录:存储了manifest用到的模板文件,其访问路径遵循template('ModulName/TemplateName')格式;

tests目录:当前模块的使用帮助或使用范例文件,类似如何声明当前模块中的类定义的类型等;

spec目录:类似于tests目录的功能,只不过,其是为lib目录中定义的各插件提供使用的范例的;


在nginx模块中定义init.pp

1
2
3
4
5
6
[root@node1 ~] # vi /etc/puppet/modules/nginx/manifests/init.pp
class nginx {
         package { 'nginx' :
               ensure => installed,
         }
}


定义nginx_web.pp文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@node1 ~] # vi /etc/puppet/modules/nginx/manifests/nginx_web.pp
class nginx::nginx_web inherits nginx {
         file  { '/etc/nginx/nginx.conf' :
             ensure =>  file ,
             source  =>  'puppet:///modules/nginx/nginx-web.conf' ,
             mode =>  '0644' ,
             owner =>  'root' ,
             group =>  'root' ,
             notify => Service[ 'nginx' ],
             require => Package[ 'nginx' ],
         }
         service { 'nginx' :
             ensure => running,
}
}
# 准备source文件
[root@node1 ~] # cp /tmp/nginx.conf /etc/puppet/modules/nginx/files/nginx-web.conf

定义nginx_proxy.pp文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@node1 ~] # vi /etc/puppet/modules/nginx/manifests/nginx_proxy.pp
class nginx::nginx_proxy inherits nginx {
         file  { '/etc/nginx/nginx.conf' :
             ensure =>  file ,
             source  =>  'puppet:///modules/nginx/nginx-proxy.conf' ,
             mode =>  '0644' ,
             owner =>  'root' ,
             group =>  'root' ,
             notify => Service[ 'nginx' ],
             require => Package[ 'nginx' ],
         }
         service { 'nginx' :
             ensure => running,
}
}
# 准备source文件
[root@node1 ~] # cp /tmp/nginx.conf /etc/puppet/modules/nginx/files/nginx-proxy.conf

创建site.pp文件调用前面定义的class

1
2
3
4
[root@node1 ~] # vi /etc/puppet/manifests/site.pp
node  'node1.luojianlong'  {
         include nginx::nginx_web
}


执行操作

1
2
3
4
5
6
[root@node1 ~] # puppet apply /etc/puppet/manifests/site.pp
notice:  /Stage [main] /Nginx/Package [nginx] /ensure : created
notice:  /Stage [main] /Nginx ::Nginx_web /File [ /etc/nginx/nginx .conf] /content : content changed  '{md5}d9dfc198c249bb4ac341198a752b9458'  to  '{md5}95f45f10386878664af2b7ccd1536ea4'
notice:  /Stage [main] /Nginx ::Nginx_web /Service [nginx] /ensure : ensure changed  'stopped'  to  'running'
notice:  /Stage [main] /Nginx ::Nginx_web /Service [nginx]: Triggered  'refresh'  from 1 events
notice: Finished catalog run  in  2.05 seconds


这样就是使用nginx-web.conf配置文件启动的nginx

也可以使用nginx-proxy.conf,启动

1
2
3
4
5
6
7
8
9
10
11
12
[root@node1 ~] # vi /etc/puppet/manifests/site.pp
node  'node1.luojianlong'  {
         include nginx::nginx_proxy
}
[root@node1 ~] # yum -y remove nginx
[root@node1 ~] # puppet apply /etc/puppet/manifests/site.pp
notice:  /Stage [main] /Nginx/Package [nginx] /ensure : created
notice:  /Stage [main] /Nginx ::Nginx_proxy /File [ /etc/nginx/nginx .conf] /content : content changed  '{md5}d9dfc198c249bb4ac341198a752b9458'  to  '{md5}95f45f10386878664af2b7ccd1536ea4'
notice:  /Stage [main] /Nginx ::Nginx_proxy /Service [nginx] /ensure : ensure changed  'stopped'  to  'running'
notice:  /Stage [main] /Nginx ::Nginx_proxy /Service [nginx]: Triggered  'refresh'  from 1 events
notice: Finished catalog run  in  2.11 seconds
# 执行成功


到此,Puppet条件语句,class,module的使用就介绍这么多了。




     本文转自ljl_19880709 51CTO博客,原文链接:http://blog.51cto.com/luojianlong/1394883,如需转载请自行联系原作者





相关文章
|
网络安全 开发工具
|
Ubuntu 应用服务中间件 Linux
|
应用服务中间件 Apache nginx
|
Ubuntu 数据中心 网络协议
《Puppet实战手册》——2.8 编写功能强大的条件语句
此外,也可以通过在清单内设置变量来调整引入的类的行为。例如,在数据中心A的服务器可能需要使用与在数据中心B不同的DNS服务器,或者可能需要为Ubuntu系统使用一组类,而其他系统使用另一组不同的类。
1730 0
|
25天前
|
运维 Linux Apache
Puppet 作为一款强大的自动化运维工具,被广泛应用于配置管理领域。通过定义资源的状态和关系,Puppet 能够确保系统始终处于期望的配置状态。
Puppet 作为一款强大的自动化运维工具,被广泛应用于配置管理领域。通过定义资源的状态和关系,Puppet 能够确保系统始终处于期望的配置状态。
48 3
|
安全 Linux 网络协议
puppet yum模块、配置仓储、mount模块
转载:http://blog.51cto.com/ywzhou/1577335 作用:自动为客户端配置YUM源,为使用yum安装软件包提供便捷。 1、服务端配置yum模块 (1)模块清单 [root@puppet ~]# tree /etc/puppe...
1113 0
|
网络协议 安全 网络安全