puppet使用file資源能夠對文件進行管理,能夠對文件的內容、權限等管理。html
定義一個資源,須要指定資源的類型和資源的title。linux
file資源語法:
app
file { "/puppet/file/abc": name=> "/puppet/file/abc", content=>"test\n", owner=> root, mode=> 777; }
第一行:指定資源是file類型
ide
第二行:資源title,默認是與name的值同樣。this
執行後,會在/puppet/file目錄下面建立abc文件,內容:test(換行),屬於root用戶,文件權限:777spa
file類型:操作系統
http://docs.puppetlabs.com/references/latest/type.html#file
file { 'resource title': path => # (namevar) The path to the file to manage. Must be fully... ensure => # Whether the file should exist, and if so what... backup => # Whether (and how) file content should be backed... checksum => # The checksum type to use when determining... content => # The desired contents of a file, as a string... ctime => # A read-only state to check the file ctime. On... force => # Perform the file operation even if it will... group => # Which group should own the file. Argument can... ignore => # A parameter which omits action on files matching links => # How to handle links during file actions. During mode => # The desired permissions mode for the file, in... mtime => # A read-only state to check the file mtime. On... owner => # The user to whom the file should belong.... provider => # The specific backend to use for this `file... purge => # Whether unmanaged files should be purged. This... recurse => # Whether and how to do recursive file management. recurselimit => # How deeply to do recursive management. Values... replace => # Whether to replace a file or symlink that... selinux_ignore_defaults => # If this is set then Puppet will not ask SELinux... selrange => # What the SELinux range component of the context... selrole => # What the SELinux role component of the context... seltype => # What the SELinux type component of the context... seluser => # What the SELinux user component of the context... show_diff => # Whether to display differences when the file... source => # A source file, which will be copied into place... source_permissions => # Whether (and how) Puppet should copy owner... sourceselect => # Whether to copy all valid sources, or just the... target => # The target for creating a link. Currently... type => # A read-only state to check the file... validate_cmd => # A command for validating the file's syntax... validate_replacement => # The replacement string in a `validate_cmd` that... # ...plus any applicable metaparameters. }
ensure=>values present:文件不存在,會自動建立 absent:刪除存在的文件 directory:建立一個目錄 link:連接文件
(file資源的文件,須要前面的路徑是存在的,以下面的/puppet/file目錄是存在的)日誌
一、建立普通文件component
在puppet/file目錄建立文件名:1,內容爲當前操做系統名稱。orm
注:如果建立新文件,只要content有值,那麼能夠不須要ensure=>present。
file { "/puppet/file/1": ensure=>present, content => $operatingsystem, owner => root, mode => 644; }
二、建立目錄
file { "/puppet/file/mulu": ensure=>directory, mode=>644; }
三、建立連接
file { "/puppet/file/link": ensure=>link, target=>"/etc/passwd"; }
四、刪除文件
file { "/puppet/file/1": ensure=>absent, content => $operatingsystem, owner => root, mode => 644; }
客戶端能夠直接運行本地manifest,命令:puppet apply -l /var/log/puppet/a.log 1.pp
[root@pclient test]# puppet apply -l /var/log/puppet/a.log 1.pp Puppet apply 運行本地manifests /var/log/puppet/ 這個目錄是在/etc/puppet.conf配置 a.log自定義
執行後查看/var/log/puppet/a.log看到如下日誌
Mon Apr 14 15:54:58 +0800 2014 Puppet (notice): Compiled catalog for pclient.onepc.com in environment production in 0.12 seconds Mon Apr 14 15:54:59 +0800 2014 /Stage[main]/Main/File[/puppet/file/1]/ensure (notice): created Mon Apr 14 15:54:59 +0800 2014 /Stage[main]/Main/File[/puppet/file/link]/ensure (notice): created Mon Apr 14 15:54:59 +0800 2014 /Stage[main]/Main/File[/puppet/file/mulu]/ensure (notice): created Mon Apr 14 15:54:59 +0800 2014 Puppet (notice): Finished catalog run in 0.11 seconds
[root@pclient file]# ll 總用量 8 -rw-r--r-- 1 root root 6 4月 14 15:54 1 lrwxrwxrwx 1 root root 11 4月 14 15:54 link -> /etc/passwd drwxr-xr-x 2 root root 4096 4月 14 15:54 mulu [root@pclient file]#