puppet資源配置

轉自:http://blog.chinaunix.net/uid-23480577-id-3379623.html


puppet資源配置

在puppet中基本的配置宣稱叫做資源,像我們在上次學習中寫到的那樣:
file { "/etc/passwd":
owner => "root",
group => "root",
mode => 644,
}
這是一個簡單的puppet的配置資源,資源是你像管理你的計算機的配置條目,資源包括的條目像文件(file),服務(service),定時工作(cron jobs),用戶組(users),羣組(groups).
你也可以這樣寫

file { "group":
name => "/etc/group",
owner => "root",
}
我們這次不用字面上的名字做爲名字,而是用一個特定的符號名字group,如果這樣做的話我們需要指出他的名字屬性,名字屬性的寫法和資源類型寫法一樣,對於這種方法,文件和服務類型都可以這樣寫。我們可以用符號使配置更容易理解。
這一點需要記着:一旦你使用了符號名,你必須使用他的名字屬性,如:
file { "passwd":
name => "/etc/passwd",
}
file { "/etc/passwd":
}
第一個資源的標題是passwd,第二個資源是/etc/passwd,但是他們管理的都是/etc/passwd
,但對於Puppet來說,他們是不同的資源,因爲他們有不同的標題。

Resource Attributes

在標題後面,緊跟的是特定的屬性,屬性用來告訴Puppet如何去配置資源,我們已經看到了一些屬性,每一個資源都可以設置自己的屬性,比如說資源的屬主,屬組,模式就像我們前面寫的文件資源那樣。
屬性和值的結構
attribute => value,
attribute => value,
每一個屬性鍵值對後面都要有一個逗號,最後一個屬性鍵值對後面可以使用逗號也可以使用分號.
Puppet 有一些本地的屬性值,比如說設置一些屬性爲true或false:
force => true,
值也可以是用戶提供的,比如文件的屬主是root,你就應該寫成"root",用戶提供的值要用加雙引號。如果要在標題或者是值中使用一些像true, false, class,define, inherit等這樣的關鍵字也要加雙引號.

Resource Style

如果一個資源只有一個屬性,它可以被聲明成一行,像這樣:
file { "/etc/group": owner => "root" }
你可以看到owner屬性後面沒加逗號,這種類型的聲明雖然短但是有時候很難讀,因爲這個原因許多人寧願爲每個資源寫多行的結構。多個資源可以被配置在一個資源類型裏面如:
file {
"/etc/passwd":
ensure => present;
"/etc/group" :
owner => "root",
group => "root";
}
上面我已經說過,我們可以用另外一種Puppet形式的資源,如下:
File["/etc/passwd"]
File["/etc/group"]
最近Puppet支持#註釋,如:ensure => running, # this is a comment

Resource Defaults

你可以設置默認的屬性值,並且將其這種類型應用到所有的資源中,這就意味着你不用每次都指定一個特定的屬性.我們看一個默認的例子,用一個可執行的資源類型允許你執行內部腳本或者編程,如:
Exec { path => "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" }
在可執行的資源類型中,path屬性被用來指出搜索路徑,Puppet用於發現那些內部的腳本,這是一個經常使用的腳本。默認的路徑可以被覆蓋,比如:
exec { "shell.rb --run": path => "/opt/bin:/opt/sbin" }
這裏覆蓋了默認的path

Collections of Resources


資 源能在結點上配置在但條目的,但是很多應用和服務都是由許多資源組成的,比如一個web服務器由軟件包,執行軟件的用戶和一些配置,日誌和其他文件.資源 集合允許你把資源聚集到一起,把他們添加到集合,並且把它們應用到1個或多個結點上。有兩種類型的資源集合:classes和definitions。

1個class 是一個資源的集合,它代表你節點上一個單獨的配置項目,SSH服務或者NFS包,Class在一個結點上僅僅形成1次,因爲已經完成的配置應該僅僅存在一次,
一個definition類似,但是代表一個配置項的集合,在你的結點上可以有多個,比如配置Xen實例或者在Apache上配置虛擬主機,在一個結點上definition可以生成多次,每一次帶有不同的輸入參數,


Classes and Subclasses

我們看一個Class

class apache {
package { httpd: ensure => installed }
service { "httpd":
ensure => running,
require => Package["httpd"],
}
}
一個class由一個class語句段定義,後面緊跟的是class的名字,class後的一對大括號是閉合的

Classes Relationships

我們可以在不同的類中指明關係:如下所示
service { "httpd":
ensure => running,
require => Package["httpd"],
}

這裏httpd service 資源包括的資源包Package["httpd"]已經存在了,我們也可以引用class以同樣的方式.
service { "squid":
ensure => running,
retuire => Class["apache"],
}
這裏指明瞭一個類和一個資源的關係,在這個例子中,squid service 需要那個已經存在的apache class ,你可能會注意到像資源的關係,我們用大寫的Class來指明我們把class引用到另一個class當中,在括號裏面壓縮它們的名字.

Class Inheritance

類也有用基類和子類來實現簡單的繼承和覆蓋的模型,一個子類可以繼承基類的值並且能夠實現它一個或者多個值對基類的值進行覆蓋.看下面這個例子:
class readhat {
service {
"mdmdp":
enable => true,
enable => stopped,    
}
}

class rhel5 inherts redhat {
Service["mdmdp"] { ensure => running }
}

這 裏class redhat是基類,定義了一個service 用來控制mdmdp service.它使用service資源類型讓mdmdp服務在啓動時間指定service必須停止。我們然後創建一個新的class,一個子類叫做 rhel5,它繼承了redhat class但是覆蓋了ensure屬性並且指明mdmdp服務必須在所有的含有rhel5子類的結點上進行執行。

除繼承之外,我們還可以在一個class中使用include函數去包含另一個class的內容像這樣:
class proxy {
include proxysquidguard
...
}

從Puppet 0.23.1版本開始,一個新的特性也可以使用拉,它允許你在子類中去爲屬性添加值像這樣:
class proxy {
service { "squid": require => Package["squid"]}
class proxysquidguard inherts proxy {
Service["squid"] { require +> Package["squidguard"]}
}
}

類proxysquidguard 等效於
service { "squid":
require => [ Package["squid"], Package["squidguard"] ]
}
我們也可以這樣用

class proxysquidguard inherits proxy {
Service["squid"] { require => undef }
}

在子類中我們將require屬性的值移去,使用undef這個屬性值.

Definitions

第二種資源類型是定義,定義應該被用作單結點有多實例的配置項,比如,虛擬機或者一個web服務有多個實例,定義用定義的關鍵字和支持的參數但不是繼承去創建,最好思想是定義的可複用性.
一個definition通過關鍵字被創建,爲每一個definition指明一個標題,然後在大括號裏面列出一些參數,這個definition 它自己被指明在下一個大括號裏面,我定義了一個definition 用來執行一個腳本去配置一個新的主機.

