Iterate over a deeply nested hiera hash in puppet manifest

十年热恋 提交于 2019-12-05 19:46:27

Why you had a tuple is explained at https://docs.puppet.com/puppet/latest/reference/function.html#each in the second example.

Given a Hiera hash like:

vhosts:
  hostname:
    sitename:
      app_url: value
      app_type: value

You can iterate over it like the following:

hiera_hash('vhosts').each |String $hostname, Hash $hostname_hash| {
  # $hostname is 'hostname'
  # $hostname_hash is { hostname => { sitename => { app_url => value, app_type => value } } }
  $hostname_hash.each |String $sitename, Hash $sitename_hash| {
    # $sitename is 'sitename'
    # $sitename_hash is { sitename => { app_url => value, app_type => value } }
    $sitename_hash.each |String $key, String $value| {
      # first loop $key is app_url and $value is 'value'
      # second loop $key is app_type and $value is 'value'
    }
  }
}

You can, of course, access hash values at any point like

hiera_hash('vhosts')['hostname']['sitename']['app_url']

which will result in value.

If you are trying to do create_resources(), then you probably want to construct the hash as a hash of resource hashes. For example, Hiera:

packages:
  gcc:
    ensure: installed
  gfortran:
    ensure: absent

with Puppet:

create_resources(hiera_hash('packages'))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!