设为首页 收藏本站
查看: 1022|回复: 0

[经验分享] puppet资源file详细介绍(附案例)

[复制链接]

尚未签到

发表于 2018-8-2 10:22:25 | 显示全部楼层 |阅读模式
一、资源介绍
  Description
  Manages files, including their content, ownership, and permissions.
  The file type can manage normal files, directories, and symlinks; the type should be specified in the ensure attribute. Note that symlinks cannot be managed on Windows systems.
  File contents can be managed directly with the content attribute, or downloaded from a remote source using the source attribute; the latter can also be used to recursively serve directories (when the recurse attribute is set to true or local). On Windows, note that file contents are managed in binary mode; Puppet never automatically translates line endings.
  Autorequires: If Puppet is managing the user or group that owns a file, the file resource will autorequire them. If Puppet is managing any parent directories of a file, the file resource will autorequire them.

  Attributes file { 'resource>  # ...plus any applicable metaparameters.  
}
1、实现功能
  1.1、支持文件和目录
  1.2、设置文件及目录的所有者及权限
  1.3、恢复文件(包括文件的内容、权限及所有者)
  1.4、清理目录以及子目录
二、系统环境
1、puppet服务端
Release:RHEL6.4  
HOSTNAME: puppetserver.kisspuppet.com
  
TCP/IP: 172.16.200.100/24
  
Packages:
  
puppet-server-2.7.21-1.el6.noarch
  
mcollective-client-2.2.4
  
activemq-5.5.0
2、puppet节点
Release: RHEL5.8  
HOSTNAME: agent1.kisspuppet.com
  
TCP/IP: 172.16.200.101/24
  
Packages:
  
puppet-2.7.21-1.el5
  
mcollective-2.2.4-1.el5
3、puppet节点
Release: RHEL6.4  
HOSTNAME: agent3.kisspuppet.com
  
TCP/IP: 172.16.200.103/24
  
Packages:
  
puppet-2.7.21-1.el6
  
mcollective-2.2.4-1.el6
3、支持参数
1.1 ensure => {present|absent|directory|file|link}, 指定文件的目标状态  
=> present, 检查文件是否存在,不存在则新建之
  
=> absent, 检查文件是否存在,存在则删除之
  
=> directory, 指定这是一个目录,不存在则创建
1.2 owner|user => root, 所属用户,也可以用UID1.3 group => puppet, 所属用户组,也可以用GID1.4 mode => 0644, 权限属性,四位八进制数1.5 source => "puppet:///modules/ssh/etc/ssh/sshd_config" | source => "/etc/passwd" 文件获取地址,以puppet:///开头为从master下载,正常路径则在agent本地读取  
备注:"puppet://"等价于主配置文件puppet.config中的modulepath值
1.6 path => "/etc/postfix/main.cf",  文件完整路径。默认与title相同可不写  
eg.
  
file { "main.cf":
  
  path => "/etc/postfix/main.cf",
  
}
1.7 content => "hello",|content => template("postfix/main.cf.erb"),  文件的具体内容,亦可由erb模板生成,选择这个可不写资源source1.8 backup => 'main',| backup => ".$backup_date.bak", 节点更新之前上一个版本备份方式;backup => 'main',需要结合资源filebucket实现1.9 recurse => '{true|false|inf|remote}', 对目录是(true)否(false)递归(ensure => directory时有效)1.10 puppet依赖关系资源有三个,分别为require,before,after  
require => Class["mysql::install"], | require => Package["setup"],  当前资源或者类被要求的资源或者类所依赖,需要被要求的资源或者类先执行成功后在执行自己的资源或者类
  
before  在某个资源之前执行
  
package { "openssh-server":
  
...
  
  before => File["/etc/ssh/sshd_config"],
  
}
  
after 在某个资源之后执行
  
