一、Puppet模塊、 類、 模板
資源、 類、 模塊、 模板構建成了Puppet的核部分。 通過資源的定義告訴Puppet應該去做什麼; 可 將多個資源封裝成類; 爲管理便, 還會將多個類組合成模塊。 可由節點直接引類名, 有系統變量時可通過模板來傳遞。
模塊( Module) , 多個類的集合。 個應編寫個模塊。
類( Class) , 多個資源的集合。 對應實例的不同功能進封裝。
模板( Template) , ERB模板。 根據服務器硬件信息定製化不同配置件。
1、模塊結構
Puppet模塊包含代碼和數據包。 可以按Puppet語法規範編寫的模塊, 然後分享到互聯供他 使, 也可以使Puppet Forge和Example42從上下載後直接使或修改後使。 下我們通過圖1來了解Puppet模塊結構。
圖1以nginx模塊爲例對Puppet模塊進了簡單的剖析, 並沒有包含全部, 但以說明它的內部核 結構及組成。 Puppet模塊核組成包括: manifests、 files、 templates( erb) 、 lib, 還可以包括tests、 spec。查看圖1的錄樹, 具體如下:
$ tree /etc/puppet/modules/nginx ├── LICENSE ├── Modulefile ├── README ├── files //文件存儲目錄 │ └── nginx.conf ├── manifests //清單存儲目錄 │ └── init.pp //必須包含且只能包含一個與模塊同名的類 │ └── vhost.pp │ └── config.pp //每個清單文件通常只包含一個類 ├── templates //模板存儲目錄 │ └── vhost.conf.erb
2、模塊管理
要進模塊管理需要先了解modulepath。 將模塊存放在modulepath錄, 以便Puppet可以查找得 到。 如果是創建模塊, 建議在模塊根錄創建Modulefile和README: Modulefile對模塊版本進說明, README對模塊使進說明。
實例: 創建個模塊
在創建模塊前我們需要先了解模塊的錄modulepath。 可以在終端下使puppet apply命令和 modulespath參數查看當前Puppet的模塊配置錄:
# puppet apply --configprint modulepath /etc/puppet/modules:/opt/puppet/share/puppet/modules
通常, 默認錄爲/etc/puppet/modules, 因此我們需要將編寫的模塊信息放在此錄中, 以便 Puppet可以查找得到。以圖1爲例, 創建個nginx模塊。 爲快速瞭解模塊結構, 本例只創建init.pp、 nginx.conf.erb及模 塊說明件。
創建模塊所需要的錄。
$ cd /etc/puppet/modules $ mkdir -p nginx/{manifests,templates,files}
爲了簡便起見, 本測試實例僅創建三個核錄, 具體如下:
創建模塊配置件manifests代碼。
$ cd /etc/puppet/modules/nginx/manifests $ vim init.pp class nginx { package { 'nginx': ensure => installed, } service { 'nginx': name => nginx, ensure => running, enable => true, subscribe => File['nginx.conf'], } file { 'nginx.conf': ensure => present, mode => 644, owner => root, group => root, path => '/etc/nginx/nginx.conf', content => template("nginx/nginx.conf.erb"), }}
manifests代碼主要是init.pp, 具體如下:
manifests代碼中定義了個類, 類名爲nginx, 類中包含3個資源: 軟件包、 服務及件。 其中服務資源依賴於件資源, nginx.conf件內容採content屬性定義來源於template中對應的nginx.conf.erb模板內容。
使template函數創建ERB模板。
$ cd /etc/puppet/modules/nginx/templates $ sudo vim nginx.conf.erb user daemon daemon; worker_processes<%= processorcount %>; pid /var/run/nginx.pid; error_log /var/log/nginx/error.log; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include /etc/nginx/mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; server { listen 80; server_name _; location /nginx_status { stub_status on; access_log off; } } }
模板的內容爲nginx主配置件nginx.conf。 其中作進程數worker_processes由facts進傳遞。 實 現代碼如下:
創建說明件, 實現代碼如下:
$ cd /etc/puppet/modules/nginx $ sudo vim Modulefile name 'nginx' version '0.0.1' author 'liuyu' license 'Apache License Version 2.0' summary 'Puppet NGINX management module' description 'This module can be used for basic NGINX Management' project_page 'http://xxxx/nginx' $ sudo vim README # NGINX Module Yu Liu <[email protected]> This module manages NGINX from within Puppet. USAGE: # Standard Classes include nginx
模塊佈局:
在使模塊前我們先看下Puppet針對模塊的佈局是什麼樣的。 我們將按照錄層次來逐進 介紹。 假設我們創建的模塊my_module包括如下代碼:
/etc/puppet/modules/my_module/ |-- files |-- lib |-- manifests | |-- config.pp | |-- implementation | | `-- foo.pp | |-- init.pp | `-- vhost.pp |-- templates | `-- vhost.conf.erb `-- tests
#級目錄, 模塊名my_module
#級目錄,manifests、 templates、 files、 lib、 tests
#三級目錄, 可定義, 例如, implementation
#級目錄manifests定義模塊清單
| |-- config.pp #配置件定義, 使法: my_module::config
| |-- implementation
| | `-- foo.pp #使my_module::implementation::foo
| |-- init.pp #包含類的所有定義, 類名需要與模塊名匹配
| `-- vhost.pp #虛擬主機件定義, 使的法爲my_module::vhost
|-- files #靜態件, 節點下載使
|-- templates #模板清單件
|--vhost.conf.erb
|-- lib #插件, 定義facts與定義資源類型
|-- tests #展如何使資源、 類等
對於在前面創建的nginx模塊, 其佈局如下:
# tree/etc/puppet/modules/nginx /etc/puppet/modules/nginx |-- Modulefile |-- README |-- files |-- manifests | `-- init.pp `-- templates |-- nginx.conf.erb
通過README的說明, 在節點配置件中進include nginx配置就可以引nginx模塊中的nginx 類。 定義節點agent.domain.com的配置件如下:
$ sudo vim /etc/puppet/manifests/agent.domain.com.pp node 'agent.domain.com' { include nginx }
在agent.domain.com客戶端執如下puppet命令:
# puppet agent --test --server puppet.domain.com Info: Retrieving plugin Info: Caching catalog for agent.domain.com Info: Applying configuration version '1358083364' #省略中間過程 Finished catalog run in 3.91 seconds
到此爲我們已經完成模塊的創建, 在創建過程過學習了模塊的佈局。 當在模塊中定義的類和使 的資源較多時, 資源之間依賴關係將變得較爲複雜, 這給我們編寫模塊帶來了不便。 Puppet提供資源依賴圖, 在客戶端執puppet命令時加上graph參數可以成關係依賴圖。 剛纔編寫的nginx模塊中到的資源關係依賴圖如圖2所。
3、類管理
Class是用於通用目標或目的的一組資源,因此,它是命名的代碼塊,在某位置創建之後可在puppet全局使用。類似於其它編程語言中的類的功能,puppet的類可以繼承,也可以包含子類。類名在manifests的實現代碼中的init.pp中進定義, 個模塊也可以創建多個類, 在init.pp中定 義的類可以稱爲基礎類, 通常基礎類名與模塊名致。
類的定義 :
類的定義通過: 個關鍵字及
個代碼塊實現, 具體如下:
class class_name { … }
如果在同個模塊定義了多個類, 可以採雙冒號( :: ) 。 例如定義個nginx模塊, 模塊中 定義三個類:
class nginx { … } class nginx::config { … } class nginx::vhost { … }
在前面我們約定config與vhost同爲nginx的類, nginx類聲明這兩個類時使了require:
class nginx { require nginx::config require nginx::vhost …}
類的繼承 :
類的繼承主要是個類繼承另個類, 且可以實現覆蓋資源屬性、 追加資源屬性。 下以nginx模 塊代碼爲例來介紹類的繼承。
定義nginx爲類, nginx: : foo爲類。 類的代碼如下:
nginx { service { 'nginx': name => nginx, ensure => running, enable => true, require => File['nginx.conf'], } }
繼承資源屬性
nginx: : foo繼承nginx類的資源屬性且不做修改, 代碼如下:
nginx::foo inherits nginx { service['nginx'] { ensure => running, enable => true } }
注意 :當有基類( base class) 的繼承關係時, Puppet2.7將不再持有參數的類作爲基類, 基類必須是參的。
覆蓋資源屬性
nginx: : foo繼承nginx類的資源屬性並將enable屬性覆蓋, 代碼如下:
nginx::foo inherits nginx { service['nginx'] { ensure => running, enable =>false } }
上述代碼實現了將類的enable參數原值從true修改爲false, 即開機時不啓動nginx。 如果定義了基類, 也可以繼承並修改, 代碼如下:
class base::freebsd inherits base::unix { File['/etc/passwd'] { group => 'wheel' } File['/etc/shadow'] { group => 'wheel' } }
如果想取消以上代碼中某個屬性的值, 可以使undef值覆蓋原有值, 結果就像是此屬性從來都沒 有被定義過樣。 具體法如下:
class base::freebsd inherits base::unix { File['/etc/passwd'] { group =>undef, } }
追加資源屬性
nginx: : foo繼承nginx類的資源屬性, 並通過require對資源屬性進值追加:
nginx::foo inherits nginx { service { 'nginx': require +> [ File['nginx.conf'], File['foo.conf']], } }
在上述代碼中, 通過require+>進參數追加後, 在類nginx: : foo中所定義的service資源變爲依 賴於兩個件才能啓動nginx, 其他參數默認將全部繼承, 即nginx: : foo變爲:
nginx::foo { service { 'nginx': nam => nginx, ensure => running, enable => true, require=> File['nginx.conf'],File['foo.conf'], } }
參數化類 :
如果個類被聲明多次, 並存在相互盾的參數值, 這個時候Puppet會編譯失敗。 有效的解決法 就是使Hiera( 可稱爲參數動查詢) 。 爲解決個類被聲明多次時參數的衝突可以採如下種做法:
Hiera使參數化類, 向上兼容Puppet 3.0。
使“經典”模塊設計, 不使參數化類。
嚴格使“純”參數化類, 可能使ENC。
使混合模式, 使Hiera或參數化類。
參數化類是指通過動態變量的查找將參數傳遞到類。 可以在個節點範圍內設置些變量, 進類 的聲明, 並將變量分配給範圍, 這個類就會去查找所需的信息並進應。 參數化類的缺點如下:
基本上所有變量將應到全局命名空間。 這意味着需要知道所定義的所有變量, 避免重複, 否則會產衝突。
沒有不完全聲明個類。 沒有內置的式告訴哪些參數需要設置哪個類, 所以需要記 錄並維護。
較混亂。 範圍的作域可以在別的地引, 這會導致作域的失效。
可以說參數化的類有定的好處, 但規模應時卻常煩, 會增加管理成本, 這需要讀者根據 的環境選擇不同的配置式。 使“經典”模塊化配置或參數化配置。
( 1) 參數化類的定義
class nginx ( $packages, $port ) { … }
參數化類的定義與傳統式樣, 類名與左括號之間採括號進參數列表的定義, 具體如下:
( 2) 參數化類的使
局部變量$packages和$port在類的定義中可作爲參數名稱, 也可以在類作域內引, 具體如下:
#作爲參數名稱 class nginx( $packages, $port ) { package { $packages: ensure => present } #在類作域內引 file { 'nginx.conf': port => $port, ensure => file, } }
( 3) 參數設置默認值
爲某些參數指定默認值, 具體如下:
class nginx( $packages, $port = '80' ) { … }
或者使如下代碼爲所有參數指定默認值, 具體如下:
class nginx( packages = 'nginx', port = '80' ) { … }
( 4) 聲明參數化類
在定義類的屬性時聲明指定的參數, 具體如下:
class { 'nginx' : $packages =>'nginx', $port =>'80' ,}
或者聲明所有的默認值, 具體如下:
class { 'nginx' : }
( 5) 參數化類的應例
下我們將通過則判斷實現來看下參數化類的應:
$ vim /etc/puppet/modules/nginx/manifests/params.pp class nginx::params { $packages = $operatingsystem ? { /(?i-mx:ubuntu|debian)/ => 'nginx', /(?i-mx:centos|fedora|redhat)/ => 'nginx2', } $port = $operatingsystem ? { /(?i-mx:ubuntu|debian)/ => '80', /(?i-mx:centos|fedora|redhat)/ => '81', } $ vim /etc/puppet/modules/nginx/manifests/init.pp class nginx ( $packages = $nginx::params::packages, $port = $nginx::params::port ) inherits nginx::params { package { $packages: ensure => present } file { 'nginx.conf': port => $port, ensure => file, } }
在以上代碼清單中聲明參數作爲類定義的部分, 類的聲明中包含$packages與$port兩個參數。nginx類在應時會根據系統的判斷傳遞兩個變量值, 並繼承nginx: : params的屬性。
再來看另外個例。 聲明參數作爲如下類定義的部分:
class eventmachine( $version ) { package { "eventmachine": provider => gem, ensure => $version, } }
在個節點上使如下語法來包含類:
class { "eventmachine": version => "0.12.8" }
在以上代碼中, 定義的類帶了個參數$version。 在節點加載eventmachine類時需要爲參數
$version指定了個值。
4、模板管理
定義與聲明
Puppet模板主要是基於ERB模板語的, 是Ruby語標準庫中的部分。 在模板中可以使的變 量是調此模板的當前類中的所有變量, 包含facts、 全局變量、 在類中定義的當前作域變量。
( 1) 標籤
除打上標籤的內容外, ERB模板中的內容會原封不動地傳送回Puppet。 ERB標籤是以尖括號對在 外、 百分號對在內爲標誌, 其中可包含或多Ruby代碼, 具體如下:
<% document = "" %>
( 2) 打印表達式
要打印個表達式, 需要使打印標籤, 就是在正常標籤的左側百分號後加個等於號。 例如, 在 打印個普通的標籤sectionheader時, 可以通過如下代碼實現打印個值:
<%= sectionheader %>
或者通過以下代碼實現任意複雜的Ruby表達式:
environment = <%= gitrevision[0,5] %>
這樣變量$sectionheader的值會直接打印到標籤所在位置。
( 3) 註釋
註釋代碼不會被解析爲代碼, 也不會顯式輸出, 例如:
<%# This comment will be ignored. %>
( 4) 禁換
如果ERB代碼有明確的寫法, 可以使如下代碼避免換帶來的空影響美觀。
<% document += thisline-%>
例如定義個模板件時需要在對應的模塊錄下創建相應的以.rb結尾的件。 假設模塊錄 爲/etc/puppet/modules。 以nginx模塊爲例, 創建個vhost.conf.erb模板件, 在模板件中定義變量 $port, 代碼如下:
在聲明這個模板時需要在資源中使content, 代碼如下:
$ cd /etc/puppet/modules/nginx/templates $ vim vhost.conf.erb server { listen $port; server_name _; location /nginx_status { stub_status on; access_log off; } }
在聲明這個模板時需要在資源中使content, 代碼如下:
$ vim vhost.pp define nginx::vhost ($port) { file { 'vhost.conf': path => '/etc/nginx/config/vhosts/vhost.conf', ensure => file, require => Package['nginx'], content => template('nginx/vhost.conf.erb') } }
Puppet會查找nginx模塊錄下template錄中的 vhost.conf.erb( /etc/puppet/modules/nginx/templates/vhost.conf.erb) 件。 ERB件的內容即 vhots.conf件的內容。
值得注意的是, 以上ERB模板中僅定義個變量$port。 ERB更重要的功能是: 可以通過facter命 令獲取變量進傳遞。 如果ERB模板是數組, 也可以採如下代碼進聲明:
content => template('nginx/vhost.conf.erb', 'nginx/config.erb')
Puppet會運算所有的模板, 並將所有輸出合併成個字符串並返回。
ERB模板語法
ERB模板的配置通常取決於所使的配置式, 它是Ruby標準庫中的個模板。 常的配置語法 如下:
%= Ruby expression %> #直接替換成表達式的值
<% Ruby code %> #Ruby代碼, 包括條件、 循環等
<%# comment %> #註釋
<%% or %%> #等同於<% or %>, 分別進
<%- #等同於<%, 閉合標籤, 控制代碼前輸出量空格
-%> #等同於%>, 閉合標籤, 控制代碼後輸出量空
(1)常規定義
在配置語法中, <%=Ruby expression %>是最常見的法, 例如將ipaddress作爲變量:
<%= @ipaddress %>
#寫成<%= ipaddress %> Puppet也能識別
ipaddress的值將通過facter傳遞:
$ facter|grep ipaddress ipaddress => 192.168.1.126 ipaddress_en0 => 192.168.1.126 ipaddress_lo0 => 127.0.0.1
(2)引變量
在當前範圍內的變量可以作爲Ruby的實例變量, 例如@fqdn、 @operatingsystem。 在當前範圍內的變量也可以作爲Ruby的局部變量, 使fqdn、 operatingsystem, 前沒有@符號。 這種使法有可能導致變量名衝突, 所以建議使@符號。
Puppet傳遞個scope對象到模板中, 其中包含當前設定的變量, 以及其他數據( 函數) 等。 也可 以使scope對象的lookupvar法找到任何變量。
超出作域範圍的變量
如果想在本地作域外查找變量, 可以使scope.lookupvar法:
<%= scope.lookupvar('apache::user') %>
也可以像下這樣使, 以確保在本地作域內能得到個變量:
<%= scope.lookupvar('::domain') %>
測試個不明確的變量
當創建的變量具有不確定因素時, 可以使if @variable的法:
<% if @myvar %> myvar has <%= @myvar %> value <% end %>
這時候其他模板通常會使has_variable? ( "myvar") 幫助函數, 但這可能會導致變量結果爲 undef, 這需要避免。
如果需要測試的變量在當前作域名之外, 應該將其複製到局部變量中測試, 具體如下:
獲取當前作域下所有變量名
如果使的作域在個to_hash下, 可以得到個在當前作域下的所有哈希變量定義列表。 這 個哈希列表使多個變量( osfamily) , 不是合格的名稱( : : osfamily) 。
如下代碼將打印當前作域下定義的所有變量名:
<% scope.to_hash.keys.each do |k| -%> <%= k %> <% end -%>
(3)模板結合
如果有多個模板時, 可以使逗號進結合, 例如:
template('my_module/template1.erb','my_module/template2.erb')
(4)數組迭代
ERB模板同樣持數組迭代。 如果要訪問的變量是個數組, 可以循環地遍歷它, 例如:
定義ERB模板, 代碼如下:
<% values.each do |val| -%> Some stuff with <%= val %> <% end -%>
這時候輸出的結果爲:
Some stuff with val1 Some stuff with val2 Some stuff with otherval
這使了閉合標籤-%>以避免產換。
利數組的迭代進循環遍歷, 較常見的使環境就是實現四層代理軟件Haproxy的後端多臺 Server配置。 假設Haproxy後端有3臺Web Server, 那麼Haproxy.conf配置件應該配置爲:
global … defaults … listen localhost 0.0.0.0:80 mode http option httpchk GET /index.htm server s1 192.168.1.3:80 weight 3 check server s2 192.168.1.4:80 weight 3 check server s2 192.168.1.5:80 weight 3 check
要實現以上代碼中的配置件, 可以定義ERB模板件內容爲:
# more /etc/puppet/modules/haproxy/templates/haproxy.conf.erb global …d efaults …l isten localhost 0.0.0.0:80 mode http option httpchk GET /index.htm <% instance=1 %> <% server.each do |ip| %>server s<%=instance%> <%=ip%>:80 weight 3 check <% instance+=1 end -%>
在節點配置中定義數組爲:
# more /etc/puppet/manfiests/nodes/agent.domain.com.pp node 'agent.domain.com' { $server = ['192.168.1.3', '192.168.1.4', '192.168.1.5'] include haproxy }
(5)條件表達式
ERB模板中條件表達式的使法如下:
<% if broadcast != "NONE" %> broadcast <%= broadcast %><% end %>
(6)語法檢查
語法檢查使的是Ruby語的檢查法, 具體如下:
erb -P -x -T '-' mytemplate.erb | ruby -c
5、融合
回顧下所定義的模塊內容。
模塊名: nginx。
類名: nginx。
類名: nginx: : configng、 inx: : vhost。
件: nginx.conf。
ERB模板: vhost.conf.erb。
變量: $port 。
追加個facts變量: $ipaddress。
要聲明此類, 需要在節點代碼中進include, 具體如下:
$ sudo vim /etc/puppet/manifests/nodes/agent.domain.com.pp node 'agent.domain.com' { #加載nginx模塊 include nginx }
再將此節點加站點件site.pp中進import, 具體如下:
$ sudo vim /etc/puppet/manifests/site.pp #追加如下 import "nodes/agent.domain.com.pp"
通過圖3可以直觀地看到Puppet是如何查找定義的件並進作的。
二、應用舉例
1、 使用帶參數的類:
$webserver = $operatingsystem ? { /^(?i-mx:redhat|centos|fedora)/ => 'httpd', /^(?i-mx:ubuntu|debian)/ => 'apache2', } class httpd ($pkgname = 'apache2') { package {"$pkgname": ensure => present, } service {"$pkgname": ensure => true, require => Package["$pkgname"], } } class {'httpd': pkgname => $webserver, }
2、類繼承
[[email protected] tmp]# vim test10.pp class nginx { package {"nginx": ensure =>present, } } class nginx::rproxy inherits nginx { file {'/etc/nginx/nginx.conf': ensure => file, source => '/tmp/nginx/nginx.reverse_proxy.conf', require => Package['nginx'], force => true, notify => Service['nginx'], } service {'nginx': ensure => true, } } class nginx::web inherits nginx { file {'/etc/nginx/nginx.conf': ensure => file, source => '/tmp/nginx/nginx.web.conf', notify => Service['nginx'], } service {'nginx': ensure => true, } } [[email protected] tmp]# mkdir nginx
將兩者個配置文件做下區別:
[[email protected] tmp]# grep "worker_connections" nginx/nginx.web.conf worker_connections 10222; [[email protected] tmp]# grep "worker_connections" nginx/nginx.reverse_proxy.conf worker_connections 10240; [[email protected] tmp]# vim node.pp import "/tmp/test10.pp" include nginx::web [[email protected] tmp]# puppet apply node.pp [[email protected] tmp]# grep "worker_connections" /etc/nginx/nginx.conf worker_connections 10222;
3、創建nginx的模塊
nginx安裝 :nginx包
做爲web服務的配置文件:nginx::web 包含:file 、service
做爲反向代理的配置文件:nginx::rproxy 包含:file 、service
當前系統puppet查找的默認配置目錄:
[[email protected] modules]# puppet agent --configprint modulepath /etc/puppet/modules:/usr/share/puppet/modules [[email protected] modules]# mkdir nginx/{manifests,files,templates} -pv [[email protected] modules]# cd nginx/manifests/ [[email protected] manifests]# vim init.pp class nginx { package {'nginx': ensure => present, } } [[email protected] nginx]# cp /etc/nginx/nginx.conf files/nginx.web.conf [[email protected] nginx]# cp /etc/nginx/nginx.conf files/nginx.rproxy.conf [[email protected] nginx]# grep 'worker_connections' files/nginx.web.conf worker_connections 10222; [[email protected] nginx]# grep 'worker_connections' files/nginx.rproxy.conf worker_connections 10240; [[email protected] nginx]# cd manifests/ [[email protected] manifests]# vim web.pp class nginx::web inherits nginx { file {'nginx.conf': ensure => file, source => "puppet:///modules/nginx/nginx.web.conf", path => '/etc/nginx/nginx.conf', require => Package['nginx'], mode => '0644', notify => Service['nginx'], } service {'nginx': ensure => true, enable => true, restart => '/etc/init.d/nginx reload', } } [[email protected] manifests]# vim rproxy.pp class nginx::rproxy inherits nginx { file {'nginx.conf': ensure => file, source => "puppet:///modules/nginx/nginx.rproxy.conf", path => '/etc/nginx/nginx.conf', require => Package['nginx'], mode => '0644', notify => Service['nginx'], } service {'nginx': ensure => true, enable => true, restart => '/etc/init.d/nginx reload', } }
測試:
第一種使用模塊方法:
[[email protected] manifests]# rpm -e nginx [[email protected] manifests]# rm -rf /etc/nginx/ [[email protected] manifests]# puppet apply -e 'include nginx::rproxy' notice: /Stage[main]/Nginx/Package[nginx]/ensure: created notice: /Stage[main]/Nginx::Rproxy/File[nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}44223269ca784ab1bdd91ce78a042feb' notice: /Stage[main]/Nginx::Rproxy/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: /Stage[main]/Nginx::Rproxy/Service[nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 11.08 seconds [[email protected] manifests]# grep 'worker_connections' /etc/nginx/nginx.conf worker_connections 10240;
第二種使用模塊方法:
[[email protected] ~]# cat local.pp node 'www.example.com' { include nginx ::web } [[email protected] ~]# puppet apply local.pp notice: /Stage[main]/Nginx::Web/File[nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}09bd56f2a8067c5a9644380a682ff85f' notice: /Stage[main]/Nginx::Web/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: /Stage[main]/Nginx::Web/Service[nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 0.53 seconds [[email protected] ~]# grep 'worker_connections' /etc/nginx/nginx.conf worker_connections 10222;
4、模版的使用舉例
[[email protected] manifests]# vim init.pp class nginx { package {'nginx': ensure => present, } file {'nginx.conf': ensure => file, content => template('nginx/nginx.conf.erb'), path => '/etc/nginx/nginx.conf', require => Package['nginx'], mode => 0644, } } [[email protected] manifests]# rm -f ../files/nginx.* [[email protected] manifests]# cd ../files/ [[email protected] files]# ls [[email protected] files]# cp /etc/nginx/conf.d/default.conf nginx.web.conf [[email protected] files]# cp /etc/nginx/conf.d/default.conf nginx.rproxy.conf [[email protected] files]# ls nginx.rproxy.conf nginx.web.conf [[email protected] files]# vim nginx.web.conf server { listen 80 default_server; server_name www.example.com; #charset koi8-r; #access_log logs/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html; index index.html index.htm; } [[email protected] files]# vim nginx.rproxy.conf server { listen 80 default_server; server_name _; #charset koi8-r; #access_log logs/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { #root /usr/share/nginx/html; #index index.html index.htm; proxy_pass http://192.168.1.9; } [[email protected] files]# cp /etc/nginx/nginx.conf ../templates/ [[email protected] files]# cd ../templates/ [[email protected] templates]# vim nginx.conf worker_processes <%= @processorcount %>; [[email protected] templates]# mv nginx.conf nginx.conf.erb [[email protected] templates]# cd ../manifests/ [[email protected] manifests]# vim web.pp class nginx::web inherits nginx { file {'nginx.web.conf': ensure => file, source => "puppet:///modules/nginx/nginx.web.conf", path => '/etc/nginx/conf.d/default.conf', require => Package['nginx'], mode => '0644', } service {'nginx': ensure => true, enable => true, restart => '/etc/init.d/nginx reload', subscribe=> File['nginx.conf','nginx.web.conf'], } } [[email protected] manifests]# vim rproxy.pp class nginx::rproxy inherits nginx { file {'nginx.rproxy.conf': ensure => file, source => "puppet:///modules/nginx/nginx.rproxy.conf", path => '/etc/nginx/conf.d/default.conf', require => Package['nginx'], mode => '0644', } service {'nginx': ensure => true, enable => true, restart => '/etc/init.d/nginx reload', subscribe=> File['nginx.conf','nginx.rproxy.conf'], } } [[email protected] manifests]# rpm -e nginx [[email protected] manifests]# rm -rf /etc/nginx/ [[email protected] manifests]# puppet apply -e 'include nginx::web' notice: /Stage[main]/Nginx/Package[nginx]/ensure: created notice: /Stage[main]/Nginx::Web/File[nginx.web.conf]/content: content changed '{md5}d5ac7759792ba2917893f73a19cc085a' to '{md5}f5a613106188b7e187f58f376cc704b9' notice: /Stage[main]/Nginx/File[nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}09bd56f2a8067c5a9644380a682ff85f' notice: /Stage[main]/Nginx::Web/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: /Stage[main]/Nginx::Web/Service[nginx]: Triggered 'refresh' from 2 events notice: Finished catalog run in 13.97 seconds
查看配置是否生效:
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf server { listen 80 default_server; server_name www.example.com; #charset koi8-r; #access_log logs/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html; index index.html index.htm; } [[email protected] ~]# vim /etc/nginx/nginx.conf worker_processes 1; [[email protected] ~]# grep 'processor' /proc/cpuinfo processor: 0
注意:使用模板生成文件時,使用的文件屬性爲content。content => template('module_name/template_file_name')