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

[经验分享] puppet使用rsync模块同步目录和文件

[复制链接]

尚未签到

发表于 2015-9-16 11:36:51 | 显示全部楼层 |阅读模式
puppet使用rsync模块同步目录和文件 2013-09-23 14:28:57

  分类: LINUX
  


环境说明:
OS :    CentOS5.4                        i686
puppet版本:                               2.7.14
puppetmaster.manzuoinfo.com            192.168.0.12
puppet1.manzuoinfo.com               192.168.0.64
rsync server                           192.168.0.12

同步需求简介:
需要把 rsync server 192.168.0.12 上的 /tmp/default 和  /tmp/test 目录 分别同步到 puppet1.manzuinfo.com 192.168.0.12 上的 /tmp/test1 下和/tmp/test。


  点击(此处)折叠或打开


  • [iyunv@puppetmaster test]# cd /tmp/
  • [iyunv@puppetmaster tmp]# ls
  • default test
  • [iyunv@puppetmaster tmp]# cd test
  • /tmp/test
  • [iyunv@puppetmaster test]# ls -l
  • total 4
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 10.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 1.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 2.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 3.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 4.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 5.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 6.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 7.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 8.txt
  • -rw-r--r-- 1 root root 0 Sep 22 17:32 9.txt
  • -rw-r--r-- 1 root root 17 Sep 22 17:32 test.txt


  • [iyunv@puppetmaster default]# pwd
  • /tmp/default
  • [iyunv@puppetmaster default]# ls
  • de.txt

一、下载安装rsync 模块
puppet 的rsync 模块在GitHub位置 https://github.com/onyxpoint/pupmod-rsync ,大家可以上去查看。


  点击(此处)折叠或打开


  • [iyunv@puppetmaster ~]# cd /etc/puppet/modules/

  • [iyunv@puppetmaster modules]# git clone https://github.com/onyxpoint/pupmod-concat && mv pupmod-concat concat
  • [iyunv@puppetmaster modules]# git clone https://github.com/onyxpoint/pupmod-rsync && mv pupmod-rsync rsync

说明:我这里是使用git,如果机器没有安装,可以使用yum -y install git 来安装,如不想安装git可以下载zip压缩包.解压到相应目录.
注意:在server, clent 端都下载安装concat 和rsync 模块。
二、服务端配置文件

1). 新建node.pp


  点击(此处)折叠或打开


  • [iyunv@puppetmaster manifests]# pwd
  • /etc/puppet/manifests

  • [iyunv@puppetmaster manifests]# cat node.pp
  • node 'puppetmaster.manzuoinfo.com' {
  • include 'rsync::server'
  • rsync::server::global { 'global':
  •   address => '192.168.0.12'
  • }


  • rsync::server::section { 'default':
  •   comment => 'The default file path',
  •   path => '/tmp/default',
  •   hosts_allow => '192.168.0.64'
  • }


  • rsync::server::section { 'test':
  • # auth_users => 'testuser',
  •    comment => 'Test comment',
  •    path => '/tmp/test',
  •    hosts_allow => '192.168.0.64',
  •    outgoing_chmod => 'o-w'
  •   }
  • }


  • node 'puppet1.manzuoinfo.com' {
  • include 'rsync'
  • }

2). 新建modules.pp 载入rsync 模块


  点击(此处)折叠或打开


  • [iyunv@puppetmaster manifests]# pwd
  • /etc/puppet/manifests

  • [iyunv@puppetmaster manifests]# cat modules.pp
  • import "rsync"

3). 把 node.pp modules.pp 包进site.pp


  点击(此处)折叠或打开


  • [iyunv@puppetmaster test]# cat /etc/puppet/manifests/site.pp
  • import 'node.pp'
  • import 'modules.pp'

