如何在木偶的 if 语句中使用列表?

How to use list in an if statement in puppet?

class am_rgw_profile::am_rgw_ports {
  firewalld::custom_service{'am_rgw_ports':
    short       => 'am_rgw_ports',
    description => 'IIQ IaaS gateway wg6561',
    port        => [
    if ('xarsiiq1xd' in $hostname) {  [
        {
            'port'     => '2470',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2472',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2474',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2476',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2478',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2480',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2482',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2484',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2486',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2490',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2492',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2494',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2496',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2498',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2500',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2502',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2504',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2506',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2508',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2510',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2512',
            'protocol' => 'tcp',
        },
        {
            'port'     => '2514',
            'protocol' => 'tcp',
        },
      ]
    }elsif ('xarsiiq1xe' in $hostname) { [
      {
            'port'     => '2492',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2516',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2518',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2520',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2522',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2524',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2526',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2528',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2530',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2532',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2534',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2536',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2538',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2540',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2542',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2544',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2546',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2548',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2550',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2552',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2554',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2556',
            'protocol' => 'tcp',
      },
      {
            'port'     => '2558',
            'protocol' => 'tcp',
      },
      ]
    }else {
      {
            'port'     => '2492',
            'protocol' => 'tcp',
      }
    }
  ]
  }
  firewalld_service { 'Allow am_racf_ports services for IIQ gateway servers':
    ensure  => 'present',
    service => 'am_rgw_ports',
    zone    => 'Cali'
  }
}

当我的代码进入 if 条件时,这是我得到的错误:

错误:无法从远程服务器检索目录:服务器上的错误 500:服务器错误:评估错误:评估资源语句时出错,评估错误:评估函数调用时出错,无法解析模板 firewalld/service.xml.erb: 文件路径:org/jruby/RubyArray.java 线路:1489 细节:没有将 String 隐式转换为 Integer (文件:/etc/puppetlabs/code/common_modules/modules/firewalld/manifests/custom_service.pp,行:66,列:16)(文件:/etc/puppetlabs/code/environments/community_am_racf_gateway/profiles/am_rgw_profile/manifests/am_rgw_ports.pp,行:2)在节点 xarsiiq1xd.opr.test.zone.org 上 警告:不在失败的目录上使用缓存 错误:无法检索目录;跳过 运行

我做错了什么?

错误消息可能有点令人困惑,但您尝试使用的语法完全不正确。

您似乎想做的是这样的事情:

class am_rgw_profile::am_rgw_ports {
  if ('xarsiiq1xd' in $hostname) {
    $port = [
      {
        'port'     => '2470',
        'protocol' => 'tcp',
      },
      {
        'port'     => '2472',
        'protocol' => 'tcp',
      }, # etc
    ]
  } elsif ('xarsiiq1xe' in $hostname) {
    $port = [
      {
        'port'     => '2492',
        'protocol' => 'tcp',
      },
      {
        'port'     => '2516',
        'protocol' => 'tcp',
      },
    ]
  }
  firewalld::custom_service { 'am_rgw_ports':
    short       => 'am_rgw_ports',
    description => 'IIQ IaaS gateway wg6561',
    port        => $port,
  }
  firewalld_service { 'Allow am_racf_ports services for IIQ gateway servers':
    ensure  => 'present',
    service => 'am_rgw_ports',
    zone    => 'Cali'
  }
}

对 Alex Harvey 的回答进行小幅优化

$port = $facts['hostname'] ? {
  /xarsiiq1xd/ => [
                    {'port' => '2470', 'protocol' => 'tcp'},
                    {'port' => '2472', 'protocol' => 'tcp'},
                  ]
  /xarsiiq1xe/ => [
                    {'port' => '2492', 'protocol' => 'tcp'},
                    {'port' => '2516', 'protocol' => 'tcp'},
                  ]
  default =>   
                  [{'port' => '2492', 'protocol' => 'tcp'},]
}
firewalld::custom_service { 'am_rgw_ports':
  short       => 'am_rgw_ports',
  description => 'IIQ IaaS gateway wg6561',
  port        => $port,
}
firewalld_service { 'Allow am_racf_ports services for IIQ gateway servers':
  ensure  => 'present',
  service => 'am_rgw_ports',
  zone    => 'Cali'
}

请注意,您可以将选择器语句内联,而不是先将其分配给端口变量,例如

firewalld::custom_service { 'am_rgw_ports':
  short       => 'am_rgw_ports',
  description => 'IIQ IaaS gateway wg6561',
  port        => $facts['hostname'] ? { /xarsiiq1xd/ => [{...}], /xarsiiq1x3/ => [{...}],}
}

但是长得丑