【问题标题】:puppet: iterate over hash which has arraypuppet:遍历具有数组的哈希
【发布时间】:2022-01-19 07:54:57
【问题描述】:

我尝试通过 puppet 生成 nginx 配置。我有这样的hieradata

profile::candy::virtualhosts:
  abc.example.com:
    listen: '80'
    location: [v1, v2]
    backend_port: [3000, 3001]

我可以使用此代码生成服务器配置

$virtualhosts.each |$virtualhost, $opts| {

nginx::resource::server { "${virtualhost}":
  listen_port => Stdlib::Port($opts[listen]),
  ssl         => $ssl,
  ssl_cert          => if $ssl { "/etc/nginx/certs/abc.pem" },
  ssl_key           => if $ssl { "/etc/nginx/certs/abc.key" },
  ssl_redirect      => $ssl,
}
}

但我想为该虚拟主机创建 nginx 位置,这是手动工作的代码

nginx::resource::location { "/v1" :
  ensure => present,
  ssl             => true,
  ssl_only        => true,
  location => '/v1/',
  server => 'abc.example.com',
  index_files => [],
  proxy               => "http://127.0.0.1:4000/",
  proxy_set_header    => [ 'Upgrade $http_upgrade', "Connection 'upgrade'", 'Host $host' ],
  proxy_http_version  => '1.1',
}

无法理解如何从 hiera 值中对其进行迭代

【问题讨论】:

  • 您在寻找$opts['location'].each吗?

标签: puppet


【解决方案1】:

修复了我的问题,我使用了 2 个循环外循环,我将 Hash 传递给它,并在第二个循环中使用每个关键字扫描所有位置

还将我的hiera更改为

profile::candy::virtualhosts:
  abc.example.com:
    v1: '3000'
    v2: '3001'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2016-09-03
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多