4). 测试配置文件


  点击(此处)折叠或打开


  • [iyunv@puppetmaster manifests]# puppet agent --server=puppetmaster.manzuoinfo.com --test -v
  • info: Caching catalog for puppetmaster.manzuoinfo.com
  • info: /Stage[main]/Rsync/Tidy[/etc/rsync]: File does not exist
  • info: Applying configuration version '1379830750'
  • notice: /Stage[main]/Rsync/File[/etc/rsync]/ensure: created
  • notice: /Stage[main]//Node[puppetmaster.manzuoinfo.com]/Rsync::Server::Section[default]/Concat_fragment[rsync+default.section]/content: content changed '' to '[default]
  • comment = The default file path
  • path = /home/sky
  • use chroot = false
  • max connections = 0
  • max verbosity = 1
  • lock file = /var/run/rsyncd.lock
  • read only = true
  • write only = false
  • list = false
  • uid = root
  • gid = root
  • outgoing chmod = o-w
  • ignore nonreadable = true
  • transfer logging = true
  • log format = "%o %h [%a] %m (%u) %f %l"
  • dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.rar *.jar *.pdf *.sar *.war
  • hosts allow = 192.168.0.64
  • hosts deny = *
  • '
  • notice: /Stage[main]//Node[puppetmaster.manzuoinfo.com]/Rsync::Server::Global[global]/Concat_fragment[rsync+global]/content: content changed '' to 'pid file = /var/run/rsyncd.pid
  • syslog facility = daemon
  • port = 873
  • address = 192.168.0.12
  • '
  • notice: /Stage[main]//Node[puppetmaster.manzuoinfo.com]/Rsync::Server::Section[test]/Concat_fragment[rsync+test.section]/content: content changed '' to '[test]
  • comment = Test comment
  • path = /home/skywu
  • use chroot = false
  • max connections = 0
  • max verbosity = 1
  • lock file = /var/run/rsyncd.lock
  • read only = true
  • write only = false
  • list = false
  • uid = root
  • gid = root
  • outgoing chmod = o-w
  • ignore nonreadable = true
  • transfer logging = true
  • log format = "%o %h [%a] %m (%u) %f %l"
  • dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.rar *.jar *.pdf *.sar *.war
  • hosts allow = 192.168.0.64
  • hosts deny = *
  • '
  • notice: /Stage[main]/Rsync::Server/Concat_build[rsync]/target: global*.section used for ordering
  • notice: /Stage[main]/Rsync::Server/File[/etc/rsyncd.conf]/mode: mode changed '0644' to '0400'
  • notice: /Stage[main]/Rsync::Server/File[/etc/rsyncd.conf]/content: audit change: newly-recorded value {md5}2148062cf8d2f7220279fd0ca07b9329
  • info: /Stage[main]/Rsync::Server/File[/etc/rsyncd.conf]: Scheduling refresh of Service[rsync]
  • notice: /Stage[main]/Rsync::Server/Service[rsync]/ensure: ensure changed 'stopped' to 'running'
  • err: /Stage[main]/Rsync::Server/Service[rsync]: Failed to call refresh: Could not stop Service[rsync]: Execution of '/bin/kill `cat \`grep "pid file" /etc/rsyncd.conf | cut -f4 -d' '\``' returned 1: at /etc/puppet/modules/rsync/manifests/server.pp:56
  • info: Creating state file /var/lib/puppet/state/state.yaml
  • notice: Finished catalog run in 2.70 seconds
  • [iyunv@puppetmaster manifests]#




注意:err: 这里出现了一个错误,请根据自己的情况来修改停止rsync,例如:
    stop => "ps -ef | grep [r]sync |awk '{print $2}'|xargs kill -9",
