天天看點

Puppet--檔案自動化管理 Puppet檔案自動化管理

安裝目錄預設存為/etc/puppet,該目錄下的manifests存放manifest檔案。其他可執行檔案在/user/sbin下,主要有:

puppet: 用于執行使用者所寫獨立的mainfests檔案

puppetd: 運作在被管理主機上的用戶端程式

puppetmasterd:運作在管理機上的伺服器程式

puppetca:puppet認證程式,主要用于對client的證書進行認證

puppetrun:用于連接配接用戶端,強制運作本地配置檔案

<b>file</b><b>資源:</b>  管理系統本地檔案

設定檔案權限和屬主

管理檔案内容,可以基于模闆的内容管理

支援管理目錄

從遠端伺服器複制整個目錄到本地

<b>參數</b><b></b>

<b></b>

Backup:決定檔案的内容在被修改前是否進行備份.

Checksum:檢查檔案是否被修改,這個狀态用來在複制檔案的時候使用, 檢測方式包括md5 ,mtime等。預設的檢測是用md5

Content:把檔案的内容設定為content 參數後面的字元串

Ensure:如果檔案本來不存在是否要建立檔案,可以設定的值是 absent和present,file和directory. 如果指定 present,就會檢查該檔案是否存在,如果不存在就建立該檔案,如果指定是 absent, 就會删除該檔案(如果recurse =&gt; true ,就會删除目錄),如果提供的值不是上面提到的4種,那麼就會建立一個檔案的符号連結.

Force:作用是用在把一個目錄變成一個連結,可用的值是ture和false

Group:指定那個該檔案的使用者組,值可以是gid或者組名

    Ignore:當用recursion 方法複制一個目錄的時候,可以用ignore來設定過濾條件,符合過濾條件的檔案不被複制.

Links:定義操作符合連結檔案. 可以設定的值是follow和manage; 檔案拷貝的時候,設定follow,會拷貝檔案的内容,而不是隻拷貝符合連結本身,如果設定成manage ,會拷貝符合連結本身.

Mode:mode用于設定檔案的權限

Owner:設定檔案的屬主

Path:指定要管理檔案的路徑,必須用引号引起來, 這也是一個資源的 namevar ,通常path 等于資源的title

Recurse:設定是否以及如何進行遞歸操作, 可以設定的值是 false,ture ,inf ,remote

Recurselimit:遞歸的深度,設定的值可以比對/^[0-9]+$/.

Source:拷貝一個檔案覆寫目前檔案,用checksum來判斷是否有必要進行複制,可以設定的值是一個引用的完整的檔案路徑,或者是URI,目前支援的URI隻有puppet和file ;

<b>File</b><b>資源測試用例</b><b></b>

登陸puppet master,編輯預設manifest檔案site.pp

[root@master ~]# cd /etc/puppet/manifests/ 

[root@master manifests]# vim site.pp 

 node default { 

          file {'file1': 

              path =&gt; '/tmp/file1', 

              ensure =&gt; present, 

              mode =&gt; 0777, 

              user =&gt; root, 

              group =&gt; root, 

              content =&gt; "Welcome to use Puppet.", 

         } 

     } 

在puppet master應用該測試檔案

[root@master manifests]# puppet apply file1.pp 

notice: /Stage[main]//File[file1]/ensure: created 

notice: Finished catalog run in 0.04 seconds 

檢查puppet master上是否生效

[root@master manifests]# more /tmp/file1 

Welcome to use Puppet. 

[root@master manifests]# ll /tmp/file1 

-rwxrwxrwx 1 root root 22  2月 22 15:13 /tmp/file1 

用其中一個用戶端測試

[root@client1 puppet]# puppetd   --server  master.puppet --test 

notice: Ignoring --listen on onetime run 

info: Caching catalog for client1.centos 

info: Applying configuration version '1329902782' 

notice: /Stage[main]//Node[default]/File[file1]/ensure: created 

notice: Finished catalog run in 0.01 seconds 

       檢視用戶端檔案屬性

[root@client1 puppet]# ls -l /tmp/file1 

-rwxrwxrwx 1 root root 22  2月 22 17:26 /tmp/file1 

