遍历木偶清单中深层嵌套的hiera哈希 [英] Iterate over a deeply nested hiera hash in puppet manifest

查看:107
本文介绍了遍历木偶清单中深层嵌套的hiera哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Web服务器构建结构.我在hiera中安装了设备,但似乎无法让人偶提供正确的类型.

I'm working on building a structure for my webservers. I have my setup in hiera, but I can't seem to get puppet to give back the correct types.

common.yaml

In common.yaml

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

我每个虚拟主机有多个站点,并且有多个虚拟主机.在清单中,我将创建文件夹结构和其他设置任务,但就目前而言,我什至无法在网站上进行迭代.

I have multiple sites per vhost and multiple vhosts. In my manifest I'm going to be creating the folder structure and other setup tasks, but for for now I can't even get it to iterate over the sites.

当前清单:

define application($app_name, $app_url) {
  notice("App Type: ${app_type}")
  notice("App Url: ${app_url}")
}

$vhosts = hiera('vhosts')

$vhosts.each |$vhost| {
  create_resources(application, $vhost)
}

我得到的错误是create_resources需要哈希.但是,如果我键入强制转换$vhost,我得到的不是哈希,而是元组.

The error I get is that create_resources requires a Hash. However, if I type cast $vhost I get that it's not a Hash but a Tuple.

我如何从Yaml哈希中获取元组?有没有更好的方法可以遍历此数据集以获取所需的数据?

How did I get a Tuple out of my yaml hash? Is there a better way to iterate over this data set to get what I need?

推荐答案

https://docs.puppet.com/puppet/latest/reference/function.html#each .

给出一个Hiera哈希,例如:

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']

这将导致value.

如果您尝试执行create_resources(),那么您可能希望将散列构造为资源散列的散列.例如,希拉(Hiera):

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

与木偶:

create_resources(hiera_hash('packages'))

这篇关于遍历木偶清单中深层嵌套的hiera哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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