5). 在server 端生成rsync配置


  点击(此处)折叠或打开


  • [iyunv@puppetmaster manifests]# puppetd --test --server puppetmaster.manzuoinfo.com
  • info: Caching catalog for puppetmaster.manzuoinfo.com
  • notice: /Stage[main]/Rsync/Tidy[/etc/rsync]: Tidying File[/etc/rsync]
  • info: /File[/etc/rsync]: Duplicate generated resource; skipping
  • info: Applying configuration version '1379837152'
  • notice: /Stage[main]//Node[puppetmaster.manzuoinfo.com]/Rsync::Server::Section[default]/Concat_fragment[rsync+default.section]/content: content changed '[default]
  • comment = The default file path
  • path = /home/sky
  • use chroot = false
  • max connections = 0
  • max verbosity = 1
  • lock file = /var/run/rsyncd.lock
  • read only = true
  • write only = false
  • list = false
  • uid = root
  • gid = root
  • outgoing chmod = o-w
  • ignore nonreadable = true
  • transfer logging = true
  • log format = "%o %h [%a] %m (%u) %f %l"
  • dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.rar *.jar *.pdf *.sar *.war
  • hosts allow = 192.168.0.64
  • hosts deny = *
  • ' to '[default]
  • comment = The default file path
  • path = /tmp/default
  • use chroot = false
  • max connections = 0
  • max verbosity = 1
  • lock file = /var/run/rsyncd.lock
  • read only = true
  • write only = false
  • list = false
  • uid = root
  • gid = root
  • outgoing chmod = o-w
  • ignore nonreadable = true
  • transfer logging = true
  • log format = "%o %h [%a] %m (%u) %f %l"
  • dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.rar *.jar *.pdf *.sar *.war
  • hosts allow = 192.168.0.64
  • hosts deny = *
  • '
  • notice: /Stage[main]//Node[puppetmaster.manzuoinfo.com]/Rsync::Server::Section[test]/Concat_fragment[rsync+test.section]/content: content changed '[test]
  • comment = Test comment
  • path = /home/skywu
  • use chroot = false
  • max connections = 0
  • max verbosity = 1
  • lock file = /var/run/rsyncd.lock
  • read only = true
  • write only = false
  • list = false
  • uid = root
  • gid = root
  • outgoing chmod = o-w
  • ignore nonreadable = true
  • transfer logging = true
  • log format = "%o %h [%a] %m (%u) %f %l"
  • dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.rar *.jar *.pdf *.sar *.war
  • hosts allow = 192.168.0.64
  • hosts deny = *
  • ' to '[test]
  • comment = Test comment
  • path = /tmp/test
  • use chroot = false
  • max connections = 0
  • max verbosity = 1
  • lock file = /var/run/rsyncd.lock
  • read only = true
  • write only = false
  • list = false
  • uid = root
  • gid = root
  • outgoing chmod = o-w
  • ignore nonreadable = true
  • transfer logging = true
  • log format = "%o %h [%a] %m (%u) %f %l"
  • dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.rar *.jar *.pdf *.sar *.war
  • hosts allow = 192.168.0.64
  • hosts deny = *
  • '
  • notice: /Stage[main]/Rsync::Server/Concat_build[rsync]/target: global*.section used for ordering
  • notice: /Stage[main]/Rsync::Server/File[/etc/rsyncd.conf]/mode: mode changed '0644' to '0400'
  • notice: /Stage[main]/Rsync::Server/File[/etc/rsyncd.conf]/content: audit change: previously recorded value {md5}2148062cf8d2f7220279fd0ca07b9329 has been changed to {md5}8063eb4c3129b055fb0106eb2cfd7912
  • info: /Stage[main]/Rsync::Server/File[/etc/rsyncd.conf]: Scheduling refresh of Service[rsync]
  • info: /Stage[main]/Rsync::Server/File[/etc/rsyncd.conf]: Scheduling refresh of Service[rsync]
  • notice: /Stage[main]/Rsync::Server/Service[rsync]/ensure: ensure changed 'stopped' to 'running'
  • notice: /Stage[main]/Rsync::Server/Service[rsync]: Triggered 'refresh' from 2 events
  • notice: Finished catalog run in 3.66 seconds
