Puppet常用資源:
常用的資源主要有以下幾個:
file:主要負責管理檔案
package:軟體包的安裝管理
service:系統服務的管理
cron:配置自動任務計劃
exec:遠端執行運作指令
【驗證puppet配置】
1、file資源
在服務端寫個例子測試一下。這個例子作用很簡單,用來在用戶端的/tmp目錄下建立一個 test.txt 檔案,内容為:hello,test!在服務端編寫代碼:【伺服器端不需要建立這個檔案】
vi /etc/puppet/manifests/site.pp
file {
“/tmp/test.txt”:
content => “helo,test!”;
}
在用戶端上執行 puppet,運作成功後會在 /tmp 看到新生成的 test.txt:
[root@slave9 tmp]# puppet agent --test --server=master
info: Caching catalog for slave9
info: Applying configuration version '1373006825'
notice: /Stage[main]//Node[default]/File[/tmp/test.txt]/ensure: defined content as '{md5}002c9dbca1fe0e6339dc7a2cbb6f776d'
notice: Finished catalog run in 0.02 seconds
最後檢視cat /tmp/test.txt
hello,test!
2、Package資源:
【package資源】遠端安裝服務
package {
["ntp"]:
ensure => "installed";
"screen":
ensure => "absent";
}
定義的意思是yum install ntp服務,并且解除安裝screen安裝包。
用戶端執行更新:
info: Applying configuration version '1373268238'
notice: /Stage[main]//Package[screen]/ensure: removed
notice: /Stage[main]//Package[ntp]/ensure: created
notice: Finished catalog run in 33.36 seconds
【service資源】 針對服務的重新開機
service {
"sshd":
ensure => running;
"nfs":
ensure => stopped;
}
意思是定義啟動sshd服務,停止nfs服務。
【Cron資源】
cron{
"ntpdate":
command => "/usr/sbin/ntpdate pool.ntp.org",
user => root,
hour => 0,
minute => 0,
意思是在用戶端寫入一個計劃任務:0 0 * * * /usr/sbin/ntpdate pool.ntp.org 自動同步時間!
info: Applying configuration version '1373269123'
notice: /Stage[main]//Cron[ntpdate]/ensure: created
notice: Finished catalog run in 0.17 seconds
【其他】
1、每隔3小時執行 hour => "*/3",
2、如果用戶端的crond是這樣定義:19 0 * * *,想要改成19 * * * *
hour => "*",
【puppet推送】
向用戶端推送本地腳本:
首先修改vi /etc/puppet/fileserver.conf 檔案,添加如下三行:
[share] --子產品名字
path /etc/puppet/files
allow *
##注意files後不能有空格,否則重新開機puppetmaster的時候會報錯,我不小心在這折騰了很久(╯﹏╰)
然後建立/etc/puppet/files這個目錄,然後cp所需要的東西到files目錄
file {
"/tmp/nginx_install.sh": --nginx_install.sh腳本會拷貝到/tmp目錄下
source => "puppet:///share/nginx_install.sh",
group => root,
owner => root,
mode => "755"
}
需要注意幾個地方,puppet:///share/nginx_install.sh,而不應該寫puppet://master/files/nginx_install.sh
recurse => "true", ##傳送該目錄下所有檔案 【需要測試】
意思是把 /etc/puppet/files/nginx_install.sh這個腳本推送到用戶端的/tmp/下!
http://kexl908.blog.51cto.com/605006/901767
【注意】
1、可以把多個file資源的title寫在一起,前提是file資源在用戶端必須先存在
["/tmp/test.txt", "/tmp/test1", "/tmp/test2.txt"]:
owner => "test",
group => "test",
mode => 777,
【遠端執行指令】
如第五步,我們把nginx_install.sh推送過去後,這時候我們就可以執行了如下:
exec {
"/tmp/1.sh":
cwd => "/tmp",
command => "sh /tmp/1.sh",
user => root,
path => ["/usr/bin","/usr/sbin","/bin","/bin/sh"],
【配置檔案更新】
例如更新/etc/sysctl.conf這個配置檔案,先在主上,拷貝/etc/sysctl.conf到/etc/puppet/files,然後對核心配置檔案進行修改,
然後修改site.php
file { "/etc/sysctl.conf":
source => "puppet:///share/sysctl.conf",
owner => "root",
group => "root",
mode => 644,
}
接着在從上面直接更新就行了
最後要讓修改好的配置檔案生效,還需要如下操作
"sysctl refresh kernel config":
path => ["/usr/bin", "/usr/sbin", "/bin", "/sbin"],
command => "/sbin/sysctl -p",
subscribe => File["/etc/sysctl.conf"],
refreshonly => true
}
需要注意:他們之間不能用冒号,兩邊的框框不能去掉
path => ["/usr/bin", "/usr/sbin", "/bin", "/sbin"],
這2個必須要寫在一起,不能把exec單獨放
【資源依賴】 --A依賴B,如果A沒有的話,則B不會被執行
【資源依賴】
資源A依賴資源B,如果資源B不存在,資源A就不被執行。定義資源依賴的屬性是require。
class base::cron
{
package { "crontabs":
ensure => installed,
}
service {
"crond":
ensure => running,
enable => true,
require => Package["crontabs"];
cron { ntpdate:
command => "/usr/sbin/ntpdate time.nist.gov 210.72.145.44 64.147.116.229 ",
user => root,
hour => 0,
minute => 0,
require => Package["crontabs"];
前提:cron已經被安裝,否則後面的都不執行