歡迎您光臨本站 註冊首頁

Puppet--文件自動化管理

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0

Puppet文件自動化管理

一.安裝后的目錄結構

安裝目錄默認存為/etc/puppet,該目錄下的manifests存放manifest文件.其他可執行文件在

/user/sbin下,主要有:

puppet 用於執行用戶所寫獨立的mainfests文件

puppetd 運行在被管理主機上的客戶端程序

puppetmasterd:運行在管理機上的伺服器程序

puppetcapuppet認證程序,主要用於對client的證書進行認證

puppetrun:用於連接客戶端,強制運行本地配置文件

file資源: 管理系統本地文件

設置文件許可權和屬主

管理文件內容,可以基於模板的內容管理

支持管理目錄

從遠程伺服器複製整個目錄到本地

參數

Backup:決定文件的內容在被修改前是否進行備份.

Checksum:檢查文件是否被修改

,這個狀態用來在複製文件的時候使用, 檢測方式包括md5 ,mtime等.默認的檢測是用md5

Content:把文件的內容設置為content 參數後面的字元串

Ensure

:如果文件本來不存在是否要新建文件,可以設置的值是 absentpresent,filedirectory. 如果指定 present,就會檢查該文件是否存在 ,如果不存在就新建該文件,如果指定是 absent, 就會刪除該文件(如果recurse => true ,就會刪除目錄),如果提供的值不是上面提到的 4,那麼就會創建一個文件的符號鏈接.

Force:作用是用在把一個目錄變成一個鏈接,可用的值是turefalse

Group

:指定那個該文件的用戶組,值可以是gid或者組名

Ignore:當用recursion 方法複製一個目錄的時候,可以用

ignore來設定過濾條件,符合過濾條件的文件不被複制.

Links:定義操作符合鏈接文件. 可以設置的值是followmanage; 文件拷貝的時候

,設置follow,會拷貝文件的內容,而不是只拷貝符合鏈接本身,如果設置成manage ,會拷貝符合鏈接本身.

Mode

mode用於設置文件的許可權

Owner:設置文件的屬主

Path:指定要管理文件的路徑,用引號引起來, 這也是一個資源的 namevar ,

通常path 等於資源的title

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

Recurselimit:遞歸的深度,設置的值可以匹配

/^[0-9] $/.

Source:拷貝一個文件覆蓋當前文件,checksum來判斷是否有必要進行複製,可以設置的值是一個引用的完整的文件路徑,或者是URI,當前支持的

URI只有puppetfile ;

File資源測試用例

登陸puppet master,編輯默認manifest文件

site.pp
  1. [root@master ~]# cd /etc/puppet/manifests/
  2. [root@master manifests]# vim site.pp
  3. node default {
  4. file {'file1':
  5. path => '/tmp/file1',
  6. ensure => present,
  7. mode => 0777,
  8. user => root,
  9. group => root,
  10. content => "Welcome to use Puppet.",
  11. }
  12. }

puppet master應用該測試文件

  1. [root@master manifests]# puppet apply file1.pp
  2. notice: /Stage[main]//File[file1]/ensure: created
  3. notice: Finished catalog run in 0.04 seconds

檢查puppet master上是否生效

  1. [root@master manifests]# more /tmp/file1
  2. Welcome to use Puppet.
  3. [root@master manifests]# ll /tmp/file1
  4. -rwxrwxrwx 1 root root 22 222 15:13 /tmp/file1

用其中一個客戶端測試
  1. [root@client1 puppet]# puppetd --server master.puppet --test
  2. notice: Ignoring --listen on onetime run
  3. info: Caching catalog for client1.centos
  4. info: Applying configuration version '1329902782'
  5. notice: /Stage[main]//Node[default]/File[file1]/ensure: created
  6. notice: Finished catalog run in 0.01 seconds

