puppet自动化之file

file文件资源的定义

file:
                    Manages files, including their content, ownership, and permissions.
                    
                    ensure:Whether the file should exist, and if so what kind of file it should be. Possible values are `present`, `absent`, `file`, `directory`, and `link`.
                        file:类型为普通文件,其内容由content属性生成或复制由source属性指向的文件路径来创建;
                        link:类型为符号链接文件,必须由target属性指明其链接的目标文件;
                        directory:类型为目录,可通过source指向的路径复制生成,recurse属性指明是否递归复制;
                    path:文件路径;
                    source:源文件;
                    content:文件内容;
                    target:符号链接的目标文件; 
                    owner:属主
                    group:属组
                    mode:权限;
                    atime/ctime/mtime:时间戳;
[root@centos7 ~]# cat file.pp  定义以个复制某文件到某目录下的文件
file{'/etc/redis.conf':
    source => '/root/redis.conf',
    owner => redis,
    group => root,
    ensure => file,
}
[root@centos7 ~]# puppet apply -v --noop file.pp  干跑运行检查语法
Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.13 seconds
Info: Applying configuration version '1506266600'
Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: current_value {md5}d98629fded012cd2
a25b9db0599a9251, should be {md5}375f2bc6dfa3bdacfbcdd24b29d08daf (noop)Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.22 seconds
[root@centos7 ~]# puppet apply -v file.pp  运行
Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.12 seconds
Info: Applying configuration version '1506266783'
Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d98629fd
ed012cd2a25b9db0599a9251Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}d98629fded012cd2a25b9db0
599a9251' to '{md5}375f2bc6dfa3bdacfbcdd24b29d08daf'Notice: Finished catalog run in 0.15 seconds
通知元参数:
                    A notify B:B依赖于A,接受由A触发refresh;
                    B subscribe A:B依赖于A,接受由A触发refresh;
                    
                    示例1:
                        file{'test.txt':
                            path    => '/tmp/test.txt',
                            ensure  => file,
                            source  => '/etc/fstab',
                        }

                        file{'test.symlink':
                            path    => '/tmp/test.symlink',
                            ensure  => link,
                            target  => '/tmp/test.txt',
                            require => File['test.txt'],
                        }

                        file{'test.dir':
                            path    => '/tmp/test.dir',
                            ensure  => directory,
                            source  => '/etc/yum.repos.d/',
                            recurse => true,
                        }
[root@centos7 ~]# cat servicecx.pp 
service{'redis':
    ensure => true,
    enable => true,
    hasrestart => true,
#   subscribe => File ['/etc/redis.conf'], #表示下面资源发生会触发此资源刷新
}
file{'/etc/redis.conf':
    source => '/root/redis.conf',
    owner => redis,
    group => root,
    ensure => file,
#   notify => Service['redis'],#表示此资源发生变化会通知给上面的资源
}
File['/etc/redis.conf'] ~> Service['redis'] #也是表示下面的资源发生变化通知上面的资源

root@centos7 ~]# puppet apply -v --noop servicecx.pp 
Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.27 s
econdsInfo: Applying configuration version '1506267989'
Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: current_value {md5}375f2b
c6dfa3bdacfbcdd24b29d08daf, should be {md5}87104f85cf87166e4215cdf7646f9103 (noop)Info: /Stage[main]/Main/File[/etc/redis.conf]: Scheduling refresh of Service[redis
]Notice: /Stage[main]/Main/Service[redis]: Would have triggered 'refresh' from 1 ev
entsNotice: Class[Main]: Would have triggered 'refresh' from 2 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.09 seconds
[root@centos7 ~]# ss -lnt   检查结果
State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN     0      128                               172.16.251.203:6379                                                       *:*                  
LISTEN     0      128                                            *:111                                                        *:*                  
LISTEN     0      5                                  192.168.122.1:53                                                         *:*                  
LISTEN     0      128                                            *:22                                                         *:*                  
LISTEN     0      128                                    127.0.0.1:631                                                        *:*                  
LISTEN     0      100                                    127.0.0.1:25                                                         *:*                  
LISTEN     0      128                                           :::111                                                       :::*                  
LISTEN     0      128                                           :::22                                                        :::*                  
LISTEN     0      128                                          ::1:631                                                       :::*                  
LISTEN     0      100                                          ::1:25                                                        :::*                  
[root@centos7 ~]# cat cfx.pp  创建一个自动生成内容的文件
file{'chenxi.txt':
    path => '/tmp/chenxi.txt',
    content => 'hi there\n',
    ensure => file,
}
[root@centos7 ~]# puppet apply -v --noop cfx.pp  干跑运行检查是否有错
Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.11 s
econdsInfo: Applying configuration version '1506268825'
Notice: /Stage[main]/Main/File[chenxi.txt]/ensure: current_value absent, should be
 file (noop)Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.03 seconds
