http://my.oschina.net/fufangchun/blog/226098
puppet自動化運維之file資源
作用:
①.支援檔案和目錄的操作;
②.設定檔案及目錄的所有者及權限;
③.恢複檔案(包括檔案的内容、權限及所有者);
④.清理目錄以及子目錄。
格式:
file {"title": #一般是檔案名 ensure => present|absent|file|directory|link content => "content", #檔案内容(必須有,否則,檔案的内容為空) user => "username", #使用者 group => "groupname", #使用者組 mode => 權限, #四位八進制數 path => "title", #一般和title同名 source => "puppet:///URL", #指定到master上檔案的絕對路徑或agent上本地檔案絕對路徑 target => {"源檔案或目錄"} #指定目标檔案,用于ln -s $target $title recurse => true, #遞歸 purge => true, #将不再資源中管理的其他資料,删除 force => true; #如不加,則不會删除 } |
注:{},代表裡面的内容,為必選的。
詳解:
注:cp就使用source,echo就使用content。
content content => "字元串",
把檔案的内容(title或path)設定為content 參數後面的字元串, 新行,tab,空格可用 escaped syntax 表示(必須有,否則,檔案的内容為空,除非你不需要)。
ensure ensure => {absent|present|file|directory|link},
如果檔案本來不存在是否要建立title名的檔案:
present,檢查該檔案是否存在,如果不存在就建立title名的檔案,
absent, 檔案存在,就會删除title名的檔案(如果recurse => true ,就會删除目錄)。
file, 不存在就建立title名的檔案
directory, 不存在就建立title名的目錄
link,和target連用,指定目标檔案
group group => {gid|組名},
指定那個該檔案的使用者組,值可以是gid或者組名。
mode mode => {rwx},
mode用于設定檔案的權限(數字)r=4,w=2,x=1。
owner owner => {使用者名},
設定檔案的屬主。
path path => " 檔案的路徑",
指定要管理檔案的路徑,必須用引号引起來, 這也是一個資源的 namevar ,通常path 等于資源的title。
source source => {"puppet:///URL"|"完整的檔案路徑"},
以puppet:///開頭為從master下載下傳,正常路徑則在client本地讀取。
拷貝一個檔案覆寫目前檔案,用checksum來判斷是否有必要進行複制,可以設定的值是一個引用master或agent的完整的檔案路徑,或者是URI,目前支援的URI隻有puppet和file ; 這是一個對檔案常用的操作,可以讓puppet修改系統的配置檔案。
source => "puppet:///modules/ssh/etc/ssh/sshd_config" ,
source => "/etc/passwd",
注:"puppet://"等價于主配置檔案puppet.config中的modulepath值。
backup backup => {|.檔案名|false},
檔案的内容在被修改前是否進行備份. 利用filebucket對檔案進行備份,按檔案的md5sum進行歸類,便于恢複檔案的時候找到檔案.可以把檔案備份到 puppet 用戶端,也可以把檔案備份到網絡上的其他機器。
backup => "." puppet會把檔案備份在同一目錄下,備份檔案的擴充名就是 bakcup裡面的那個字元串。
backup => false 該檔案不做備份。
recurse recurse => { true|false|inf|remote},
設定是否以及如何進行遞歸操作,即可以管理子目錄(recurse,purge和force連用,用于删除子目錄中,不在資源控制的檔案或目錄)。
purge purge => {true|false},
将不再資源中管理的其他資料,則删除。
force force => {ture|false},
force是否可以删除檔案或目錄,與ignore相反。
ignore ignore => {檔案名|正規表達式},
當用recursion 方法複制一個目錄的時候,可以用ignore來設定過濾條件,符合過濾條件的檔案不被複制或删除. 使用ruby自帶的比對法則.是以shell級别的過濾表達式完全支援,例如[a-g]*, 與force相反。
target target => {"源檔案或目錄"},
是為建立連結檔案的,即将target的值,作為源檔案,title的值,作為目标檔案。如ln -s $target $title。當且僅當ensure => link, 才可以使用。
checksum checksum => {md5|mtime|time|timestamp},
檢查檔案是否被修改,這個狀态用來在複制檔案的時候使用, 這裡有幾種檢測方式,包括md5 ,mtime,time,timestamp等.預設的檢測是用md5。
links link => {follow|manage},
定義操作符合連結檔案.檔案拷貝的時候,
follow,會拷貝檔案的内容,而不是隻拷貝符合連結本身,
manage ,會拷貝符合連結本身。
recurselimit recurselimit => { 數字},
遞歸的深度,設定的值可以比對/^[0-9]+$/。
replace replace => {true|false},
是否覆寫已經存在的檔案。可以設定的值是(true,yes),(false,no)。
selrange 檔案内容是否屬于SElinux哪個組成部分,隻适于開啟了Selinux的機器。
selrole 檔案所屬的SeLinux 角色。
seltype 檔案所屬的Selinux type。
seluser 檔案所屬的Selinux user。
sourceselect 選擇拷貝目錄級别,預設,source是遞歸的。
type 檢查檔案是否隻讀。
注:綠色的,表示常用的;紫色的,表示不常用的。
file資源在puppet裡面用的挺多,屬性包括大家已經屬性的owner,group,mode,content等等。file還有兩個重要的指令,source和template。
通常,一個檔案的内容可以由content屬性來包含固定的内容,但是也可以用source指令來從其他url複制檔案内容。目前puppet隻支援puppet這個url,表示從puppet的fileserver去下載下傳檔案内容。例如:
source => "puppet://${fileserver}/lvs/${corp}.${idc}.keepalived.conf " |
其中fileserver後面的lvs表示是lvs子產品的files目錄這個路徑【/etc/puppet/module/lvs/files/】。正如前面提到的一樣。用source就可以把很多配置檔案放到puppet伺服器端統一管理。
template,可以通過erb模闆生成檔案内容,erb模闆可以使用變量。而且還可以對變量進行計算和操作。
這是puppet強大的地方,舉一個例子,你配置兩台squid伺服器,兩台伺服器的記憶體不一樣,那麼在squid.conf裡面有關記憶體的配置指令就要根據硬體配置來設定。在過去,你隻能手工去判定和修改,現在puppet自己搞定。看下面的代碼:
vi /etc/puppet/manifest/test.pp file {"/etc/squid/squid.conf": mode => 0644, content => template("squid/squid.conf.erb"); } |
這裡的template裡面的"squid/squid.conf.erb"表示的路徑是squid子產品下面templates目錄下的squid.conf.erb這個路徑。看看squid.conf.erb裡面的部分内容。
vi squid.conf.erb cache_mem <%= Integer(vmx_memsize.to_i*0.45) -%> MB visible_hostname <%= fqdn %> |
在這裡,cache_mem設定成總記憶體的45%大小,visible_hostname 設定成主機名。更多有趣的功能也可以實作。
在使用puppet過程中,有時有個類需要使用到多個file 資源. 而這些資源有相同的屬性,例如,使用者群組相同,權限相同,你還是每次都照樣寫嗎?這裡給大家提供一個小的技巧.其實這裡我們可以給file 設定預設的屬性。就不用重複寫了,大大地簡化我們的代碼.
例如:
vi /etc/puppet/manifest/test.pp File { ##這裡的大寫,表示預設屬性,下面的兩個檔案,屬主群組都會被設定為root且權限為644 ensure => "present", owner => "root", group => "root", mode => 644; } file { "/etc/cobbler/modules.conf": content => template("cobbler/modules.conf"), } file { "/etc/cobbler/dhcp.template": content => template("cobbler/dhcp.template"), } |
其實我們可以再優化下寫法,如下:
vi /etc/puppet/manifest/test.pp File { ensure => "present", owner => "root", group => "root", mode => 644, } file { "/etc/cobbler/modules.conf": content => template("cobbler/modules.conf"), "/etc/cobbler/dhcp.template": content => template("cobbler/dhcp.template"), } |
執行個體:
#軟連接配接 vi /etc/puppet/manifest/test.pp file {"/tmp/puppet_link.txt": ensure => link, target => "/etc/motd"; } # [root@client ~]# puppetd --test --server master.perofu.com info: Caching catalog for client.perofu.com info: Applying configuration version '1395062999' notice: /Stage[main]//File[/tmp/puppet_link.txt]/ensure: created notice: Finished catalog run in 0.07 seconds [root@client ~]# ll /tmp/ total 8 lrwxrwxrwx 1 root root 9 Mar 17 21:32 puppet_link.txt -> /etc/motd #目錄及管理 file {"/tmp/puppet_dir": ensure => directory, owner => root, group => root, recurse => true, #遞歸管理目錄 purge => true, #将不再目前file資源管理的,均删除 force => true, #可以删除檔案盒目錄 ignore => "dir*"; #正則,忽略dir*的 } # [root@client ~]# puppetd --test --server master.perofu.com info: Caching catalog for client.perofu.com info: Applying configuration version '1395063252' notice: /Stage[main]//File[/tmp/puppet_dir]/ensure: created notice: Finished catalog run in 0.14 seconds [root@client ~]# touch /tmp/puppet_dir/aaa [root@client ~]# touch /tmp/puppet_dir/dir1 [root@client ~]# [root@client ~]# puppetd --test --server master.perofu.com info: Caching catalog for client.perofu.com info: Applying configuration version '1395063252' info: FileBucket adding /tmp/puppet_dir/aaa as {md5}d41d8cd98f00b204e9800998ecf8427e info: /File[/tmp/puppet_dir/aaa]: Filebucketed /tmp/puppet_dir/aaa to puppet with sum d41d8cd98f00b204e9800998ecf8427e notice: /File[/tmp/puppet_dir/aaa]/ensure: removed notice: Finished catalog run in 0.12 seconds [root@client ~]# ll /tmp/puppet_dir/ total 0 -rw-r--r-- 1 root root 0 Mar 17 21:36 dir1 # source和template 後面會有詳解。 |
至此,puppet的file資源就結束了,接下來的是package資源的學習,請聽下回分解!!!