Puppet - 当在hash

时间:2017-12-21 10:13:53

标签: iteration puppet hiera defaults

我正在迭代hiera散列中的许多条目,并希望通过在清单中设置默认值来删除hiera中相同的重复行(例如ensuregroupsmanagehome等),并且如果hiera中存在重复的键/值对,则覆盖默认值。

到目前为止,我尝试过的所有内容都无法获得默认值。我知道我需要声明一个资源,但我不确定。

我尝试过设置" default_values_hash"在查找和其他方法中,似乎没有任何东西将默认值传递给迭代和--debug输出

这是我的清单和hiera数据的(伪)示例。任何指导都真诚地感谢。谢谢。

class test (
  Hash $testhash = lookup('test::hash', "merge" => 'hash'}),
){

  $testhash.each |$key, $value| {
    user { $key :
      ensure     => $value['ensure'],
      name       => $value['name'],
      password   => $value['password'],
      groups     => $value['groups'],
      managehome => $value['managehome'],
    }
  }
}
include class test

在Hiera:

test::hash:

'fred':
  name:         fred
  password:     somepassword
  groups:       wheel
  managehome:   true

'mary':
  name:         mary
  password:     otherpassword

'john':
  name:         john
  password:     anotherpassword

'harry':

在清单中设置资源默认值(使用大写的User)不会传递到迭代中,但如果我将所有数据保留在清单中(这样做的数据太多)。

1 个答案:

答案 0 :(得分:3)

lookup函数使您能够为散列本身建立默认值,但不能真正用于散列内的键和值。此外,在Puppet中使用lookup函数作为类参数值将被自动参数绑定取代。换句话说,这个上下文中的lookup函数什么都不做,因为Puppet更喜欢自动参数绑定,而你正在使用它们(对于lookup和Hiera 5一样,这肯定是正确的看来你正在使用)。我个人觉得这种行为很烦人,但事实就是如此。编辑:没关系那个具体的推理;该参数称为$testhash而不是$hash。如果该参数来自模块数据,那么lookup仍然会被忽略,因为Puppet只允许模块数据的自动参数绑定。

我很惊讶资源默认设置不适合您。我不得不相信这是无意的,或者当你走这条路时,某些事情被误解了。

无论如何,这是一种有保障的方法。首先,我们实现每个表达式的默认属性:https://puppet.com/docs/puppet/5.3/lang_resources_advanced.html#per-expression-default-attributes

class test (
  Hash $testhash = lookup('test::hash', "merge" => 'hash'}),
){

  $testhash.each |String $key, Hash $value| {
    user { 
      default:
        ensure     => present,
        name       => 'username',
        password   => 'userpassword',
        groups     => ['usergroups'],
        managehome => false,
      ;
      $key:
        ensure     => $value['ensure'],
        name       => $value['name'],
        password   => $value['password'],
        groups     => $value['groups'],
        managehome => $value['managehome'],
      ;
    }
  }
}

但是仍然存在一个问题,即您正在为所有迭代建立第二个块中属性的值。如果这些键值对未定义,Puppet将错误而不是默认为另一个值。如果您首先为它们定义了值,我们需要指示Puppet仅为属性建立值。值得庆幸的是,我们可以使用*属性执行此操作:https://puppet.com/docs/puppet/5.3/lang_resources_advanced.html#setting-attributes-from-a-hash

class test (
  Hash $testhash = lookup('test::hash', "merge" => 'hash'}),
){

  $testhash.each |String $key, Hash $value| {
    user { 
      default:
        ensure     => present,
        name       => 'username',
        password   => 'userpassword',
        groups     => ['usergroups'],
        managehome => false,
      ;
      $key:
        * => $value,
      ;
    }
  }
}

这里的一个建议是让你的lambda迭代器变量$key, $value更透明地命名。另一个注意事项是,要使*属性起作用,您的哈希键必须与Puppet属性名称匹配。

更新的问题:在散列具有nil值的键的情况下,您可以将空散列设置为lambda迭代器参数中键值的默认值。在该迭代期间,空哈希将替换undef的{​​{1}}(Puppet nil)。这将确保$value运算符中不包含任何属性和值,并且所有默认值都将占优势。

*