[root@centos7 ~]# puppet apply -v cfx.pp    生成此文件
Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.13 s
econdsInfo: Applying configuration version '1506268853'
Notice: /Stage[main]/Main/File[chenxi.txt]/ensure: defined content as '{md5}d52a8a
0512f44317a61ee80d8b9eb784'Notice: Finished catalog run in 0.11 seconds
[root@centos7 ~]# cat /tmp/chenxi.txt 
hi there\n
复制目录及目录下的所有文件
[root@centos7 ~]# cat cfx.pp 
file{'chenxi.txt':
    path => '/tmp/chenxi.txt',
    content => 'hi there\n',
    ensure => file,
}
[root@centos7 ~]# puppet apply -v  fgcx.pp 
Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.12 s
econdsInfo: Applying configuration version '1506269549'
Notice: /Stage[main]/Main/File[/tmp/pam.d]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/pam.d/runuser]/ensure: defined content as '{md
5}b8b44b045259525e0fae9e38fdb2aeeb'Notice: /Stage[main]/Main/File[/tmp/pam.d/system-auth]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/pam.d/smtp.postfix]/ensure: defined content as
 '{md5}a4fb464fbcf0f1f470ea2016d120df62'Notice: /Stage[main]/Main/File[/tmp/pam.d/passwd]/ensure: defined content as '{md5
}dcc4e27593a30780464a87d69edfcf68'Notice: /Stage[main]/Main/File[/tmp/pam.d/pluto]/ensure: defined content as '{md5}
f0188fc8f667dc97590f885fd1adfc7d'Notice: /Stage[main]/Main/File[/tmp/pam.d/remote]/ensure: defined content as '{md5
}5841e2efb8ead55ad7f0a385c41db2da'Notice: /Stage[main]/Main/File[/tmp/pam.d/polkit-1]/ensure: defined content as '{m
d5}038eb4e924b8027bfe852e63edb998de'Notice: /Stage[main]/Main/File[/tmp/pam.d/gdm-pin]/ensure: defined content as '{md
5}0545417c9f340889324a244afe9d137a'Notice: /Stage[main]/Main/File[/tmp/pam.d/password-auth]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/pam.d/cups]/ensure: defined content as '{md5}b
b713ba94821433dbf61113eaa532a31'Notice: /Stage[main]/Main/File[/tmp/pam.d/vlock]/ensure: defined content as '{md5}
7ddd3d661b917ab94d398a5d88033ed6'Notice: /Stage[main]/Main/File[/tmp/pam.d/gdm-password]/ensure: defined content as
 '{md5}6e95fcc3e9cc35473e475b6edf8e58f2'Notice: /Stage[main]/Main/File[/tmp/pam.d/su-l]/ensure: defined content as '{md5}7