[root@client1 puppet]# more /tmp/file1 

出現的問題1,報錯如下:

[root@client3 ~]# puppetd  --test --server master.puppet 

err: Could not retrieve catalog from remote server: Error 400 on SERVER: No support for http method POST 

warning: Not using cache on failed catalog 

err: Could not retrieve catalog; skipping run 

notice: /File[/var/puppet/state/last_run_summary.yaml]/content: 

--- /var/puppet/state/last_run_summary.yaml     2012-02-23 09:13:50.000000000 +0800 

+++ /tmp/puppet-file20120223-4772-1la6w5s-0     2012-02-23 09:14:59.000000000 +0800 

@@ -3,4 +3,4 @@ 

     puppet: 2.7.10 

     config: 

   time: 

-    last_run: 1329959630 

\ No newline at end of file 

+    last_run: 1329959699 

出現這個報錯的原因是因為puppet的用戶端版本和伺服器的版本不一樣導緻的,puppet master的版本是2.6.13,而puppet client的版本是2.7.10下面是檢查版本:

[root@master ~]# puppetmasterd --version 

2.6.13 

安裝2.6.13的用戶端恢複

[root@client3 ~]# fetch http://puppetlabs.com/downloads/puppet/puppet-2.6.13.tar.gz 

[root@client3 ~]# tar -zxvf puppet-2.6.13.tar.gz 

[root@client3 ~]# cd puppet-2.6.13 

[root@client3 ~/puppet-2.6.13]# ruby  install.rb 

[root@client3 ~]# puppetd --version 

       出現的問題2,證書問題,報錯如下

[root@client3 /var/lib/puppet]# puppetd  --test  --server  master.puppet 

err: Could not request certificate: Retrieved certificate does not match private key; please remove certificate from server and regenerate it with the current key 

Exiting; failed to retrieve certificate and waitforcert is disabled 

        清楚伺服器端的證書,重新認證,如仍然報錯可以将用戶端的ssl正式也删除

[root@master ssl]# puppetca --clean  client3.freebsd 

notice: Revoked certificate with serial 7 

notice: Removing file Puppet::SSL::Certificate client3.freebsd at '/var/lib/puppet/ssl/ca/signed/client3.freebsd.pem' 

notice: Removing file Puppet::SSL::Certificate client3.freebsd at '/var/lib/puppet/ssl/certs/client3.freebsd.pem' 

[root@client3 ~]# cd /etc/puppet/ 

[root@client3 /etc/puppet]# mv ssl  ssl_bak 

   重新認證,用戶端恢複

[root@client3 ~]# puppetd  --test  --server  master.puppet        

info: Caching catalog for client3.freebsd 

info: Applying configuration version '1329961887' 

notice: Finished catalog run in 0.02 seconds 

       <b>測試</b><b>file</b><b>資源的其他參數</b><b></b>

[root@master ~]# vim /etc/puppet/manifests/site.pp 

node default { 

              #user =&gt; root, 

              #group =&gt; root, 

          } 

          file{ '/tmp/file2': 

              ensure =&gt; directory, 

              mode=&gt;0644, 

          file{'/tmp/file3': 

              ensure=&gt;link, 

              target=&gt;'/tmp/file1', 

      用戶端測試配置

[root@client1 ~]# puppetd   --server  master.puppet --test 

info: Applying configuration version '1329963133' 

notice: /Stage[main]//Node[default]/File[/tmp/file2]/ensure: created 

notice: /Stage[main]//Node[default]/File[/tmp/file3]/ensure: created 

notice: Finished catalog run in 0.03 seconds 

       檢視生成檔案資訊

[root@client1 ~]# ls -l /tmp/file* 

-rwxrwxrwx 1 root root   22  2月 22 17:26 /tmp/file1 

lrwxrwxrwx 1 root root   10  2月 23 10:12 /tmp/file3 -&gt; /tmp/file1 

/tmp/file2: 

總用量 0 

本文轉自 waydee 51CTO部落格,原文連結:http://blog.51cto.com/waydee/847118,如需轉載請自行聯系原作者

繼續閱讀