jhfgds 发表于 2017-10-11 09:24:06

Puppet File资源介绍(贰拾)

puppet file资源常用的参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
file {
    path
    ensure
    backup
    checksum
    content
    force
    group
    links
    mode
    owner
    source
    selinux_ignore_default
    selrange
    selrole
    seltype
    seluser
}






path:指定要管理的文件或目录,必须用引号引起来.


ensure:有5个值,分别是absent,present,file,directory,link.设置present值表示【匹配文件,它会检查path值中的路径文件是否存在,如果不存在就会创建;设置absent值,删除已经存在的文件;设置directory表示创建目录,但是要删除目录需增加参数force=>true,设置link时会根据path路径创建软连接文件.

示例:
删除目录

1
2
3
4
5
6
7
8
# ls /tmp/
test
# puppet apply dire.pp
Notice: Compiled catalog for sh-web1.localdomain in environment production in 0.06 seconds
Notice: /Stage/Main/File/ensure: removed
Notice: Finished catalog run in 0.05 seconds
# ls /tmp/
#






backup:文件的内容在修改前是否备份,目前puppet支持两种备份方式,一种是将文件备份在agent上被修改文件的目录中,另一种方式将资源文件通过filebucket备份在远程服务器上。备份在agent上的方式,buckup属性的值如果是以".bak"开头的字符串的话,puppet会将变更文件备份在agent资源文件的同一目录下,备份文件的扩展名就是"."值里面的字符串,另一种远程备份需要借助filebucket资源.

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
file {"/tmp/test":
    ensure => present,
    content=> "this is file.",
    backup => ".$backup_date.bak",
}
file {"/tmp/test":
    ensure => present,
    content=> "this is test.",
    backup => ".$backup_date.bak",
}
# puppet apply file2.pp
Notice: Compiled catalog for sh-web1.localdomain in environment production in 0.07 seconds
Notice: /Stage/Main/File/content: content changed '{md5}70b7ea41998bea7dc5be44528ae37ba3' to '{md5}480fc0d368462326386da7bb8ed56ad7'
Notice: Finished catalog run in 0.03 seconds
# ls /tmp/
testtest..bak
# cat /tmp/test..bak
this is file.





注意:agent端主动备份会在file当前目录下备份.

checksum:检查文件内容是否被修改过,通过它可以检查文件的一致性。包括:md5,mtime,ctime默认使用md5.

content:可以向文件中追加内容或者通过调用template函数向erb模板中追加内容.

force:可以将一个目录变成一个连接,可用的值是true、false、yes和no,其中true与yes参数在这里均表示创建目录连接,false与no参数均表示不创建目录连接.

group:可以指定该文件的用户组,值可以使gid或系统组名.

links:定义操作符合连接的文件,可以设置的值是follow和manage.设置follow值文件复制时,会赋值文件的内容,而不是只复制连接本身;如果设置为manage值,则只复制符合连接本身.

mode:用于设置文件的权限.

owner:文件的属主.

source:指定源文件的位置,值可以是指定的远程文件的uris或者本地完整路径.

示例:
通过source同步puppet数组中多个文件.

1
2
3
4
5
6
7
file {
    source => [
      "puppet:///modules/nfs/conf.$host",
      "puppet:///modules/nfs/conf.$operatingsystem",
      "puppet:///modules/nfs/conf",
    ]
}






target:指定创建软连接的目标.

1
2
3
4
5
6
7
8
9
10
11
# cat link.pp
file {"/tmp/3.pp":
    ensure=> link,
    target=> '/root/3.pp',
}
# puppet apply link.pp
Notice: Compiled catalog for sh-web1.localdomain in environment production in 0.06 seconds
Notice: /Stage/Main/File/ensure: created
Notice: Finished catalog run in 0.03 seconds
# ls /tmp/
3.pptesttest..bak






selinux_ignore_default:selinux系列功能,实现自定义selinux.
selrange:selinux系列功能,定义范围.
selrole:selinux系列功能,定义角色.
seltype:selinux系列功能,定义类型.
seluser:selinux系列功能,定义用户.

filebucket资源示例:
filebucket资源主要用于文件的备份与恢复,通常与file资源配合使用.

1
2
3
4
5
6
filebucket {'':
    name
    path
    port
    server
}





name:filebucket的名字.
path:服务器备份数据路径.
port:备份服务器的端口.
server:备份服务的域名.

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
node base {
    include admin
    include cron
    filebucket {'main':
      server => 'puppet',
      path   => '/var/lib/puppet/clientbucket',
}
    file {"/tmp/test":
      ensure => present,
      content=> "this is lisi.",
      backup => "main",
    }
}
node /sh-(proxy|web)\d+/inherits base {
    case $::hostname {
      /sh-proxy\d+/: {
      tag ("web::proxy")
             include php
          }
         "sh-web1": {
            include php
            }
      }
}






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1507549594'
Notice: /Stage/Admin/Exec/returns: executed successfully
Notice: /Stage/Main/Node/File/content:
--- /tmp/test2017-10-09 19:02:15.527825330 +0800
+++ /tmp/puppet-file20171009-5104-6zk5y1-02017-10-09 19:46:34.951821641 +0800
@@ -1 +1 @@
-this is test.
\ No newline at end of file
+this is lisi.
\ No newline at end of file
Info: Computing checksum on file /tmp/test
Info: /Stage/Main/Node/File: Filebucketed /tmp/test to main with sum 480fc0d368462326386da7bb8ed56ad7
Notice: /Stage/Main/Node/File/content: content changed '{md5}480fc0d368462326386da7bb8ed56ad7' to '{md5}b58ff837e1152bf6d13212d1860c1219'
Notice: Finished catalog run in 0.39 seconds




注意:当资源改变时agent更新就会提示备份.

puppet 3.8的版本发现个问题:
master端并未备份到puppet 代码指定的位置:

1
2
3
4
5
6
7
8
9
10
11
12
# tree
.
└── 8
    └── d
      └── 6
            └── 1
                └── 0
                  └── f
                        └── 1e8d610ffbe27bf880c7d734386dbde1
                            ├── contents
                            └── paths
7 directories, 2 files





1
2
# pwd
/var/lib/puppet/bucket/1/e




客户端备份到了path的路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
# tree
.
└── 8
    └── 6
      └── f
            └── 3
                └── 1
                  └── 9
                        └── 2
                            └── 186f319242818f98380d6369593bfb47
                              ├── contents
                              └── paths
8 directories, 2 files





1
2
# pwd
/var/lib/puppet/clientbucket/1



页: [1]
查看完整版本: Puppet File资源介绍(贰拾)