代码之家  ›  专栏  ›  技术社区  ›  xit

Puppet-在哈希上迭代时,如果hiera中不存在,则在清单中设置默认值

  •  3
  • xit  · 技术社区  · 7 年前

    我正在迭代hiera哈希中的许多条目,并希望通过在清单中设置默认值(例如 ensure , groups , managehome 如果hiera中存在重复的键/值对,则覆盖默认值。

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

    我尝试在查找和其他方法中设置“default\u values\u hash”,但似乎没有将默认值传递到迭代和调试输出中

    这是我的清单和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
    

    在希拉:

    test::hash:
    
    'fred':
      name:         fred
      password:     somepassword
      groups:       wheel
      managehome:   true
    
    'mary':
      name:         mary
      password:     otherpassword
    
    'john':
      name:         john
      password:     anotherpassword
    
    'harry':
    

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

    1 回复  |  直到 7 年前
        1
  •  3
  •   Matthew Schuchard    7 年前

    这个 lookup 函数使您能够为哈希本身建立默认值,但实际上不能为哈希中的键和值建立默认值。此外,使用 查找 Puppet中作为类参数值的函数将被自动参数绑定取代。换句话说,您的 查找 此上下文中的函数不起任何作用,因为Puppet更喜欢自动参数绑定,而您正在使用这些绑定(对于 查找 结合Hiera 5,您似乎正在使用)。我个人觉得这种行为很烦人,但事实就是这样。编辑:别在意那个具体的推理;调用该参数 $testhash 而不是 $hash . 如果该参数来自模块数据,则 查找 仍将被忽略,因为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 (木偶 )对于 $value 在迭代过程中。这将确保 * 并以所有默认值为准。

    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,
          ;
        }
      }
    }