puppet使用file资源可以对文件进行管理,可以对文件的内容、权限等管理。

定义一个资源,需要指定资源的类型和资源的title。

file资源语法:

file {        "/puppet/file/abc":        name=> "/puppet/file/abc",        content=>"test\n",        owner=> root,        mode=> 777;}

第一行:指定资源是file类型

第二行:资源title,默认是与name的值一样。

执行后,会在/puppet/file目录下面创建abc文件,内容:test(换行),属于root用户,文件权限:777

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=>valuespresent:文件不存在,会自动创建absent:删除存在的文件directory:创建一个目录link:链接文件

(file资源的文件,需要前面的路径是存在的,如下面的/puppet/file目录是存在的)

1、创建普通文件

在puppet/file目录创建文件名:1,内容为当前操作系统名称。

注:若是创建新文件,只要content有值,那么可以不需要ensure=>present。

file {        "/puppet/file/1":        ensure=>present,        content => $operatingsystem,        owner => root,        mode => 644;}

2、创建目录

file {        "/puppet/file/mulu":        ensure=>directory,        mode=>644;}

3、创建链接

file {        "/puppet/file/link":        ensure=>link,        target=>"/etc/passwd";}

4、删除文件

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.ppPuppet 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 secondsMon Apr 14 15:54:59 +0800 2014 /Stage[main]/Main/File[/puppet/file/1]/ensure (notice): createdMon Apr 14 15:54:59 +0800 2014 /Stage[main]/Main/File[/puppet/file/link]/ensure (notice): createdMon Apr 14 15:54:59 +0800 2014 /Stage[main]/Main/File[/puppet/file/mulu]/ensure (notice): createdMon 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 1lrwxrwxrwx 1 root root   11  4月 14 15:54 link -> /etc/passwddrwxr-xr-x 2 root root 4096  4月 14 15:54 mulu[root@pclient file]#