注意:/etc/rsyncd.conf 是生成的,而不是手动创建的。
内容如下:


  点击(此处)折叠或打开


  • [iyunv@puppetmaster manifests]# cat /etc/rsyncd.conf
  • pid file = /var/run/rsyncd.pid
  • syslog facility = daemon
  • port = 873
  • address = 192.168.0.12
  • [default]
  • comment = The default file path
  • path = /tmp/default
  • use chroot = false
  • max connections = 0
  • max verbosity = 1
  • lock file = /var/run/rsyncd.lock
  • read only = true
  • write only = false
  • list = false
  • uid = root
  • gid = root
  • outgoing chmod = o-w
  • ignore nonreadable = true
  • transfer logging = true
  • log format = "%o %h [%a] %m (%u) %f %l"
  • dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.rar *.jar *.pdf *.sar *.war
  • hosts allow = 192.168.0.64
  • hosts deny = *
  • [test]
  • comment = Test comment
  • path = /tmp/test
  • use chroot = false
  • max connections = 0
  • max verbosity = 1
  • lock file = /var/run/rsyncd.lock
  • read only = true
  • write only = false
  • list = false
  • uid = root
  • gid = root
  • outgoing chmod = o-w
  • ignore nonreadable = true
  • transfer logging = true
  • log format = "%o %h [%a] %m (%u) %f %l"
  • dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.rar *.jar *.pdf *.sar *.war
  • hosts allow = 192.168.0.64
  • hosts deny = *
  • [iyunv@puppetmaster manifests]#

6). 启动rsync 服务


  点击(此处)折叠或打开


  • [iyunv@puppetmaster manifests]# /etc/init.d/xinetd restart
  • Stopping xinetd: [ OK ]
  • Starting xinetd: [ OK ]

  • [iyunv@puppetmaster manifests]# ps -ef | grep rsync
  • root 13227 1 0 16:16 ? 00:00:00 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
  • root 13266 3142 0 16:19 pts/0 00:00:00 grep rsync

7). 客户端需要执行rsync类中,我们编辑puppetmaster上的代码文件,添加default 和test。


  点击(此处)折叠或打开


  • [iyunv@puppetmaster manifests]# vim /etc/puppet/modules/rsync/manifests/init.pp
  • class rsync {
  •   package { "rsync": ensure => "latest" }

  •   # This define provides a useful abstraction for common rsync client side
  •   # activities.
  •   exec { "rsync_stub":
  •     command => "/bin/true",
  •     refreshonly => true
  •   }


  •   file { '/etc/rsync':
  •     ensure => 'directory',
  •     owner => 'root',
  •     group => 'root',
  •     mode => '750'
  •   }


  •   tidy { '/etc/rsync':
  •     size => "0b",
  •     recurse => 'true',
  •     rmdirs => 'true'
  •   }


  •   rsync { 'default':
  •     source => 'default',
  •     target => '/tmp/test1',
  •     server => '192.168.0.12'
  •   }


  •   rsync { 'test':
  •     source => 'test',
  •     target => '/tmp/test',
  •     server => '192.168.0.12'
  •   }
  • }

三、puppet1 客户端测试


  点击(此处)折叠或打开


  • [iyunv@puppet1 tmp]# puppetd --test --server puppetmaster.manzuoinfo.com
  • info: Caching catalog for puppet1.manzuoinfo.com
  • notice: /Stage[main]/Rsync/Tidy[/etc/rsync]: Tidying File[/etc/rsync]
  • info: /File[/etc/rsync]: Duplicate generated resource; skipping
  • info: Applying configuration version '1379841152'
  • notice: /Stage[main]/Rsync/Rsync[test]/do: executed successfully
  • notice: Finished catalog run in 0.75 seconds


  • 或者用命令:
  • [iyunv@puppet1 tmp]# puppet agent --test --server puppetmaster.manzuoinfo.com



  点击(此处)折叠或打开


  • [iyunv@puppet1 test1]# pwd
  • /tmp/test1
  • [iyunv@puppet1 test1]# ls
  • de.txt
  • [iyunv@puppet1 test]# pwd
  • /tmp/test
  • [iyunv@puppet1 test]# ls -l
  • total 4
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 10.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 1.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 2.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 3.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 4.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 5.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 6.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 7.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 8.txt
  • -rw-r--r-- 1 root root 0 Sep 23 11:42 9.txt
  • -rw-r--r-- 1 root root 17 Sep 23 11:42 test.txt

在客户端查看/tmp/test 和 /tmp/test1 目录及文件已经同步过来了。

运维网声明 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-114422-1-1.html 上篇帖子: Create Virtual Machines with Vagrant and Puppet 下篇帖子: RHEL6下puppet部署管理1之安装测试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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