#vi /etc/puppet/fileserver.conf puppetmasterd自帶一個簡易的文件服務器,配置方法是編輯/etc/puppet/fileserver.conf,增長一個配置片斷myfiles ------------------------------------ [myfiles] path /opt/ // 設置待推送的文件/文件夾相對模塊根目錄,結合後面的體會含義 allow 192.168.1.0/24 // 設置准許鏈接到服務端的主機地址
#vi /etc/puppet/manifests/site.pp ------------------------------------- file { "/bbb/redis-2.4.tar.gz": //客戶端將要生成的拷貝文件名稱 source => "puppet://server.puppet/myfiles/redis-2.4.tar.gz" /* source字段爲服務器端一個被傳送文件/文件夾, puppet.server爲puppet服務器名稱,即hostname,生產環境下用內部的DNS上做解析, myfiles爲上步中/etc/puppet/fileserver.conf文件中配置的module名稱,每一個module都有單獨的path和allow等屬性,這裏的myfiles映射到path爲/opt/的文件系統實際路徑, redis-2.4.tar.gz爲服務器端文件系統中一個相對於module中path的文件實際路徑。 綜上:這段配置實際含義是把服務端/opt/redis-2.4.tar.gz文件傳到客戶端/bbb/redis-2.4.tar.gz。 */ }
#puppetd --test --server server.puppet
若是要分發文件夾,做以下配置 html
#vi /etc/puppet/manifests/site.pp ------------------------------------- file { "/bbb/redisdir": //客戶端將要生成的拷貝文件目錄名稱 source => "puppet://server.puppet/myfiles/redisdir", recurse=>true, //遞歸拷貝 ensure=>directory, //確保客戶端/bbb/redisdir目錄存在 force=>true //強制刪除或覆蓋已存在目錄 }
#vi /etc/puppet/manifests/site.pp ------------------------------------------------------ file { "/tmp/dd142/puppet-2.6.13.tar.gz": owner => "puppet", group => "puppet", mode => 666, }
#puppetd --test --server server.puppet
vi /etc/puppet/manifests/site.pp --------------------------------------------- exec { "exec-mkdir": cwd => "/opt", //切換到命令行運行時目錄 command => "sh /opt/lgh.sh", user => "root", path => "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin", }
vi /opt/lgh.sh --------------------------------------------- #!/bin/bash mkdir /opt/shelldir
# puppetd --test --server server.puppet
在/opt目錄下查看shelldir目錄有沒有創建。 ios
簡單說來exec資源就是在執行puppet的時 候,調用shell執行一條shell語句。經過exec可執行程序,還能夠設置執行時的path,環境變量,user/group,是否記錄output等,很方便,例如: git
exec { 「/root/puppet/nagiosclient/nrpe.sh」:
cwd => 「/root/puppet」,
timeout => 7200,
logoutput => on_failure,
user => root,
path => ["/sbin", "/usr/sbin", "/usr/local/sbin", "/root/bin", "/usr/local/bin", "/usr/bin", "/bin", "/usr/lib64/jvm/jre/bin"],
require => File["/root/puppet/nagiosclient/nrpe.sh"]
} github
想在各客戶機上執行時,我通常就是先用上述file資源的配置把該腳本推送到客戶機上,而後用空上exec資源去執行它,注意exec中要配置好require 該file,不然沒法保證exec在file之類在客戶上執行。 redis
有人說,「exec資源在不到萬不得已的時候不要去用,若是能用腳本實現,儘可能寫成腳本經過file資源分發到服務器上面。而後用其餘的方式來調用腳本。例如crontab。雖然puppet提供了crontab資源,可是你徹底能夠用file資源來把 crontab任務放到 /etc/ cron.d目錄下來實現crontab資源的管理。使用puppet的時候,儘可能用最簡單的語法,越是花哨的語法也越容易出錯。」 shell
vi /etc/puppet/manifests/site.pp --------------------------------------------- cron { "cron-shell": #title部分,可用來做爲註釋。 command => "sh /opt/lgh.sh" #要執行的命令 user => "root", #添加到root用戶下的crontab中 minute => "30", #即第一個星號 hour => "17" #即第二個星號 }
[root@client ~]# puppetd --test --server server.puppet
而後在客戶端使用#crontab -l查看效果 bash
[root@client ~]# crontab -l # HEADER: This file was autogenerated at Mon Nov 04 16:56:25 +0800 2013 by puppet. # HEADER: While it can still be managed manually, it is definitely not recommended. # HEADER: Note particularly that the comments starting with 'Puppet Name' should # HEADER: not be deleted, as doing so could cause duplicate cron jobs. # Puppet Name: cron-shell 30 17 * * * sh /opt/lgh.sh
#vi /etc/puppet/manifests/site.pp ---------------------------------------------- service { iptables: ensure => "running", }
#puppetd --test --server server.puppet
http://blog.loudly.me/tag/puppet/ 服務器