file {"/etc/ssh/sshd_config":
  
...
  
  after => Package["openssh-server"],
  
}
1.11 puppet触发更新有两个,分别为notify,subscribe,写的位置不同。  
notify {"operatingsystem is $operatingsystem":  将输出内容记录到日志里面,可在调试的时候查看。
  
  withpath => true|false,  #是否打印全路径
  
}
  
notify => Class["mysql::service"], 当前类或者资源的文件被改动后通知服务重启。
  
subscribe => Class["ssh::config"], 该资源有更新时,通知另一个资源执行相应的动作。目前支持subscribe只有exec、service、mount
1.12 link软连接设置 /etc/file2 -> /etc/passwd  
file{ "/etc/file2":
  
...
  
  ensure => link,
  
  target => "/etc/passwd",
  
}
1.13 purge => true  清理目录下面没有被资源被管理的文件都会被清除  
force => true   和purge => true配合使用才能删除目录,mode => 0700保证具有删除权限
  
ignore => file|directory, 忽略某一个目录或者文件做任何操作
三、资源示例
1、示例一
1.1 实现功能  
*要求从服务器指定路径下载motd文件
  
*要求文件权限为700,属组和属主都为puppet
  
*要求setup包在motd文件下载之前被安装
1.2 配置说明  
class motd::motd {
  
  package{ setup:
  
    ensure  => present,
  
  }
  
  file{ "/etc/motd":
  
    owner   => "puppet",
  
    group   => "puppet",
  
    mode    => 0700,
  
    source  => "puppet://$puppetserver/modules/motd/etc/motd",
  
    require => Package["setup"],
  
  }
1.3 客户端agent1上测试  
[root@agent1 ~]# puppet agent --test
  
info: Caching catalog for agent1.kisspuppet.com
  
info: Applying configuration version '1378193573'
  
notice: /File[/etc/motd]/ensure: defined content as '{md5}0acb622c16dbdecb670d8920d96bdd30'
  
notice: Finished catalog run in 0.41 seconds
  
[root@agent1 ~]# ll /etc/motd
  
-rwx------ 1 puppet puppet 82 Sep  3 15:33 /etc/motd
2、示例二
2.1 实现功能  
*在节点上创建/etc/passwd的软连接为/etc/file2
2.2 配置说明  
class motd::file2 {
  
  file{ "/etc/file2":
  
    owner  => "puppet",
  
    group  => "puppet",
  
    ensure => link,
  
    target => "/etc/passwd",
  
  }
  
}
2.3 客户端agent1上测试  
[root@agent1 ~]# puppet agent --test
  
info: Caching catalog for agent1.kisspuppet.com
  
info: Applying configuration version '1378194373'
  
notice: /File[/etc/file2]/ensure: created
  
notice: Finished catalog run in 0.07 seconds
  
[root@agent1 ~]# ll /etc/file2
  
lrwxrwxrwx 1 puppet puppet 11 Sep  3 15:46 /etc/file2 -> /etc/passwd
3、示例三
3.1 实现功能  
*在节点上创建/etc/dir1目录
  
*要求目录下面除了dir2外的所有目录及文件的权限为0700,所有者为puppet
  
*要求每次更新将"This is dir1!"写入日志里面
3.2 配置说明  
class motd::dir1 {
  
  file{ "/etc/dir1":
  
    owner   => "puppet",
  
    group   => "puppet",
  
    mode    => 0700,
  
    ensure  => directory,
  
    recurse => true,
  
    purge   => true,
  
    force   => true,
  
    ignore  => "dir2",
  
  }
  
