如何在puppet中获取哈希值的关键字

时间:2014-04-22 09:12:58

标签: configuration puppet

我不知道是否有可能。 我有一个这样的清单:

$some_external_value = 'pew_pew'
$dict = {
    ensure => $ensure,
    configuration => {
         "random_name-${some_external_value}" => {
            command => 'python script.py config/random_name-${some_external_value}.cfg',
         },
         "some_other_name-${some_external_value}" => {
            command => 'python script.py config/some_other_name-${some_external_value}.cfg',
         },
         "without-external" => {
            command => "python script.py config/without-external.cfg",
            user => 'cluster',
         },
      }
}
notice ($dict["configuration"]["some_other_name-${some_external_value}"]["command"])

我得到了

notice: Scope(Class[main]): python script.py config/some_other_name-pew_pew.cfg

是否有一些技巧只能写一次密钥名称,然后才引用它?

"some_other_name-${some_external_value}" => {
     command => 'python script.py config/${wild_magic_variable_pasting_key_here}.cfg',
 },

1 个答案:

答案 0 :(得分:0)

您可以使用custom parser function来实现目标。

$orig_dict = {
  ...
  configuration => {
     "random_name-${some_external_value}" => {
        command => 'python script.py config/__KEY__.cfg',
     },
  ...
}

$dict = keysubst($orig_dict)

... ruby​​函数用递归方式将__KEY__标记替换为相应的键值。