56fef5687fecc0d986e5951427b0c4f'Notice: /Stage[main]/Main/File[/tmp/pam.d/fingerprint-auth-ac]/ensure: defined con
tent as '{md5}f47f343a190b3b5b88276570a33552cc'Notice: /Stage[main]/Main/File[/tmp/pam.d/gdm-launch-environment]/ensure: defined 
content as '{md5}3056e83ce16b77618f463eab6ecb83d6'Notice: /Stage[main]/Main/File[/tmp/pam.d/gdm-fingerprint]/ensure: defined content
 as '{md5}414790e978e9d5e688d213cbcf52d3e0'Notice: /Stage[main]/Main/File[/tmp/pam.d/sudo-i]/ensure: defined content as '{md5
}6151e51bcdcf427be3d57ca53cca02f7'Notice: /Stage[main]/Main/File[/tmp/pam.d/gdm-smartcard]/ensure: defined content a
s '{md5}30cb356eaf252adecebb60f14758e16e'Notice: /Stage[main]/Main/File[/tmp/pam.d/smartcard-auth]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/pam.d/vmtoolsd]/ensure: defined content as '{m
d5}5b4c1dd1b10c8b1b99d084146ff5cc60'Notice: /Stage[main]/Main/File[/tmp/pam.d/setup]/ensure: defined content as '{md5}
64ee33eed57429e249335b4c64c88ec9'Notice: /Stage[main]/Main/File[/tmp/pam.d/gdm-autologin]/ensure: defined content a
s '{md5}b9d28c0e446abbb36439200b6e6b24c6'Notice: /Stage[main]/Main/File[/tmp/pam.d/sudo]/ensure: defined content as '{md5}b
a24c05c27c14f376233910f656dfa66'Notice: /Stage[main]/Main/File[/tmp/pam.d/runuser-l]/ensure: defined content as '{
md5}2106ea05877e8913f34b2c77fa02be45'Notice: /Stage[main]/Main/File[/tmp/pam.d/xserver]/ensure: defined content as '{md
5}3a78b7163c9f27314c0f8d3658f3813c'Notice: /Stage[main]/Main/File[/tmp/pam.d/other]/ensure: defined content as '{md5}
af140f3d3ae7fcf504f103e72256cfef'Notice: /Stage[main]/Main/File[/tmp/pam.d/sshd]/ensure: defined content as '{md5}c
a51dcdb22404ef8cfb875583388c9de'Notice: /Stage[main]/Main/File[/tmp/pam.d/password-auth-ac]/ensure: defined conten
t as '{md5}358c54c8ef7d1c16191fd015da90d3a6'Notice: /Stage[main]/Main/File[/tmp/pam.d/chfn]/ensure: defined content as '{md5}2
0697b6a640ccd785cb8c96ac8c1ff7c'Notice: /Stage[main]/Main/File[/tmp/pam.d/smtp]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/pam.d/postlogin]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/pam.d/config-util]/ensure: defined content as 
'{md5}ac222217925c4552d63a8982a1c2937c'Notice: /Stage[main]/Main/File[/tmp/pam.d/chsh]/ensure: defined content as '{md5}2
0697b6a640ccd785cb8c96ac8c1ff7c'Notice: /Stage[main]/Main/File[/tmp/pam.d/postlogin-ac]/ensure: defined content as
 '{md5}1af006491505807b0aff49584045cf2e'Notice: /Stage[main]/Main/File[/tmp/pam.d/atd]/ensure: defined content as '{md5}00
0d2f30379d2bf8af09f51416e863ec'Notice: /Stage[main]/Main/File[/tmp/pam.d/crond]/ensure: defined content as '{md5}
73dbc2487cc4d9f5fab5e4cdea7aed7e'Notice: /Stage[main]/Main/File[/tmp/pam.d/ppp]/ensure: defined content as '{md5}f6
21e5f4c67b70a30f446fa397b9c9be'Notice: /Stage[main]/Main/File[/tmp/pam.d/fingerprint-auth]/ensure: created
Notice: /Stage[main]/Main/File[/tmp/pam.d/systemd-user]/ensure: defined content as
 '{md5}75f50eec5885dbbcc548d248c75bb194'Notice: /Stage[main]/Main/File[/tmp/pam.d/login]/ensure: defined content as '{md5}
0e8c66d8879a5f4c55b82e2fcc19459a'Notice: /Stage[main]/Main/File[/tmp/pam.d/system-auth-ac]/ensure: defined content 
as '{md5}4e1f3dce31807b69fed4ac37ecffeaad'Notice: /Stage[main]/Main/File[/tmp/pam.d/liveinst]/ensure: defined content as '{m
d5}f5be82be51135cf46de5955695f67c0d'Notice: /Stage[main]/Main/File[/tmp/pam.d/smartcard-auth-ac]/ensure: defined conte
nt as '{md5}0d667e4761f4e703e9b479d0f595b61f'Notice: /Stage[main]/Main/File[/tmp/pam.d/su]/ensure: defined content as '{md5}951
e804edc88857ad7fbce0fc515e23f'Notice: Finished catalog run in 0.59 seconds
[root@centos7 ~]# cat lik.pp 创建连接文件
file{'/tmp/tesr.link':
    ensure => link,
    target => '/tmp/chenxi.txt',
}




最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 1. 概述 在网络环境中一般用户只需要在浏览器中输入url如www.sunny.com就可以到对应服务器获取相应的...
    ghbsunny阅读 2,675评论 0 7
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • 最近很多次,隐约之间感到,有一种缘分在默默的堆积,悄悄的释放,正是时候。 就拿最近的事情来说,正愁着某个重要的工作...
    自在牛阅读 189评论 0 1
  • 回想起在做愿景板的过程中,内心充满想象,寻找符合心意的图片,细分目标,发现障碍,寻找对策,一步步完成完善,内心不断...
    田心远阅读 927评论 10 9