  notify { "This is dir1!":
  
#   withpath => true,
  
  }
  
}
3.3 客户端agent1上测试  
[root@agent1 ~]# puppet agent --test
  
info: Caching catalog for agent1.kisspuppet.com
  
info: Applying configuration version '1378195554'
  
notice: This is dir1!
  
notice: /Stage[main]/Motd::Dir1/Notify[This is dir1!]/message: defined 'message' as 'This is dir1!'
  
notice: /File[/etc/dir1/dir3]/owner: owner changed 'root' to 'puppet'
  
notice: /File[/etc/dir1/dir3]/group: group changed 'root' to 'puppet'
  
notice: /File[/etc/dir1/dir3]/mode: mode changed '0755' to '0700'
  
notice: /File[/etc/dir1/dir3]/seluser: seluser changed 'root' to 'system_u'
  
notice: /File[/etc/dir1/dir3/file3]/owner: owner changed 'root' to 'puppet'
  
notice: /File[/etc/dir1/dir3/file3]/group: group changed 'root' to 'puppet'
  
notice: /File[/etc/dir1/dir3/file3]/mode: mode changed '0644' to '0700'
  
notice: /File[/etc/dir1/dir3/file3]/seluser: seluser changed 'root' to 'system_u'
  
notice: Finished catalog run in 0.11 seconds
  
[root@agent1 ~]#
  
[root@agent1 ~]#
  
[root@agent1 ~]# ll /etc/dir1/
  
total 16
  
drwxrwxrwx 2 puppet puppet 4096 Sep  3 16:00 dir2
  
drwx------ 2 puppet puppet 4096 Sep  3 16:06 dir3
4、示例四
4.1 实现功能  
*在节点上创建/etc/dir2目录,权限为0700,所有者为puppet
  
*要求目录下面只允许有dir1目录,并且dir1目录及下一级目录或文件权限属性保持原有不变
4.2 配置说明  
class motd::dir2 {
  
  file{ "/etc/dir2":
  
    owner   => "puppet",
  
    group   => "puppet",
  
    mode    => 0700,
  
    ensure  => directory,
  
    recurse => true,
  
    purge   => true,
  
    force   => true,
  
    ignore  => "dir1",
  
  }
  
}
4.3 客户端agent1上测试  
[root@agent1 ~]# puppet agent --test
  
info: Retrieving plugin
  
info: Loading facts in /var/lib/puppet/lib/facter/my_apply2.rb
  
info: Loading facts in /var/lib/puppet/lib/facter/my_apply1.rb
  
info: Loading facts in /var/lib/puppet/lib/facter/my_apply3.rb
  
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
  
info: Caching catalog for agent1.kisspuppet.com
  
info: Applying configuration version '1378195951'
  
notice: /File[/etc/dir2]/ensure: created
  
notice: Finished catalog run in 0.05 seconds
  
[root@agent1 ~]# mkdir /etc/dir2/dir1
  
[root@agent1 ~]# mkdir /etc/dir2/dir2
  
[root@agent1 ~]# touch /etc/dir2/dir1/file1
  
[root@agent1 ~]# touch /etc/dir2/dir2/file2
  
[root@agent1 ~]# puppet agent --test
  
info: Retrieving plugin
  
info: Loading facts in /var/lib/puppet/lib/facter/my_apply2.rb
  
info: Loading facts in /var/lib/puppet/lib/facter/my_apply1.rb
  
info: Loading facts in /var/lib/puppet/lib/facter/my_apply3.rb
  
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
  
info: Caching catalog for agent1.kisspuppet.com
  
info: Applying configuration version '1378195951'
  
info: /File[/etc/dir2/dir2]: Recursively backing up to filebucket
  
info: FileBucket adding {md5}d41d8cd98f00b204e9800998ecf8427e
  
info: /File[/etc/dir2/dir2]: Filebucketed /etc/dir2/dir2/file2 to puppet with sum d41d8cd98f00b204e9800998ecf8427e
  
notice: /File[/etc/dir2/dir2]/ensure: removed
  
notice: Finished catalog run in 0.09 seconds
  
[root@agent1 ~]# ll /etc/dir2/
  
total 8
  
drwxr-xr-x 2 root root 4096 Sep  3 16:13 dir1
  
[root@agent1 ~]#
  文章原文:http://kisspuppet.com/2013/11/14/file/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-545198-1-1.html 上篇帖子: puppet语法与命令详解 下篇帖子: puppet资源yumrepo详细介绍(附案例)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表