博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
puppet之文件管理
阅读量:7105 次
发布时间:2019-06-28

本文共 3348 字,大约阅读时间需要 11 分钟。

本文系统Centos6.0 puppet 2.6.18

先看下file资源的部分属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
file {“absolute path”:
  
ensure=> present/absent/file/directory/link,
               
#定义absolute path的是否存在在及它应该是什么角色
  
user=>
"username"
,
  
group=>
"groupname"
,
  
mode=>
0777
,
  
path=>
"absolute path"
,
  
source=>
"puppet:///URL"
,
              
#指定到master上文件的路径或者agent上本地文件路径
  
content=>
"content"
,
           
#描述文件内容的字符串,也可以是模版或者输出内容的函数,模版常用
  
target=>    #link文件源
  
recurse=>   #递归处理
  
purge=>     #清理文件及目录
  
force=>    #必须使用此选项,否则不能清理
  
ignore =>     #不清理的文件
  
backup  => 决定文件的内容在被修改前是否进行备份. 利用filebucket对文件进行备份,按文件的md5sum进行归类,便于恢复文件的时候找到文件.可以把文件备份到 puppet 客户端,也可以通过设置backpup => bucket_name 把文件备份到网络上的其他机器. 如果backup的值是一个点号”.”开头的字符串,puppet会把文件备份在同一目录下,备份文件的扩展名就是 bakcup里面的那个字符串.如果设置 backup => 
false 
, 该文件不做备份.
checksum  =>  怎样检查文件是否被修改,这个状态用来在复制文件的时候使用, 这里有几种检测方式,包括md5 ,mtime,time,timestamp等.
默认的检测是用md5
 

主机信息:

master:master.lansgg.com 192.168.182.143

client1 :client1.lansgg.com 192.168.182.142

client2: client2.lansgg.com 192.168.182.149

相关安装请看

案例一:

目的:在client1上/tmp/下创建文件lansgg.txt

在client2上/root/下创建目录testdir

master:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@master ~]# cat /etc/puppet/manifests/site.pp
import 
"node1.pp"
[root@master ~]# cat /etc/puppet/manifests/node1.pp
node 
'client1.lansgg.com'
{
file {
"/tmp/lansgg.txt"
:
    
ensure => present,
    
owner => root,
    
group => root,
    
mode => 
0644
,
    
content => " # path /path/to/resource\n
            
# [environment envlist]\n
            
# [method methodlist]\n
            
# [auth[enthicated] {yes|no|on|off|any}]\n
            
# allow [host|ip|*]\n
            
# deny [host|ip]\n";
    
}
}
node 
'client2.lansgg.com'
{
file { 
"/root/testdir"
:
    
ensure => directory,
    
owner => nobody,
    
group => nobody,
    
mode => 
0755
,
    
}
}

client1:

client2:

案例二:

将master的sshd_config文件映射到client1上;

master:

1
2
3
4
5
6
7
[root@master manifests]# vim node1.pp
file {
"/etc/ssh/sshd_config"
:
        
ensure => present,
        
owner => root,
        
group => root,
        
mode => 
0400
,
        
}

client1:

案例三:

client2上/root/testdir目录下有多余文件及目录,我们进行清理;

master:

1
2
3
4
5
6
7
8
9
10
[root@master ~]# vim /etc/puppet/manifests/node1.pp
file { 
"/root/testdir"
:
        
ensure => directory,
        
owner => root,
        
group => root,
        
mode => 
0755
,
        
recurse => 
true
,
        
purge => 
true
,
        
force => 
true
,
        
}

client2:

案例四:

接上例,如果不想test.file文件删掉,其他都删掉呢?

master:

1
2
3
4
5
6
7
8
9
10
11
[root@master ~]# vim /etc/puppet/manifests/node1.pp
file { 
"/root/testdir"
:
        
ensure => directory,
        
owner => root,
        
group => root,
        
mode => 
0755
,
        
recurse => 
true
,
        
purge => 
true
,
        
force => 
true
,
        
ignore => 
'test.file'
,
        
}

client2:

案例五:

我们在client2上的/root/testdir/link.f创建为链接文件:

master:

1
2
3
4
5
[root@master manifests]# vim node1.pp
file {
"/root/testdir/link.f"
:
        
ensure => link,
        
target => 
'/etc/puppet/puppet.conf'
,
        
}

client2:

案例六:

我们在client端创建个目录树,类似mkdir -p 的效果。比如我要在/tmp/目录创建,/tmp/1,/tmp/1/2,/tmp/1/2,/tmp/1/2/3文件夹。

代码示例:

file { [ "/tmp/1", "/tmp/1/2", "/tmp/1/2/3" ]: ensure => "directory", }

案例七:

管理目录,不覆盖/覆盖已经存在的文件。

1
2
3
4
5
6
7
file { 
"/tmp/test"
:
    
replace => 
"true"
,  #不覆盖的话为
false
    
ensure => 
"present"
,
    
content => template(
"/etc/smartd.conf"
),  #此为引用本地的文件
    
owner => 
'zhangsan'
,
    
mode => 
644
,
}

案例八:

将client端需要跟新的文件先备份,然后将文件更新

1
2
3
4
5
6
7
8
file { 
"/tmp/ntp.conf"
:
    
backup => 
".zzq20131101"
, #先将文件备份ntp.conf.zzq.
20131101
#   backup => 
"false"
,  #此为不备份,默认是不备份的
    
ensure => present,
    
owner => zhangsan,
    
content => template(
"/etc/ntp.conf"
),
    
mode => 
644
,
    
}

本文转自 西索oO 51CTO博客,原文链接:http://blog.51cto.com/lansgg/1299857

转载地址:http://ntuhl.baihongyu.com/

你可能感兴趣的文章
Shell命令-文件及内容处理之grep(egrep)、join
查看>>
CentOS7 防火墙
查看>>
svn项目冲突时显示无法加载项目的解决方法
查看>>
2019-4-22 jdbc学习笔记
查看>>
7 行代码优雅地实现 Excel 文件导出功能?
查看>>
thinkphp3.2.3 无法调用带下划线的模型
查看>>
RK 3299 Ubuntu 配置密钥
查看>>
react-hooks: CSSProperties
查看>>
abp 关闭审计日志
查看>>
迭代器模式 Iterator 行为型 设计模式(二十)
查看>>
解决walle报错:宿主机代码检出检测出错,请确认svn用户名密码无误
查看>>
svn使用openldap验证apache访问方式
查看>>
Linux下安装emacs-24.3
查看>>
二分搜索找到所在区间
查看>>
拓扑排序(topsort)
查看>>
倒要看看你有啥本事
查看>>
JavaScript高级程序设计(第三版)学习笔记22、24、25章
查看>>
【清北学堂2018-刷题冲刺】Contest 3
查看>>
CCF NOI1030 角谷猜想
查看>>
HDU1042 n!
查看>>