1. 程式人生 > >Puppet package資源介紹(二十四)

Puppet package資源介紹(二十四)

puppet package資源介紹(二十四)


package資源

package資源可以借助本地包管理系統幫我們安裝軟件,也可以通過參數指定軟件包來安裝.


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軟件包.

[[email protected] ~]# cat httpd.pp 
package {"httpd":
    ensure => present,
    provider => ‘yum‘,
}


provider:這個參數默認也是從yum源安裝,所以不需要也是可以的,除非你需要特別安裝安裝包.


運行結果:

[[email protected] ~]# 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

檢查:

[[email protected] ~]# rpm -qa httpd
httpd-2.2.15-60.el6.centos.5.x86_64


示例二:

安裝haproxy.(通過source源指定軟件包,通過rpm方式安裝.)


安裝haproxy的puppet代碼.

[[email protected] ~]# cat haproxy.pp 
package {"haproxy":
    ensure => present,
    source => ‘/tmp/haproxy-1.5.2-2.el6.x86_64.rpm‘,
    provider => ‘rpm‘,
}


運行puppet代碼的結果.

[[email protected] ~]# 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
[[email protected] ~]# rpm -qa haproxy
haproxy-1.5.2-2.el6.x86_64


示例三:

puppet 一次安裝多個軟件包.

[[email protected] ~]# cat many.pp 
package {["autoconf",
    "mysql-devel",
    "make",
    "gcc"]:
    ensure => present,
}

運行結果:

[[email protected] ~]# 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
[[email protected] ~]# rpm -qa make
make-3.81-23.el6.x86_64
[[email protected] ~]# rpm -qa autoconf
autoconf-2.63-5.1.el6.noarch
[[email protected] ~]# rpm -qa mysql-devel
mysql-devel-5.1.73-8.el6_8.x86_64


前幾篇文章中有一個php模塊的puppet代碼如下:

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"],
    }
}


$packages = [‘php‘,‘php-devel‘]
package {[$packages]:
    ensure=> "installed"
}


$packages 等於的並不是數組,是數組中的元素,所以下面的package安裝還是需要"[]"的.


官網給出的示例

name


# 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‘],
}


本文出自 “青衫解衣” 博客,請務必保留此出處http://215687833.blog.51cto.com/6724358/1973370

Puppet package資源介紹(二十四)