0

我想知道如何解决以下 puppet 问题:我想基于字符串数组创建几个文件。复杂之处在于我想用文件创建多个目录:

dir1/
    fileA
    fileB
dir2/
    fileA
    fileB
    fileC

问题是文件资源标题必须是唯一的。因此,如果我将文件名保存在数组中,我需要以自定义方式迭代数组,以便能够使用目录名对文件名进行后缀:

$file_names = ['fileA', 'fileB']
$file_names_2 = [$file_names, 'fileC']

file {'dir1': ensure => directory }
file {'dir2': ensure => directory }

file { $file_names: path = 'dir1', ensure =>present, }
file { $file_names_2: path = 'dir2', ensure =>present, }

这不起作用,因为文件资源标题冲突。所以我需要将例如目录名称附加到文件标题,但是,这将导致文件数组被连接并且不被视为多个文件......

啊。。

file { "${file_names}-dir1": path = 'dir1', ensure =>present, }
file { "${file_names_2}-dir2": path = 'dir1', ensure =>present, }

如何在不需要重复文件资源本身的情况下解决这个问题。谢谢

4

1 回答 1

0

我试过这个。您可以以此为例,根据您的具体需求进行修改

class common_setup::test {                                                                                                                                                             

   file {'/var/tmp/dir1': ensure => directory }                                                                                                                                         
   file {'/var/tmp/dir2': ensure => directory }                                                                                                                                         

   $file_names = ['fileA', 'fileB']                                                                                                                                                     
   $file_names_2 = unique(flatten([$file_names, 'fileC']))                                                                                                                              
   common_setup::makeDirs { $file_names_2 :}                                                                                                                                            
}                                                                                                                                                                                      

define common_setup::makeDirs {                                                                                                                                                        
   include common_setup::test                                                                                                                                                           

   if ($name in $file_names) and ($name in $file_names_2) {                                                                                                                             
      file { "/var/tmp/dir1/$name" :                                                                                                                                                     
        ensure => present,                                                                                                                                                               
      }                                                                                                                                                                                  

      file {"/var/tmp/dir2/$name" :                                                                                                                                                      
        ensure => present,                                                                                                                                                               
      }                                                                                                                                                                                  
   }                                                                                                                                                                                    
   else {                                                                                                                                                                               
     file {"/var/tmp/dir2/$name" :                                                                                                                                                      
       ensure => present,                                                                                                                                                               
     }                                                                                                                                                                                  
   }                                                                                                                                                                                    
}                 

测试成功:

notice: /Stage[main]/Common_setup::Test/File[/var/tmp/dir2]/ensure: created
notice:   /Stage[main]/Common_setup::Test/Common_setup::Makedirs[fileA]/File[/var/tmp/dir2/fileA]/ensure: created
notice: /Stage[main]/Common_setup::Test/Common_setup::Makedirs[fileC]/File[/var/tmp/dir2/fileC]/ensure: created
notice: /Stage[main]/Common_setup::Test/Common_setup::Makedirs[fileB]/File[/var/tmp/dir2/fileB]/ensure: created
notice: /Stage[main]/Common_setup::Test/File[/var/tmp/dir1]/ensure: created
notice: /Stage[main]/Common_setup::Test/Common_setup::Makedirs[fileB]/File[/var/tmp/dir1/fileB]/ensure: created
notice: /Stage[main]/Common_setup::Test/Common_setup::Makedirs[fileA]/File[/var/tmp/dir1/fileA]/ensure: created
notice: Finished catalog run in 1.97 seconds

并创建目录和文件:

ls /var/tmp/dir*/
/var/tmp/dir1/:
fileA  fileB

/var/tmp/dir2/:
fileA  fileB  fileC
于 2012-10-26T13:32:07.893 回答