package资源

package资源可以借助本地包管理系统帮我们安装软件,也可以通过参数指定软件包来安装.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package {  'resource title' :
   provider             =>  # (namevar) The specific backend to use for this `package...
   name                 =>  # (namevar) The package name.  This is the name that the...
   ensure               =>  # What state the package should be in. On...
   adminfile            =>  # A file containing package defaults for...
   allow_virtual        =>  # Specifies if virtual package names are allowed...
   allowcdrom           =>  # Tells apt to allow cdrom sources in the...
   category             =>  # A read-only parameter set by the...
   configfiles          =>  # Whether to keep or replace modified config files 
   description          =>  # A read-only parameter set by the...
   flavor               =>  # OpenBSD supports 'flavors', which are further...
   install_options      =>  # An array of additional options to pass when...
   instance             =>  # A read-only parameter set by the...
   package_settings     =>  # Settings that can change the contents or...
   platform             =>  # A read-only parameter set by the...
   reinstall_on_refresh =>  # Whether this resource should respond to refresh...
   responsefile         =>  # A file containing any necessary answers to...
   root                 =>  # A read-only parameter set by the...
   source                =>  # Where to find the package file. This is only...
   status               =>  # A read-only parameter set by the...
   uninstall_options    =>  # An array of additional options to pass when...
   vendor               =>  # A read-only parameter set by the...
   # ...plus any applicable metaparameters.
}


provider:包管理系统,不同的操作平台有着不同的包管理器,如redhat的yum,ubuntu的dpkg等.


name:软件包的名字.


ensure :软件包的状态,installed或present表示安装,absent表示卸载;pureged表示一处软件包;latest表示安装最新的。


adminfile:软件包的默认配置文件.


allowcdrom:通知apt允许使用cdrom作为软件源,可以设置false或者true.


allow_virtual:虚拟包名.


source:软件包的源.


install_options:安装软件包的参数.


参数很多,有安装的也有卸载的卸载参数uninstall_options,不一个一个写了.



示例一:

系统源安装httpd软件包.

1
2
3
4
5
[root@sh-web1 ~] # cat httpd.pp 
package { "httpd" :
     ensure => present,
     provider =>  'yum' ,
}


provider:这个参数默认也是从yum源安装,所以不需要也是可以的,除非你需要特别安装安装包.


运行结果:

1
2
3
4
[root@sh-web1 ~] # puppet apply httpd.pp 
Notice: Compiled catalog  for  sh-web1.localdomain  in  environment production  in  0.04 seconds
Notice:  /Stage [main] /Main/Package [httpd] /ensure : created
Notice: Finished catalog run  in  5.69 seconds

检查:

1
2
[root@sh-web1 ~] # rpm -qa httpd
httpd-2.2.15-60.el6.centos.5.x86_64


示例二:

安装haproxy.(通过source源指定软件包,通过rpm方式安装.)


安装haproxy的puppet代码.

1
2
3
4
5
6
[root@sh-web1 ~] # cat haproxy.pp 
package { "haproxy" :
     ensure => present,
     source  =>  '/tmp/haproxy-1.5.2-2.el6.x86_64.rpm' ,
     provider =>  'rpm' ,
}


运行puppet代码的结果.

1
2
3
4
5
6
[root@sh-web1 ~] # puppet apply haproxy.pp 
Notice: Compiled catalog  for  sh-web1.localdomain  in  environment production  in  0.04 seconds
Notice:  /Stage [main] /Main/Package [haproxy] /ensure : created
Notice: Finished catalog run  in  0.61 seconds
[root@sh-web1 ~] # rpm -qa haproxy
haproxy-1.5.2-2.el6.x86_64


示例三:

puppet 一次安装多个软件包.

1
2
3
4
5
6
7
[root@sh-web1 ~] # cat many.pp 
package {[ "autoconf" ,
     "mysql-devel" ,
     "make" ,
     "gcc" ]:
     ensure => present,
}

运行结果:

1
2
3
4
5
6
7
8
9
10
[root@sh-web1 ~] # puppet apply many.pp 
Notice: Compiled catalog  for  sh-web1.localdomain  in  environment production  in  0.04 seconds
Notice:  /Stage [main] /Main/Package [mysql-devel] /ensure : created
Notice: Finished catalog run  in  14.96 seconds
[root@sh-web1 ~] # rpm -qa make
make -3.81-23.el6.x86_64
[root@sh-web1 ~] # rpm -qa autoconf
autoconf-2.63-5.1.el6.noarch
[root@sh-web1 ~] # rpm -qa mysql-devel
mysql-devel-5.1.73-8.el6_8.x86_64


前几篇文章中有一个php模块的puppet代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class php {
include php::phpfpmconf
     $packages = [ 'php' , 'php-devel' ]
     package {[$packages]:
     ensure=>  "installed"
}
package { "php-fpm" :
     ensure => present,
}
service { "php-fpm" :
     ensure=> running,
     enable =>  true ,
     hasrestart=>  true ,
     hasstatus=>  true ,
     provider => init,
     require=> Package[ "php-fpm" ],
     }
}


1
2
3
4
$packages = [ 'php' , 'php-devel' ]
package {[$packages]:
     ensure=>  "installed"
}


$packages 等于的并不是数组,是数组中的元素,所以下面的package安装还是需要"[]"的.


官网给出的示例

name


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# In the 'openssl' class
$ssl = $operatingsystem ? {
   solaris => SMCossl,
   default => openssl
}
package {  'openssl' :
   ensure => installed,
   name   => $ssl,
}
. etc. .
$ ssh  = $operatingsystem ? {
   solaris => SMCossh,
   default => openssh
}
package {  'openssh' :
   ensure  => installed,
   name    => $ ssh ,
   require => Package[ 'openssl' ],
}