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',
}




你可能感兴趣的:(puppet自动化之file)