define newip ( $ip ) {
exec { "/sbin/ifconfig" $title $ip ":
}
}
new ip { eth0:
ip => "11.11.11.11"
}

我們創建了一個definition叫做newip並且有一個參數叫做$ip,內部定義了我們用來執行資源類型內部二進制代碼,這裏是ifconfig 命令,我們已經指出變量$ip和使用的另一個變量$title
下一行我們真有一個叫做newip的definition,他被上一個definition調用了。
我們使用這個definition時,我們將看到下面的日誌:
notice://newip[eth0]/Exec[/sbin/ifconfig eth0 11.11.11.11]/returns: executed ➥
successfully


我們也可以爲每一個參數指定一個默認的值
define config_file(owner = root, group = root, mode = 0644,
source, backup = false, recurse = false, ensure = file ) {
file{ $name:
mode => $mode,
owner => $owner,
group => $group,
backup => $backup,
recurse => $recurse,
ensure => $ensure,
source => "puppet:///$source"
}
}

config_file { "/etc/vnc.conf":
source => "vnc/vnc.conf",
mode => "0640"
}

我們創建了config_file定義, 然後對其應用,這跟函數調用差不多.

Qualifying Definitions

Qualified definitions允許你執行很多有用的方法,首先你可以在class中這樣定義

class virtuals {
define newip ( $ip ) {
exec { "/sbin/ifconfig $title $ip": 
}

}

virtuals::newip { eth0:
ip => "11.11.11.11",
}

這裏我們定義了一個class叫做virtuals並且裏面放了一個 newip的define,當我們調用的時候使用::
我們也可以用這種方式爲一個definition設置默認值
virtuals::newip { noop => true }
最後我們用definition qualitication去指明獨立性
file { "/etc/hosts.conf":
notify => network::checkhosts[$hostname]
}
前面的一行我們定義了一個文件資源使用了nofify的元參數,這個notify是一個觸發器
如果文件/etc/hosts.con改變,network::checkhosts definition將會被調用
並且將hostname變量傳遞給它

Variables

在前面的你遇到的一些例子中,你遇到一個新概念:變量。變量前綴是$.如
$variable = "string"
你應該把變量值放到雙引號裏面,你可以在你的字符串中對定義過的變量進行引用,如
$variable = "string ${anothervariable} string ${thirdvariable}"

Variable Scoping

你必須知道變量的範圍,首先Puppet是一門弱類型語言,變量的範圍不同於其他語言,在一個範圍之內你不能重新定義一個變量,所以你不能重新定義packagelist
class apache {
$packagelist = ["httpd", "openssl", "mod_ssl"]
package { $packagelist: ensure => installed }
$packagelist = "httpd"
service { "httpd":
ensure => running,
require => Package[$packagelist],
}
}
我們試着定義packagelist兩次,但是如果對其進行編譯,就會產生如下錯誤:
err: Cannot reassign variable packagelist at /etc/puppet/manifests/classes.pp:6
那麼什麼是範圍呢,每個class,每個definition或者結點的介紹都是一個範圍,如class A是一個範圍,Class B是一個範圍,Class C又是一個範圍

class apache {
$apachever = 1
}
class apache2 {
$apachever = 2
}

這樣寫就不會有錯

Variables and Class Inheritance

變量的範圍在類繼承中非常重要,如果我們已經有了一個相同名字的變量在一個類和子類中,它的行爲將不像你希望的那樣,如
class master {
$server = "primary"
file { "/etc/server.conf":
content => "$server",
ensure => present,
}
}
class slave inherits master {
$server = "secondary"
}
在 第一個類中 master 我定義$servers的值爲"primary",第二個類slave中繼承第一個類,我們試着去重定義$server這個變量成 “secondary”,這裏$server重定義將不起作用,因爲在繼承的時候$server還在變量範圍當中,你可以這樣寫:

$server = "primary"
class master {
file { "/etc/server.conf":
content => "$server",
ensure => present,
}
}
class slave {
$server = "secondary"
include master
}
這樣$server的值就爲"secondary"了.

Qualified Variables

我們也可以從一個class到另一個class引用變量或者賦值
class master {
$server = "primary"
}
class slave {
$ms = $master::server
}
我們也可以用下面的這種方式代表最高的範圍
$ms = $::server

Variables and Metaparameters

使用變量句法,你也可以爲一個類中的所有資源設置一個默認的元參數
class start_vhost {
$noop = true
exec { "/usr/sbin/start_ws": }
exec { “/usr.sbin/start_vhost”: }
}
在start_vhost class中,我們指定了空操作的元參數作爲一個變量,並且把它設爲true,在這個類中所有的資源兩個exec資源,這個noop元參數將被添加,並且設爲true。

Arrays

Puppet 也允許你定義數組,像下面那樣,我們爲$packagelist定義了數組,包含了3個包名字:httpd,openssl,mod_ssl
$packagelist = ["httpd",“openssl”,"mod_ssl"]
package { $packagelist: ensure => installed }

Conditionals

Puppet在資源、類、definition和結點中也支持條件,他們用一個默認的觀點以一種選擇的形式
service { "apache":
name => $operatingsystem ? {
debian => "apache2",
redhat => "httpd",
default => "apache",
},
ensure => running,
}
在apache service資源中有一個條件選擇.它的結構是用一個?,然後跟了一個選擇值的列表,也是key => value 這種形式的
注意:如果你不設置默認值,而其他的值都無法匹配時,Puppet將會產生一個錯誤
這還有2種其他的條件語句方法我們可以在mainfrests中應用:case和if。如:
case $operatingsystem {
redhat: { service { "httpd": ensure => running }}
debian: { service { "apache": ensure => running }}
default: { service { "apache2": ensure => running }}
}
case裏面也可以使用變量
case $definedvariable {
"true" => { include class }
default => {}

最後一種條件語句:if,一種非常簡單的if/else語句,只支持bool型的值,如
if $server {
file { "/etc/server.conf": ensure => present }
} else {
file { "/etc/client.conf": ensure => present }
}
如果$server定義了形成第一個文件資源

Creating Nodes
我們用這種方法去做到,讓一些客戶端去執行一些操作讓另一些客戶端去執行另一些操作
一旦一個client連接上了master,那麼他的主機名將會用來匹配結點定義,hostname用來創建客戶端證書。如果匹配不成功顯示
err: Could not retrieve configuration: Could not find node1.testing.com with ➥
names node1.testing.com, node1

結點是如何定義的呢,我們看幾個例子
node 'webserver.testing.com' {
include apache
}
node 'webserver2.testing.com' inherits 'webserver.testing.com' {
include mysql, rails
virtuals::new_vhost { vhost1:
ip => "11.11.11.11",
domainname => "vhost2.testing.com"
}
}
node default {
include $operatingsystem
package { "perl": ensure => present }
}
我們添加一些結點,每個結點和其他結點都以關鍵字進行區分

Facts
Facter 和Puppet有很緊密的聯繫,Puppet允許你爲你的結點定義指定配置文件,Facter允許你從你的結點向Puppet的配置文件中添加信息:比如 知道被配置結點的操作系統,並且爲這個結點定製配置信息,我們這個時候使用的就是Facter根據結點的操作系統平臺選擇爲一個服務選擇一個合適的名字.
Facts在變量定義在你的mainfests範圍內是有效的,你可以看到對於一個特殊系統的facts有用是通過執行二進制的facter來獲得可用性的。一個全列表的facts和他們的值將會被返回,下面列出了在不同平臺中facts的常見返回值.
Fact                      Description
architecture              The architecture opuppet資源配置參考
在puppet中基本的配置宣稱叫做資源,像我們在上次學習中寫到的那樣:
file { "/etc/passwd":
owner => "root",
group => "root",
mode => 644,
}
這是一個簡單的puppet的配置資源,資源是你像管理你的計算機的配置條目,資源包括的條目像文件(file),服務(service),定時工作(cron jobs),用戶組(users),羣組(groups).
你也可以這樣寫

file { "group":
name => "/etc/group",
owner => "root",
}
我們這次不用字面上的名字做爲名字,而是用一個特定的符號名字group,如果這樣做的話我們需要指出他的名字屬性,名字屬性的寫法和資源類型寫法一樣,對於這種方法,文件和服務類型都可以這樣寫。我們可以用符號使配置更容易理解。
這一點需要記着:一旦你使用了符號名,你必須使用他的名字屬性,如:
file { "passwd":
name => "/etc/passwd",
}
file { "/etc/passwd":
}
第一個資源的標題是passwd,第二個資源是/etc/passwd,但是他們管理的都是/etc/passwd
,但對於Puppet來說,他們是不同的資源,因爲他們有不同的標題。

Resource Attributes

在標題後面,緊跟的是特定的屬性,屬性用來告訴Puppet如何去配置資源,我們已經看到了一些屬性,每一個資源都可以設置自己的屬性,比如說資源的屬主,屬組,模式就像我們前面寫的文件資源那樣。
屬性和值的結構
attribute => value,
attribute => value,
每一個屬性鍵值對後面都要有一個逗號,最後一個屬性鍵值對後面可以使用逗號也可以使用分號.
Puppet 有一些本地的屬性值,比如說設置一些屬性爲true或false:
force => true,
值也可以是用戶提供的,比如文件的屬主是root,你就應該寫成"root",用戶提供的值要用加雙引號。如果要在標題或者是值中使用一些像true, false, class,define, inherit等這樣的關鍵字也要加雙引號.

Resource Style

如果一個資源只有一個屬性,它可以被聲明成一行,像這樣:
file { "/etc/group": owner => "root" }
你可以看到owner屬性後面沒加逗號,這種類型的聲明雖然短但是有時候很難讀,因爲這個原因許多人寧願爲每個資源寫多行的結構。多個資源可以被配置在一個資源類型裏面如:
file {
"/etc/passwd":
ensure => present;
"/etc/group" :
owner => "root",
group => "root";
}
上面我已經說過,我們可以用另外一種Puppet形式的資源,如下:
File["/etc/passwd"]
File["/etc/group"]
最近Puppet支持#註釋,如:ensure => running, # this is a comment

Resource Defaults

你可以設置默認的屬性值,並且將其這種類型應用到所有的資源中,這就意味着你不用每次都指定一個特定的屬性.我們看一個默認的例子,用一個可執行的資源類型允許你執行內部腳本或者編程,如:
Exec { path => "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" }
在可執行的資源類型中,path屬性被用來指出搜索路徑,Puppet用於發現那些內部的腳本,這是一個經常使用的腳本。默認的路徑可以被覆蓋,比如:
exec { "shell.rb --run": path => "/opt/bin:/opt/sbin" }
這裏覆蓋了默認的path

Collections of Resources


資 源能在結點上配置在但條目的,但是很多應用和服務都是由許多資源組成的,比如一個web服務器由軟件包,執行軟件的用戶和一些配置,日誌和其他文件.資源 集合允許你把資源聚集到一起,把他們添加到集合,並且把它們應用到1個或多個結點上。有兩種類型的資源集合:classes和definitions。

1個class 是一個資源的集合,它代表你節點上一個單獨的配置項目,SSH服務或者NFS包,Class在一個結點上僅僅形成1次,因爲已經完成的配置應該僅僅存在一次,
一個definition類似,但是代表一個配置項的集合,在你的結點上可以有多個,比如配置Xen實例或者在Apache上配置虛擬主機,在一個結點上definition可以生成多次,每一次帶有不同的輸入參數,


Classes and Subclasses

我們看一個Class

class apache {
package { httpd: ensure => installed }
service { "httpd":
ensure => running,
require => Package["httpd"],
}
}
一個class由一個class語句段定義,後面緊跟的是class的名字,class後的一對大括號是閉合的

Classes Relationships

我們可以在不同的類中指明關係:如下所示
service { "httpd":
ensure => running,
require => Package["httpd"],
}

這裏httpd service 資源包括的資源包Package["httpd"]已經存在了,我們也可以引用class以同樣的方式.
service { "squid":
ensure => running,
retuire => Class["apache"],
}
這裏指明瞭一個類和一個資源的關係,在這個例子中,squid service 需要那個已經存在的apache class ,你可能會注意到像資源的關係,我們用大寫的Class來指明我們把class引用到另一個class當中,在括號裏面壓縮它們的名字.

Class Inheritance

類也有用基類和子類來實現簡單的繼承和覆蓋的模型,一個子類可以繼承基類的值並且能夠實現它一個或者多個值對基類的值進行覆蓋.看下面這個例子:
class readhat {
service {
"mdmdp":
enable => true,
enable => stopped,    
}
}

class rhel5 inherts redhat {
Service["mdmdp"] { ensure => running }
}

這 裏class redhat是基類,定義了一個service 用來控制mdmdp service.它使用service資源類型讓mdmdp服務在啓動時間指定service必須停止。我們然後創建一個新的class,一個子類叫做 rhel5,它繼承了redhat class但是覆蓋了ensure屬性並且指明mdmdp服務必須在所有的含有rhel5子類的結點上進行執行。

除繼承之外,我們還可以在一個class中使用include函數去包含另一個class的內容像這樣:
class proxy {
include proxysquidguard
...
}

從Puppet 0.23.1版本開始,一個新的特性也可以使用拉,它允許你在子類中去爲屬性添加值像這樣:
class proxy {
service { "squid": require => Package["squid"]}
class proxysquidguard inherts proxy {
Service["squid"] { require +> Package["squidguard"]}
}
}

類proxysquidguard 等效於
service { "squid":
require => [ Package["squid"], Package["squidguard"] ]
}
我們也可以這樣用

class proxysquidguard inherits proxy {
Service["squid"] { require => undef }
}

在子類中我們將require屬性的值移去,使用undef這個屬性值.

Definitions

第二種資源類型是定義,定義應該被用作單結點有多實例的配置項,比如,虛擬機或者一個web服務有多個實例,定義用定義的關鍵字和支持的參數但不是繼承去創建,最好思想是定義的可複用性.
一個definition通過關鍵字被創建,爲每一個definition指明一個標題,然後在大括號裏面列出一些參數,這個definition 它自己被指明在下一個大括號裏面,我定義了一個definition 用來執行一個腳本去配置一個新的主機.

define newip ( $ip ) {
exec { "/sbin/ifconfig" $title $ip ":
}
}
new ip { eth0:
ip => "11.11.11.11"
}

我們創建了一個definition叫做newip並且有一個參數叫做$ip,內部定義了我們用來執行資源類型內部二進制代碼,這裏是ifconfig 命令,我們已經指出變量$ip和使用的另一個變量$title
下一行我們真有一個叫做newip的definition,他被上一個definition調用了。
我們使用這個definition時,我們將看到下面的日誌:
notice://newip[eth0]/Exec[/sbin/ifconfig eth0 11.11.11.11]/returns: executed ➥
successfully


我們也可以爲每一個參數指定一個默認的值
define config_file(owner = root, group = root, mode = 0644,
source, backup = false, recurse = false, ensure = file ) {
file{ $name:
mode => $mode,
owner => $owner,
group => $group,
backup => $backup,
recurse => $recurse,
ensure => $ensure,
source => "puppet:///$source"
}
}

config_file { "/etc/vnc.conf":
source => "vnc/vnc.conf",
mode => "0640"
}

我們創建了config_file定義, 然後對其應用,這跟函數調用差不多.

Qualifying Definitions

Qualified definitions允許你執行很多有用的方法,首先你可以在class中這樣定義

class virtuals {
define newip ( $ip ) {
exec { "/sbin/ifconfig $title $ip": 
}

}

virtuals::newip { eth0:
ip => "11.11.11.11",
}

這裏我們定義了一個class叫做virtuals並且裏面放了一個 newip的define,當我們調用的時候使用::
我們也可以用這種方式爲一個definition設置默認值
virtuals::newip { noop => true }
最後我們用definition qualitication去指明獨立性
file { "/etc/hosts.conf":
notify => network::checkhosts[$hostname]
}
前面的一行我們定義了一個文件資源使用了nofify的元參數,這個notify是一個觸發器
如果文件/etc/hosts.con改變,network::checkhosts definition將會被調用
並且將hostname變量傳遞給它

Variables

在前面的你遇到的一些例子中,你遇到一個新概念:變量。變量前綴是$.如
$variable = "string"
你應該把變量值放到雙引號裏面,你可以在你的字符串中對定義過的變量進行引用,如
$variable = "string ${anothervariable} string ${thirdvariable}"

Variable Scoping

你必須知道變量的範圍,首先Puppet是一門弱類型語言,變量的範圍不同於其他語言,在一個範圍之內你不能重新定義一個變量,所以你不能重新定義packagelist
class apache {
$packagelist = ["httpd", "openssl", "mod_ssl"]
package { $packagelist: ensure => installed }
$packagelist = "httpd"
service { "httpd":
ensure => running,
require => Package[$packagelist],
}
}
我們試着定義packagelist兩次,但是如果對其進行編譯,就會產生如下錯誤:
err: Cannot reassign variable packagelist at /etc/puppet/manifests/classes.pp:6
那麼什麼是範圍呢,每個class,每個definition或者結點的介紹都是一個範圍,如class A是一個範圍,Class B是一個範圍,Class C又是一個範圍

class apache {
$apachever = 1
}
class apache2 {
$apachever = 2
}

這樣寫就不會有錯

Variables and Class Inheritance

變量的範圍在類繼承中非常重要,如果我們已經有了一個相同名字的變量在一個類和子類中,它的行爲將不像你希望的那樣,如
class master {
$server = "primary"
file { "/etc/server.conf":
content => "$server",
ensure => present,
}
}
class slave inherits master {
$server = "secondary"
}
在 第一個類中 master 我定義$servers的值爲"primary",第二個類slave中繼承第一個類,我們試着去重定義$server這個變量成 “secondary”,這裏$server重定義將不起作用,因爲在繼承的時候$server還在變量範圍當中,你可以這樣寫:

$server = "primary"
class master {
file { "/etc/server.conf":
content => "$server",
ensure => present,
}
}
class slave {
$server = "secondary"
include master
}
這樣$server的值就爲"secondary"了.

Qualified Variables

我們也可以從一個class到另一個class引用變量或者賦值
class master {
$server = "primary"
}
class slave {
$ms = $master::server
}
我們也可以用下面的這種方式代表最高的範圍
$ms = $::server

Variables and Metaparameters

使用變量句法,你也可以爲一個類中的所有資源設置一個默認的元參數
class start_vhost {
$noop = true
exec { "/usr/sbin/start_ws": }
exec { “/usr.sbin/start_vhost”: }
}
在start_vhost class中,我們指定了空操作的元參數作爲一個變量,並且把它設爲true,在這個類中所有的資源兩個exec資源,這個noop元參數將被添加,並且設爲true。

Arrays

Puppet 也允許你定義數組,像下面那樣,我們爲$packagelist定義了數組,包含了3個包名字:httpd,openssl,mod_ssl
$packagelist = ["httpd",“openssl”,"mod_ssl"]
package { $packagelist: ensure => installed }

Conditionals

Puppet在資源、類、definition和結點中也支持條件,他們用一個默認的觀點以一種選擇的形式
service { "apache":
name => $operatingsystem ? {
debian => "apache2",
redhat => "httpd",
default => "apache",
},
ensure => running,
}
在apache service資源中有一個條件選擇.它的結構是用一個?,然後跟了一個選擇值的列表,也是key => value 這種形式的
注意:如果你不設置默認值,而其他的值都無法匹配時,Puppet將會產生一個錯誤
這還有2種其他的條件語句方法我們可以在mainfrests中應用:case和if。如:
case $operatingsystem {
redhat: { service { "httpd": ensure => running }}
debian: { service { "apache": ensure => running }}
default: { service { "apache2": ensure => running }}
}
case裏面也可以使用變量
case $definedvariable {
"true" => { include class }
default => {}

最後一種條件語句:if,一種非常簡單的if/else語句,只支持bool型的值,如
if $server {
file { "/etc/server.conf": ensure => present }
} else {
file { "/etc/client.conf": ensure => present }
}
如果$server定義了形成第一個文件資源

Creating Nodes
我們用這種方法去做到,讓一些客戶端去執行一些操作讓另一些客戶端去執行另一些操作
一旦一個client連接上了master,那麼他的主機名將會用來匹配結點定義,hostname用來創建客戶端證書。如果匹配不成功顯示
err: Could not retrieve configuration: Could not find node1.testing.com with ➥
names node1.testing.com, node1

結點是如何定義的呢,我們看幾個例子
node 'webserver.testing.com' {
include apache
}
node 'webserver2.testing.com' inherits 'webserver.testing.com' {
include mysql, rails
virtuals::new_vhost { vhost1:
ip => "11.11.11.11",
domainname => "vhost2.testing.com"
}
}
node default {
include $operatingsystem
package { "perl": ensure => present }
}
我們添加一些結點,每個結點和其他結點都以關鍵字進行區分

Facts
Facter 和Puppet有很緊密的聯繫,Puppet允許你爲你的結點定義指定配置文件,Facter允許你從你的結點向Puppet的配置文件中添加信息:比如 知道被配置結點的操作系統,並且爲這個結點定製配置信息,我們這個時候使用的就是Facter根據結點的操作系統平臺選擇爲一個服務選擇一個合適的名字.
Facts在變量定義在你的mainfests範圍內是有效的,你可以看到對於一個特殊系統的facts有用是通過執行二進制的facter來獲得可用性的。一個全列表的facts和他們的值將會被返回,下面列出了在不同平臺中facts的常見返回值.
Fact                      Description
architecture              The architecture of the node, x86_64, for example
domain                    The domain name of the node
facterversion             The version of Facter running on the node
fqdn                      The fully qualified domain name of the node
hardwaremodel             The model of the hardware, for example, x86_64
hostname                  The hostname of the node
id                        The user running Facter
ipaddress                 The IP address
kernel                    The kernel type on the node
kernelrelease             Description
The kernel release of the kernel running on node
lsbdistcodename           The LSB codename of the distribution running on the node
lsbdistdescription        The LSB description of the distribution running on the node
lsbdistid                 The LSB release ID of the distribution running on the node
lsbdistrelease            The LSB release number of the distribution running on node
macaddress                The MAC address of the node
memoryfree                The available memory
memorysize                The total memory size
operatingsystem           The node’s operating system, for example, Fedora
operatingsystemrelease    The release of the node’s operating system
processorx                The make of each processor, includes an entry for each processor,
incremented from 0
processorcount            The total processor count
puppetversion             The version of Puppet on the node
rubyversion               The version of Ruby on the node
sshdsakey                 The node’s public DSA key
sshrsakey                 The node’s public RSA key
swapfree                  The available swap space
swapsize                  The total swap size
在Puppet master上,你可以去訪問其他facts,servername和serverip facts時間哦最重要的,這些返回值返回完整的domain name 和 ip 地址
Facter也可以作爲facts返回環境變量,Facters 假定任意的環境變量都以FACTER_爲前綴,所以建立這些環境變量facts,我們只需要簡單的配置環境變量就可以拉,就像這樣:
export FACTER_statevar="primary"
這樣在Puppet的mainifest裏面就能引用環境變量這個值了用一個變量$statevar就代表了"primary"這個值。
因爲有些環境變量是動態的,因此在默認情況下,這些變量是被Client乎略的
• memorysize
• memoryfree
• swapsize
• swapfree
列表中的facts被乎略要在Puppet配置中對其進行控制.在[puppet]這個名空間中通過dynamicfacts這個配置項
dynamicfacts = memorysize,memoryfree,swapfree,swapsize

Resource Types

我 們已經看到一些關於變量資源類型的例子被用來管理我們結點的資源,我們看到file類型的資源能夠管理文件和目錄,service類型的資源能管理服務和 守護進程,group類型的管理組,package類型的爲我們的結點管理包.我們能利用的不僅僅是這些,那有我們可以在我們結點上配置資源的一個大的關 於資源類型的集合。集合也在社區成員的推動下迅速的發展,下面是我們目前能利用的資源類型:

Resource               Type Description
cron                   Manages cron jobs
exec                   Executes external scripts
file                   Manages files
filebucket             A repository for backing up files
group                  Manages groups
host                   Manages host entries
interface              Configures interfaces (currently only works on Red Hat and Solaris)
mailalias              Manages mail aliases
maillist               Manages mailing lists
mount                  Manages mount entries
notify                 Sends a message to the puppetd log file
package                Manages packages
schedule               Defines Puppet scheduling
service                Manages services
sshkey                 Manages SSH host keys
tidy                   Removes unwanted files
user                   Manages users
yumrepo                Manages YUM repositories
zones                  Manages Solaris zones

先看一下Cron的一個例子
cron { "syscheck":
command => "/usr/bin/syscheck",
user
=> "root",
hour
=> "18",
minute => "0"
}
這 裏定義一個cron類型管理資源,command是cron要執行的命令,user屬性是cron job作爲那個用戶去執行的,hour和minute屬性是cron job執行時間,cron 也支持標準的minutes,hours,days,months attributes cron jobs用來調度

Using a Filebucket

filebucket { main: server => "puppetmaster.testing.com" }

Managing Host Files

host資源用來管理你結點中的host文件,通常指/etc/hosts,但是這種類型也支持使用網絡添加host,你可以看下面這個例子
host { "router":
ensure => present,
ip => "10.0.0.1",
alias => ["router.testing.com", "firewall"]
}
這 種類型有一些屬性,像我們上面用到的那樣,ensure屬性指明是否host信息添加或刪除,當前設置的是添加,設置成absent用來刪除host信 息,使用ip屬性你可以指明是ipv4還是ipv6,最後我們有alias屬性,它允許我們列出主機名,多個主機名要以數組的形式給出。執行完以後在 /etc/hosts多了一行
10.0.0.1 router router.testing.com firewall

Managing SSH Host Keys

sshkey資源類型去管理SSH host的keys,當前資源類型可以被用來在熟悉的hosts文件中爲你的sshserver安裝keys,/etc/ssh/ssh_known_hosts.看一個例子
sshkey { $hostname:
type => dsa,
key => $sshdsakey
}
這 裏我們用了2個facts,我們指明瞭資源標題是$hostname,key的值爲$sshdsakey fact,上面例子是做什麼的呢?爲每一個結點應用,他將把結點DSA的host key 放在/etc/ssh/ssh_know_hosts文件中,我們也可以傳一個知道的host keys列表,或者使用ensure熟悉去指明key應該被添加還是被丟棄

Tidy Unwanted Files

titdy資源類型被用於基於特定的尺度移出不想要的文件,你可以指明一個特定文件的大小創建文件的時間,下面看一個移出的例子:
tidy { "outputs":
path => "/tmp/dboutput.sql",
age => '15m',
before => Service[mysql]
}
上 面的例子是一個tidy資源將刪除文件/tmp/dboutput.sql如果他在15分鐘前建立的,我們給了資源一個象徵性的名字outputs並且使 用path熟悉精確指明瞭文件的路徑,age屬性用來設置文件在建立多長時間後進行刪除。時間可以是 seconds,minutes,hours,days 和 weeks,age的值使用的都是這幾個詞的第一個字母。1s代表一秒,2d代表2天,默認情況下,tidy資源用的是訪問時間。爲了確定一個文件的 age,你可以覆蓋它使用ctime和mtime,如 type => "ctime",我們還可以控制文件大小,使用
size => "10m"

Functions
Functions有兩種形式,statements不返回值,rvalues 返回值
Function      Type              Description
include       Statement         Evaluates one or more classes
realize       Statement         Makes a virtual object real
alert         Statement         Logs a message on the server at level alert
crit          Statement         Logs a message on the server at level crit
debug         Statement         Logs a message on the server at level debug
emerg         Statement         Logs a message on the server at level emerg
err           Statement         Logs a message on the server at level err
info          Statement         Logs a message on the server at level info
notice        Statement         Logs a message on the server at level notice
warning       Statement         Logs a message on the server at level warning
defined       Rvalue            Determines whether a resource or class is defined
fail          Statement         Fails with a parse error
file          Rvalue            Returns the contents of a file or files
generate      Rvalue            Calls an external command and returns the results of the command
search        Statement         Adds another namespace for this class to search
tag           Statement         Adds tags to a class, node, or definition
tagged        Rvalue            A Boolean function that tells you whether the current container is tagged with the specified tags
template      Rvalue            Evaluates templates and returns their values

Logging Functions
alert,crit,debug,emerg,error,notice and warning 允許你向Puppet master發送信息,每個日誌級別都是一個獨立的函數,所以向服務器發送一條notice級別的信息,你可以用notice函數,像這 樣:notice("This is log message sent at notice log level")

Checking for Existence with defined

defined 判斷一個給出的資源或者class 是否被定義,在你include一個class時,你能夠使用這個函數判斷class是否存在,類似這樣:

if defined(webservices) {
include apache
} else {
include lighttpd
}
如果webservices class 被定義了,apache class 應該被包含,否則lighttpd class被包含
你還可以去用defined 函數去驗證是否一個資源被定義:
if defined(Service[sshd]) {...} 
如果sshd 服務被定義了,將執行一個action

Generating Errors with fail

fail函數在強迫一個解析錯誤,並且向服務器返回一個錯誤信息,你可以這樣寫:
fail("This function will result in a parse error")
這將在服務器上產生一個相似的錯誤
err: This function will result in a parse error at ➥
/etc/puppet/manifests/classes.pp:32

Adding External Data with file

file函數返回一個指定文件的內容,用法如下:
file { "resolv.conf":
name => "/etc/resolv.conf",
owner => "root",
group => "root",
content => file("/var/puppet/file/resolv.conf")
}
這 裏我們創建一個新文件資源,然後爲它設置自己的屬主和屬組,我們用file資源的content屬性爲我們新文件提供內容,content屬性內部,我們 調用file函數形成/var/puppet/file/resolv.conf的內容,這個文件必須放在master主機上,我們也可以指定多個文件像 這樣:
file(["/var/puppet/file/hosts", "/var/puppet/file/secondary_hosts"])

Using generate

generate 函數調用外部命令並且返回結果給Puppet,用法如下:
$interfaces = generate("/sbin/ifconfig", "eth0")
這裏我們定義了一個變量叫做$interfaces,它調用了generate 函數,所有的generate 函數必須有一個指明的命令,然後填入若干參數,這兩個直接用逗號分割,返回的結果就是執行命令
# /sbin/ifconfig eth0
將返回結果返回給$interface,命令執行完必須返回狀態碼爲0,返回其他的狀態碼就會造成解釋錯誤

Qualifying Definitions Using search

search 函數允許你引用定義包含在其他classes中,但是不需要限定它
class rails {
define site { ... }
}
class webserver {
search("rails")
site { mysite: ... }
}
這 裏我們定義了2個class,rails 和 webserver,然後又在rails的class中建立一個definition叫做site。我們想在第二個類中引用這個定義,因此,我們加入了 search函數,並且這個class的名字寫成我們要引用的,在這個例子中是rails,我們現在調用site definition,rails::site

Using tag and tagged

tag和tagged這兩個函數能被添加倒結點、類、定義中提供另外的爲他們分類的方法。
node 'node.testing.com' {
tag(devel)
if tagged(devel) {
include dev_test
}
include basics
}
第一行我們指定了一個結點定義,並且使用了tag函數用來增加devel tag,我們然後使用一個條件判斷if 和 tagged 函數,如果devel tag被應用到了那個結點,dev_test class就被包含進來了。
一 些tags是自動創建的,比如,在一個class或node或definition結構中聲明的所有資源將會使用結構的名字被tag,比如如果我們在一個 叫做basic的class中定義了一個file資源,/etc/passwd.這個資源將自動有一個basic的tag添加了進去,另一個例子是當一個 class被一個node包含時,一個和class名字相同的tag被加到那個node裏面,你可以看到他是多麼的有用.

node 'node1.testing.com' {
include webserver
include basics
}
node 'node2.testing.com' {
include databaseserver
include basics
}
class basics {
if tagged(webserver) {
notice("This is a web server")
}
if tagged(databaseserver) {
notice("This is a database server")
}
}
我們創建了兩個node,包含webserver和basic class的node1和包含databaseserber和basic class的node2.
然 後我們定義了basic class。在這個class裏面我們接合使用了if 條件和tagged 函數,在這個例子中,如果結點包含basics class作爲webserver被tagged 一條notice被髮送,如果一個結點帶有databaseserver被tagged一個前者的notice被髮送。

我們還可以選擇那個配置基於tags被實現了,我們在Puppet配置文件中做這些事情通過設置tags的值,配置項像這樣:
[puppetd]
tags = devel
或者pippet執行時,我們這樣做
# puppetd --tags devel
如果多個tags用逗號分割

Using Templating

最後一個函數,tmplate 也是一個最有用的並且允許你去充分利用Ruby ERB的模板,這允許我們創建模板文件,像配置文件,比如能夠從Puppet中遷移配置數據

class resolv {
$searchpath = "testing.com"
$nameservers = ["192.168.0.1", "192.168.0.2"]
file { "resolv.conf":
name => "/etc/resolv.conf",
content => template("resolv-template.erb")
}
}
我 們創建了一個叫做resolv的class,並且指明兩個變量,$searchpath和$nameservers,一個file資源的配置/etc /resolv.conf,我們指出content屬性調用了template函數,我們指明瞭一個模板文件叫做resolv-template.erb 的文件。
模板文件需要放在master機器上,默認情況下,如果你不指明全路徑,Puppet將會在指明templatedir的配置的值中去搜 索你的配置文件,通常templatedir設置爲/var/puppet/templates.在模板內部,你可以指出任意變量,下面的例子你可以使 用$searchpath和$nameservers在特定的模板中,我們看一下模板文件
search <%= searchpath %>
<% nameservers.each do |ns| %>nameserver <%= ns %>
<% end %>
這個ERB模板將收到$searchpath和$nameservers變量,我們已經指出一便Ruby code將遍歷$nameservers數組,如果我們按上面的配置,最終的結果看上去像這樣:

search testing.com
nameserver 192.168.0.1
nameserver 192.168.0.2
f the node, x86_64, for example
domain                    The domain name of the node
facterversion             The version of Facter running on the node
fqdn                      The fully qualified domain name of the node
hardwaremodel             The model of the hardware, for example, x86_64
hostname                  The hostname of the node
id                        The user running Facter
ipaddress                 The IP address
kernel                    The kernel type on the node
kernelrelease             Description
The kernel release of the kernel running on node
lsbdistcodename           The LSB codename of the distribution running on the node
lsbdistdescription        The LSB description of the distribution running on the node
lsbdistid                 The LSB release ID of the distribution running on the node
lsbdistrelease            The LSB release number of the distribution running on node
macaddress                The MAC address of the node
memoryfree                The available memory
memorysize                The total memory size
operatingsystem           The node’s operating system, for example, Fedora
operatingsystemrelease    The release of the node’s operating system
processorx                The make of each processor, includes an entry for each processor,
incremented from 0
processorcount            The total processor count
puppetversion             The version of Puppet on the node
rubyversion               The version of Ruby on the node
sshdsakey                 The node’s public DSA key
sshrsakey                 The node’s public RSA key
swapfree                  The available swap space
swapsize                  The total swap size
在Puppet master上,你可以去訪問其他facts,servername和serverip facts時間哦最重要的,這些返回值返回完整的domain name 和 ip 地址
Facter也可以作爲facts返回環境變量,Facters 假定任意的環境變量都以FACTER_爲前綴,所以建立這些環境變量facts,我們只需要簡單的配置環境變量就可以拉,就像這樣:
export FACTER_statevar="primary"
這樣在Puppet的mainifest裏面就能引用環境變量這個值了用一個變量$statevar就代表了"primary"這個值。
因爲有些環境變量是動態的,因此在默認情況下,這些變量是被Client乎略的
• memorysize
• memoryfree
• swapsize
• swapfree
列表中的facts被乎略要在Puppet配置中對其進行控制.在[puppet]這個名空間中通過dynamicfacts這個配置項
dynamicfacts = memorysize,memoryfree,swapfree,swapsize

Resource Types

我 們已經看到一些關於變量資源類型的例子被用來管理我們結點的資源,我們看到file類型的資源能夠管理文件和目錄,service類型的資源能管理服務和 守護進程,group類型的管理組,package類型的爲我們的結點管理包.我們能利用的不僅僅是這些,那有我們可以在我們結點上配置資源的一個大的關 於資源類型的集合。集合也在社區成員的推動下迅速的發展,下面是我們目前能利用的資源類型:

Resource               Type Description
cron                   Manages cron jobs
exec                   Executes external scripts
file                   Manages files
filebucket             A repository for backing up files
group                  Manages groups
host                   Manages host entries
interface              Configures interfaces (currently only works on Red Hat and Solaris)
mailalias              Manages mail aliases
maillist               Manages mailing lists
mount                  Manages mount entries
notify                 Sends a message to the puppetd log file
package                Manages packages
schedule               Defines Puppet scheduling
service                Manages services
sshkey                 Manages SSH host keys
tidy                   Removes unwanted files
user                   Manages users
yumrepo                Manages YUM repositories
zones                  Manages Solaris zones

先看一下Cron的一個例子
cron { "syscheck":
command => "/usr/bin/syscheck",
user
=> "root",
hour
=> "18",
minute => "0"
}
這 裏定義一個cron類型管理資源,command是cron要執行的命令,user屬性是cron job作爲那個用戶去執行的,hour和minute屬性是cron job執行時間,cron 也支持標準的minutes,hours,days,months attributes cron jobs用來調度

Using a Filebucket

filebucket { main: server => "puppetmaster.testing.com" }

Managing Host Files

host資源用來管理你結點中的host文件,通常指/etc/hosts,但是這種類型也支持使用網絡添加host,你可以看下面這個例子
host { "router":
ensure => present,
ip => "10.0.0.1",
alias => ["router.testing.com", "firewall"]
}
這 種類型有一些屬性,像我們上面用到的那樣,ensure屬性指明是否host信息添加或刪除,當前設置的是添加,設置成absent用來刪除host信 息,使用ip屬性你可以指明是ipv4還是ipv6,最後我們有alias屬性,它允許我們列出主機名,多個主機名要以數組的形式給出。執行完以後在 /etc/hosts多了一行
10.0.0.1 router router.testing.com firewall

Managing SSH Host Keys

sshkey資源類型去管理SSH host的keys,當前資源類型可以被用來在熟悉的hosts文件中爲你的sshserver安裝keys,/etc/ssh/ssh_known_hosts.看一個例子
sshkey { $hostname:
type => dsa,
key => $sshdsakey
}
這 裏我們用了2個facts,我們指明瞭資源標題是$hostname,key的值爲$sshdsakey fact,上面例子是做什麼的呢?爲每一個結點應用,他將把結點DSA的host key 放在/etc/ssh/ssh_know_hosts文件中,我們也可以傳一個知道的host keys列表,或者使用ensure熟悉去指明key應該被添加還是被丟棄

Tidy Unwanted Files

titdy資源類型被用於基於特定的尺度移出不想要的文件,你可以指明一個特定文件的大小創建文件的時間,下面看一個移出的例子:
tidy { "outputs":
path => "/tmp/dboutput.sql",
age => '15m',
before => Service[mysql]
}
上 面的例子是一個tidy資源將刪除文件/tmp/dboutput.sql如果他在15分鐘前建立的,我們給了資源一個象徵性的名字outputs並且使 用path熟悉精確指明瞭文件的路徑,age屬性用來設置文件在建立多長時間後進行刪除。時間可以是 seconds,minutes,hours,days 和 weeks,age的值使用的都是這幾個詞的第一個字母。1s代表一秒,2d代表2天,默認情況下,tidy資源用的是訪問時間。爲了確定一個文件的 age,你可以覆蓋它使用ctime和mtime,如 type => "ctime",我們還可以控制文件大小,使用
size => "10m"

Functions
Functions有兩種形式,statements不返回值,rvalues 返回值
Function      Type              Description
include       Statement         Evaluates one or more classes
realize       Statement         Makes a virtual object real
alert         Statement         Logs a message on the server at level alert
crit          Statement         Logs a message on the server at level crit
debug         Statement         Logs a message on the server at level debug
emerg         Statement         Logs a message on the server at level emerg
err           Statement         Logs a message on the server at level err
info          Statement         Logs a message on the server at level info
notice        Statement         Logs a message on the server at level notice
warning       Statement         Logs a message on the server at level warning
defined       Rvalue            Determines whether a resource or class is defined
fail          Statement         Fails with a parse error
file          Rvalue            Returns the contents of a file or files
generate      Rvalue            Calls an external command and returns the results of the command
search        Statement         Adds another namespace for this class to search
tag           Statement         Adds tags to a class, node, or definition
tagged        Rvalue            A Boolean function that tells you whether the current container is tagged with the specified tags
template      Rvalue            Evaluates templates and returns their values

Logging Functions
alert,crit,debug,emerg,error,notice and warning 允許你向Puppet master發送信息,每個日誌級別都是一個獨立的函數,所以向服務器發送一條notice級別的信息,你可以用notice函數,像這 樣:notice("This is log message sent at notice log level")

Checking for Existence with defined

defined 判斷一個給出的資源或者class 是否被定義,在你include一個class時,你能夠使用這個函數判斷class是否存在,類似這樣:

if defined(webservices) {
include apache
} else {
include lighttpd
}
如果webservices class 被定義了,apache class 應該被包含,否則lighttpd class被包含
你還可以去用defined 函數去驗證是否一個資源被定義:
if defined(Service[sshd]) {...} 
如果sshd 服務被定義了,將執行一個action

Generating Errors with fail

fail函數在強迫一個解析錯誤,並且向服務器返回一個錯誤信息,你可以這樣寫:
fail("This function will result in a parse error")
這將在服務器上產生一個相似的錯誤
err: This function will result in a parse error at ➥
/etc/puppet/manifests/classes.pp:32

Adding External Data with file

file函數返回一個指定文件的內容,用法如下:
file { "resolv.conf":
name => "/etc/resolv.conf",
owner => "root",
group => "root",
content => file("/var/puppet/file/resolv.conf")
}
這 裏我們創建一個新文件資源,然後爲它設置自己的屬主和屬組,我們用file資源的content屬性爲我們新文件提供內容,content屬性內部,我們 調用file函數形成/var/puppet/file/resolv.conf的內容,這個文件必須放在master主機上,我們也可以指定多個文件像 這樣:
file(["/var/puppet/file/hosts", "/var/puppet/file/secondary_hosts"])

Using generate

generate 函數調用外部命令並且返回結果給Puppet,用法如下:
$interfaces = generate("/sbin/ifconfig", "eth0")
這裏我們定義了一個變量叫做$interfaces,它調用了generate 函數,所有的generate 函數必須有一個指明的命令,然後填入若干參數,這兩個直接用逗號分割,返回的結果就是執行命令
# /sbin/ifconfig eth0
將返回結果返回給$interface,命令執行完必須返回狀態碼爲0,返回其他的狀態碼就會造成解釋錯誤

Qualifying Definitions Using search

search 函數允許你引用定義包含在其他classes中,但是不需要限定它
class rails {
define site { ... }
}
class webserver {
search("rails")
site { mysite: ... }
}
這 裏我們定義了2個class,rails 和 webserver,然後又在rails的class中建立一個definition叫做site。我們想在第二個類中引用這個定義,因此,我們加入了 search函數,並且這個class的名字寫成我們要引用的,在這個例子中是rails,我們現在調用site definition,rails::site

Using tag and tagged

tag和tagged這兩個函數能被添加倒結點、類、定義中提供另外的爲他們分類的方法。
node 'node.testing.com' {
tag(devel)
if tagged(devel) {
include dev_test
}
include basics
}
第一行我們指定了一個結點定義,並且使用了tag函數用來增加devel tag,我們然後使用一個條件判斷if 和 tagged 函數,如果devel tag被應用到了那個結點,dev_test class就被包含進來了。
一 些tags是自動創建的,比如,在一個class或node或definition結構中聲明的所有資源將會使用結構的名字被tag,比如如果我們在一個 叫做basic的class中定義了一個file資源,/etc/passwd.這個資源將自動有一個basic的tag添加了進去,另一個例子是當一個 class被一個node包含時,一個和class名字相同的tag被加到那個node裏面,你可以看到他是多麼的有用.

node 'node1.testing.com' {
include webserver
include basics
}
node 'node2.testing.com' {
include databaseserver
include basics
}
class basics {
if tagged(webserver) {
notice("This is a web server")
}
if tagged(databaseserver) {
notice("This is a database server")
}
}
我們創建了兩個node,包含webserver和basic class的node1和包含databaseserber和basic class的node2.
然 後我們定義了basic class。在這個class裏面我們接合使用了if 條件和tagged 函數,在這個例子中,如果結點包含basics class作爲webserver被tagged 一條notice被髮送,如果一個結點帶有databaseserver被tagged一個前者的notice被髮送。

我們還可以選擇那個配置基於tags被實現了,我們在Puppet配置文件中做這些事情通過設置tags的值,配置項像這樣:
[puppetd]
tags = devel
或者pippet執行時,我們這樣做
# puppetd --tags devel
如果多個tags用逗號分割

Using Templating

最後一個函數,tmplate 也是一個最有用的並且允許你去充分利用Ruby ERB的模板,這允許我們創建模板文件,像配置文件,比如能夠從Puppet中遷移配置數據

class resolv {
$searchpath = "testing.com"
$nameservers = ["192.168.0.1", "192.168.0.2"]
file { "resolv.conf":
name => "/etc/resolv.conf",
content => template("resolv-template.erb")
}
}
我 們創建了一個叫做resolv的class,並且指明兩個變量,$searchpath和$nameservers,一個file資源的配置/etc /resolv.conf,我們指出content屬性調用了template函數,我們指明瞭一個模板文件叫做resolv-template.erb 的文件。
模板文件需要放在master機器上,默認情況下,如果你不指明全路徑,Puppet將會在指明templatedir的配置的值中去搜 索你的配置文件,通常templatedir設置爲/var/puppet/templates.在模板內部,你可以指出任意變量,下面的例子你可以使 用$searchpath和$nameservers在特定的模板中,我們看一下模板文件
search <%= searchpath %>
<% nameservers.each do |ns| %>nameserver <%= ns %>
<% end %>
這個ERB模板將收到$searchpath和$nameservers變量,我們已經指出一便Ruby code將遍歷$nameservers數組,如果我們按上面的配置,最終的結果看上去像這樣:

search testing.com
nameserver 192.168.0.1
nameserver 192.168.0.2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章