使用Puppet同步两个目录

我有一堆服务器上有两个目录需要同步。 例如,在同一台服务器上,我可能有/var/path/to/dir1/var/path/to/dir2 ,它们可能在每个文件和文件夹的下面。 我想同步/var/path/to/dir1并将/var/path/to/dir2作为“备份”。

有没有人知道一个有效的方式在木偶同步这两个目录? 我将不得不创build一个自定义脚本来执行此操作,并使用Puppet清单文件中的exec方法?

我的猜测是创build一个脚本来recursionvalidation主目录中每个文件上的MD5散列,然后使用该列表validation第二个目录中的文件。 我不确定是否有办法在木偶本身做到这一点。

你和傀儡走在了正确的轨道上。 让傀儡大师充当真相的源头,把它们放在运行你的傀儡大师的服务器中,并且宣布应该存在的文件和那里的规范版本。 你甚至可能想使用类似SVN或GIT的方式,在puppetmaster服务器上提供你正在使用的文件。

这为您提供了有关这两个位置的一些有力保证。

 file { '/var/path/to/dir1/': ensure => 'present', source => 'puppet:///path/to/dir1', recurse => 'true', } file { '/var/path/to/dir2/': ensure => 'present', source => 'puppet:///path/to/dir1', recurse => 'true', } 

如上所述,您可能需要删除任何收集到的不受欢迎的附加内容

 { purge => 'true', force => 'true', } 

正如我指出的那样(谢谢!!),如果'/ var / path / to / dir1 /'下有大量文件或子文件夹的深层次结构,则完成木偶运行的时间可能会增加迅速。 我仍然build议保持您的傀儡服务器作为真相的来源,但是您可能会更好地使用rsync,如下所示:

 rsync::get { '/var/path/to/dir1/': source => 'puppet:///path/to/dir1', purge => true, } rsync::get { '/var/path/to/dir2/': source => 'puppet:///path/to/dir1', purge => true, } 

我很好奇,试了一下:

 [root@localhost vagrant]# cat test.pp file { '/tmp/path2': ensure => directory, source => 'file:///tmp/path1', recurse => true, purge => true, force => true, } [root@localhost vagrant]# ls -lR /tmp/path1 /tmp/path1: total 4 -rw-r--r-- 1 root root 0 Nov 29 01:55 bar drwxr-xr-x 2 root root 4096 Nov 29 02:07 folder -rw-r--r-- 1 root root 0 Nov 29 01:55 foo -rw-r--r-- 1 root root 0 Nov 29 01:55 x /tmp/path1/folder: total 0 -rw-r--r-- 1 root root 0 Nov 29 02:07 xxx [root@localhost vagrant]# ls -lR /tmp/path2/ /tmp/path2/: total 4 -rw-r--r-- 1 root root 0 Nov 29 02:17 deleteme drwxr-xr-x 2 root root 4096 Nov 29 02:17 folder2 /tmp/path2/folder2: total 0 -rw-r--r-- 1 root root 0 Nov 29 02:17 deleteme [root@localhost vagrant]# puppet apply test.pp Notice: Compiled catalog for localhost in environment production in 0.04 seconds Notice: /Stage[main]/Main/File[/tmp/path2/x]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e' Notice: /Stage[main]/Main/File[/tmp/path2/deleteme]/ensure: removed Notice: /Stage[main]/Main/File[/tmp/path2/folder]/ensure: created Notice: /Stage[main]/Main/File[/tmp/path2/folder/xxx]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e' Notice: /Stage[main]/Main/File[/tmp/path2/bar]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e' Notice: /Stage[main]/Main/File[/tmp/path2/folder2]/ensure: removed Notice: /Stage[main]/Main/File[/tmp/path2/foo]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e' Notice: Finished catalog run in 0.06 seconds [root@localhost vagrant]# diff -r /tmp/path1 /tmp/path2 [root@localhost vagrant]# 

这样的工作。
请注意,您也需要force => true,
否则,目标path中的现有文件夹将不会被删除。

或者像这样的东西也将工作:

 rsync::get { '/backup/foo': source => '/foo', purge => true, }