查看客戶端文件屬性

  1. [root@client1 puppet]# ls -l /tmp/file1
  2. -rwxrwxrwx 1 root root 22 222 17:26 /tmp/file1
  3. [root@client1 puppet]# more /tmp/file1
  4. Welcome to use Puppet.

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

  1. [root@client3 ~]# puppetd --test --server master.puppet
  2. err: Could not retrieve catalog from remote server: Error 400 on SERVER: No support for http method POST
  3. warning: Not using cache on failed catalog
  4. err: Could not retrieve catalog; skipping run
  5. notice: /File[/var/puppet/state/last_run_summary.yaml]/content:
  6. --- /var/puppet/state/last_run_summary.yaml 2012-02-23 09:13:50.000000000 0800
  7. /tmp/puppet-file20120223-4772-1la6w5s-0 2012-02-23 09:14:59.000000000 0800
  8. @@ -3,4 3,4 @@
  9. puppet: 2.7.10
  10. config:
  11. time:
  12. - last_run: 1329959630
  13. \ No newline at end of file
  14. last_run: 1329959699
  15. \ No newline at end of file

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

  1. [root@master ~]# puppetmasterd --version
  2. 2.6.13

安裝2.6.13的客戶端恢復

  1. [root@client3 ~]# fetch http://puppetlabs.com/downloads/puppet/puppet-2.6.13.tar.gz
  2. [root@client3 ~]# tar -zxvf puppet-2.6.13.tar.gz
  3. [root@client3 ~]# cd puppet-2.6.13
  4. [root@client3 ~/puppet-2.6.13]# ruby install.rb
  5. [root@client3 ~]# puppetd --version
  6. 2.6.13

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

  1. [root@client3 /var/lib/puppet]# puppetd --test --server master.puppet
  2. err: Could not request certificate: Retrieved certificate does not match private key; please remove certificate from server and regenerate it with the current key
  3. Exiting; failed to retrieve certificate and waitforcert is disabled

清楚伺服器端的證書,重新認證,如仍然報錯可以將客戶端的

ssl正式也刪除
  1. [root@master ssl]# puppetca --clean client3.freebsd
  2. notice: Revoked certificate with serial 7
  3. notice: Removing file Puppet::SSL::Certificate client3.freebsd at '/var/lib/puppet/ssl/ca/signed/client3.freebsd.pem'
  4. notice: Removing file Puppet::SSL::Certificate client3.freebsd at '/var/lib/puppet/ssl/certs/client3.freebsd.pem'
  1. [root@client3 ~]# cd /etc/puppet/
  2. [root@client3 /etc/puppet]# mv ssl ssl_bak

重新認證,客戶端恢復

  1. [root@client3 ~]# puppetd --test --server master.puppet
  2. info: Caching catalog for client3.freebsd
  3. info: Applying configuration version '1329961887'
  4. notice: Finished catalog run in 0.02 seconds

測試file資源的其他參數

  1. [root@master ~]# vim /etc/puppet/manifests/site.pp
  2. node default {
  3. file {'file1':
  4. path => '/tmp/file1',
  5. ensure => present,
  6. mode => 0777,
  7. #user => root,
  8. #group => root,
  9. content => "Welcome to use Puppet.",
  10. }
  11. file{ '/tmp/file2':
  12. ensure => directory,
  13. mode=>0644,
  14. }
  15. file{'/tmp/file3':
  16. ensure=>link,
  17. target=>'/tmp/file1',
  18. }
  19. }

客戶端測試配置

  1. [root@client1 ~]# puppetd --server master.puppet --test
  2. notice: Ignoring --listen on onetime run
  3. info: Caching catalog for client1.centos
  4. info: Applying configuration version '1329963133'
  5. notice: /Stage[main]//Node[default]/File[/tmp/file2]/ensure: created
  6. notice: /Stage[main]//Node[default]/File[/tmp/file3]/ensure: created
  7. notice: Finished catalog run in 0.03 seconds

查看生成文件信息

  1. [root@client1 ~]# ls -l /tmp/file*
  2. -rwxrwxrwx 1 root root 22 222 17:26 /tmp/file1
  3. lrwxrwxrwx 1 root root 10 223 10:12 /tmp/file3 -> /tmp/file1
  4. /tmp/file2:
  5. 總用量 0

本文出自 「Waydee的博客」 博客,請務必保留此出處http://waydee.blog.51cto.com/4677242/847118


[火星人 ] Puppet--文件自動化管理已經有379次圍觀

http://coctec.com/docs/linux/show-post-45806.html