Puppet-遍历哈希时在hiera中不存在的情况下在清单中设置默认值 [英] Puppet - set defaults in manifest if not present in hiera when iterating over hash

查看:158
本文介绍了Puppet-遍历哈希时在hiera中不存在的情况下在清单中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历hiera哈希中的许多条目,并希望通过在清单中设置默认值(例如ensuregroupsmanagehome等)从hiera中删除相同的重复行,并具有默认值如果hiera中存在重复的键/值对,则覆盖.

I am iterating over many entries in a hiera hash, and wish to remove identical duplicate lines from hiera by setting defaults in the manifest (such as ensure, groups, managehome etc), and have the defaults overridden IF the duplicate key/value pair exists in hiera.

到目前为止,我尝试过的所有操作均未获得默认值.我的想法是我需要声明资源,但是不确定.

To date, everything I have tried fails to get the default values. I get the idea that I need to declare a resource, but am uncertain.

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

I have tried setting "default_values_hash" in the lookup and other methods, but nothing appears to pass defaults into the iteration and --debug output

这是清单和工资单数据的(伪)示例.真诚地感谢任何指导.谢谢.

This is a (pseudo) example of my manifest and hiera data. Any guidance is sincerely appreciated. Thank you.

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)不会传递到迭代中,尽管如果我将所有数据都保留在清单中(这样做的数据太多),也会这样做.

Setting resource defaults in the manifest (with capitalized User) does not pass into the iteration, though it does if I keep all data in the manifest (too much data to do that).

推荐答案

lookup函数使您能够为哈希自身建立默认值,但不能为哈希内部的键和值建立默认值.此外,在Puppet中将lookup函数用作类参数值将被自动参数绑定取代.换句话说,在这种情况下,您的lookup函数没有任何作用,因为Puppet首选自动参数绑定,而您正在使用这些参数(对于lookup与Hiera 5结合使用,这确实是正确的,看来您正在使用它) ).我个人觉得这种行为很烦人,但事实就是如此.不要介意那种特定的推理;该参数称为$testhash而不是$hash.但是,如果该参数来自模块数据,则lookup仍将被忽略,因为Puppet仅允许对模块数据进行自动参数绑定.

The lookup function gives you the ability to establish a default value for the hash itself, but not really for the keys and values inside the hash. Additionally, using the lookup function as a class parameter value in Puppet will be superseded by automatic parameter bindings. In other words, your lookup function in this context does nothing because Puppet is preferring automatic parameter bindings over it, and you are using those (this is definitely true for lookup in conjunction with Hiera 5, which it appears you are using). I personally find this behavior annoying, but it is what it is. Never mind on that specific reasoning; the parameter is called $testhash and not $hash. If that parameter is coming from module data though, then the lookup will still be ignored since Puppet only allows automatic parameter binding for module data.

我很惊讶资源默认设置在这里不适合您.我必须相信,这要么是意料之外的,要么是当您走那条路线时,关于它们的实现有误.

I am surprised that the resource defaults are not working here for you. I have to believe that this is either unintended, or something was implemented wrong regarding them when you went that route.

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

Regardless, here is a guaranteed method for you. First, we implement per-expression default attributes: 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仅为属性建立值.幸运的是,我们可以使用*属性来做到这一点:

One problem still remains here though, which is that you are establishing values for the attributes in the second block for all iterations. If those key-value pairs are undefined, Puppet will error instead of defaulting to another value. We need to instruct Puppet to only establish values for attributes if you have defined a value for them in the first place. Thankfully, we can do this with the * attribute: 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属性名称匹配.

One recommendation here would be to make your lambda iterator variables $key, $value a bit more transparently named. Another note is the caveat that for the * attribute to work, your hash keys must match Puppet attribute names.

更新的问题:在散列包含具有nil值的键的情况下,可以在lambda迭代器参数中将空散列设置为键值的默认值.在该迭代期间,空散列将替换undef(人偶nil)为$value.这样可以确保*运算符中不包含任何属性和值,并且所有默认设置都将适用.

Updated question: In the situation where the hash has a key with a nil value, you can set an empty hash as the default value for the key's value in the lambda iterator parameters. The empty hash will replace the undef (Puppet nil) for the $value during that iteration. This will ensure that no attributes and values are included in the * operator and that all the defaults will prevail.

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

这篇关于Puppet-遍历哈希时在hiera中不存在的情况下在清单中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