【问题标题】:How to pass array as parameter to puppet classes如何将数组作为参数传递给木偶类
【发布时间】:2015-10-06 17:04:57
【问题描述】:

如何将参数传递给 puppet 类。 例如,我有这样的代码:

class testlogging {
  file { '/home/amo/testloggingfile':
    ensure => 'present',
    source => 'puppet:///modules/testlogging/testlogging',
  }
}

我想要做的是将文件名/路径的数组作为参数传递给这个类。

【问题讨论】:

    标签: puppet


    【解决方案1】:

    John Bollinger 的回答是推荐的。如果你想不用 Hiera yaml 后端,你可以写一个define 类型

    class myclass {
     # Create a define type, that will take $name as the file
     # You provide $src for each file
     define mydefine ($src) {
       file { $name :
         ensure => present,
         source => "puppet:///modules/<mymodule>/$src",
       }
      }
    
      # Use two arrarys, one for file names and other for source
      $filearray=["/path/to/file1","/path/to/file2"]
      $filesrc=["source1","source2"]
    
      # Loop over the array
      each($filearray) | $index, $value | {
       mydefine{ $value :
          src => $filesrc[$index],
        }
     }
    

    【讨论】:

      【解决方案2】:

      您确实应该熟悉文档,尤其是Language Reference。它的section on classes 讨论了如何定义您的类以使其接受参数,以及如何指定所需的参数值。

      不过,简而言之,模块 mymodule 中接受必需参数的类 myclass 的定义采用以下形式:

      class mymodule::myclass($param) {
          # ...
      }
      

      您通常会通过自动数据绑定将值绑定到该类的参数,对于所有意图和目的而言,这意味着 Hiera。要为 Hiera 的默认 yaml 后端指定要处理的数组值,数据将包含以下内容:

      ---
      mymodule::myclass::param:
        - '/path/to/file1'
        - '/path/to/file2'
      

      关于如何配置 Puppet 和 Hiera 的完整说明对于本论坛来说过于宽泛。

      【讨论】:

        猜你喜欢
        • 2020-03-06
        • 1970-01-01
        • 2020-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 2020-05-11
        • 1970-01-01
        • 2018-12-11
        相关资源
        最近更新 更多