遍历木偶中的一系列哈希

时间:2018-11-19 11:35:13

标签: arrays hash puppet

我的人偶中有hiera数据,如下所示:

elasticsearch::cluster_name: 'elasticsearch-dev'
elasticsearch::masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
elasticsearch::kibanalist: [ "kibanadev01.domain.com" ]

我的清单包含以下内容:

  $masterlist = hiera('elasticsearch::masterlist')
  $kibanalist = hiera('elasticsearch::kibanalist')

  if ( $::fqdn in $masterlist ) or ( $::fqdn in $kibanalist ) {
    $cluster_name = hiera('elasticsearch::cluster_name')
  }

  else {
    notify { 'No cluster for node':
      message => "${::fqdn} is not configured to be in any cluster in the hiera data",
    }
  }

我想对此进行修改,以允许为单个环境配置多个集群,但我不确定实现此目标的最佳方法。我希望能够将主列表与它们相应的群集名称进行分组,然后为每个节点调用相关的群集名称。

我认为也许我需要使用一组哈希值并对其进行迭代,但是我不知道如何在清单中覆盖它。

1 个答案:

答案 0 :(得分:0)

您可能希望将数据重组为哈希散列:

elasticsearch::clusters:
  'elasticsearch-dev':
    masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
    kibanalist: [ "kibanadev01.domain.com" ]

然后在您的清单中显示

$clusters = hiera('elasticsearch::clusters')

$mycluster = $clusters.filter |$cluster, $data| {
  ($::fqdn in $data['masterlist']) or ($::fqdn in $data['kibanalist'])
}

if ($mycluster.empty) {
  notify { 'No cluster for node':
    message => "${::fqdn} is not configured to be in any cluster in the hiera data",
  }
}

$cluster_name = $mycluster.keys[0]
notice($cluster_name)

您可能还需要考虑将已弃用的hiera()调用替换为lookup(),并使用$facts['networking']['fqdn']而不是旧版$::fqdn