人偶嵌套资源create_resources,无法将字符串转换为哈希

时间:2018-09-01 17:51:27

标签: puppet

尝试使用以下模块构建DNS:ref。但是出现此错误:

Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, can't convert String into Hash.

我嵌套了YAML,但是不确定它的格式是否正确,或者代码中的其他问题。 这是我的dns个人资料dns.pp:

class profile::bind {

  validate_hash($conf)
  $conf   = hiera_hash('bind::zone', undef)
  create_resources('profile::bind::make::zone', $conf)

}

这是我使用make_zone.pp定义区域的方式:

define profile::bind::make::zone (
  $hash_data,
  $zone,
  $ensure,
  $zone_contact,
  $zone_ns,
  $zone_serial,
  $zone_ttl,
  $zone_origin,
) {

  validate_hash($hash_data)

  bind::zone { $zone :
    ensure       => $ensure,
    zone_contact => $zone_contact,
    zone_ns      => [$zone_ns],
    zone_serial  => $zone_serial,
    zone_ttl     => $zone_ttl,
    zone_origin  => $zone_origin,
  }
}

这是我的host1.yaml数据:

---
version: 5

bind::zone:
  zone: test.ltd
  ensure: present
  zone_contact: 'contact.test.ltd'
  zone_ns: 
    -'ns0.test.ltd'
    -'ns1.test.ltd'
  zone_serial: '2018010101'
  zone_ttl: '767200'
  zone_origin: 'test.ltd'
  hash_data:
    "newyork":
      owner: "11.22.33.44"
    "tokyo":
      owner: "22.33.44.55"
    "london":
      owner: "33.44.55.66"

bind::cname:
  ensure: present
  record_type: master

1 个答案:

答案 0 :(得分:2)

代码中存在许多错误和误解。我对其进行了修复,以使代码至少可以编译并最终完成。

更改为profile :: bind:

class profile::bind {
  include bind
  $conf = lookup('bind::zone')
  create_resources(profile::bind::make::zone, $conf)
}

更改为profile :: bind :: make :: zone:

define profile::bind::make::zone (
  Enum['present','absent'] $ensure,
  String         $zone_contact,
  Array[String]  $zone_ns, 
  String         $zone_serial,
  String         $zone_ttl,
  String         $zone_origin,
  Hash[String, Hash[String, String]] $hash_data,
) {
  bind::zone { $name:
    ensure       => $ensure,
    zone_contact => $zone_contact,
    zone_ns      => $zone_ns,
    zone_serial  => $zone_serial,
    zone_ttl     => $zone_ttl,
    zone_origin  => $zone_origin,
  }
}

更改为host1.yaml:

---
bind::zone:
  'test.ltd':
    ensure: present
    zone_contact: 'contact.test.ltd'
    zone_ns:
      - 'ns0.test.ltd'
      - 'ns1.test.ltd'
    zone_serial: '2018010101'
    zone_ttl: '767200'
    zone_origin: 'test.ltd'
    hash_data:
      "newyork":
        owner: "11.22.33.44"
      "tokyo":
        owner: "22.33.44.55"
      "london":
        owner: "33.44.55.66"

一些解释:

直接问题

Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, can't convert String into Hash.

此错误是由于您的Hiera数据未正确构造为Hash[String, Hash[String, String]]而引起的。请注意,在Yaml中,我已删除了关键的“区域”,并在那里创建了一个嵌套的哈希。

必须包括绑定类

camptocamp BIND模块要求还声明绑定类。请参阅其文档。

validate_hash函数是旧函数,并且放置在错误的位置

正如约翰·布林格(John Bollinger)在评论中提到的那样,您在错误的行上加上了validate_hash。我认为这是一个剪切/粘贴问题,因为如果这确实是您的代码,那么您将收到一条不同的错误消息。无论如何,由于您正在使用Puppet 5(我想您的Hiera中的版本=> 5),所以不要使用传统的validate函数;使用Puppet的数据类型验证。所以我刚刚删除了这一行。

使用lookup()代替hiera_hash()

同样,由于您正在使用Puppet 5,请使用lookup()函数而不是不推荐使用的hiera_hash()函数。

版本5属于hiera.yaml,而不是host1.yaml

这不会给您带来任何问题,但是version: 5行在这里不会做任何事情,它属于您的hiera.yaml文件。我使用了如下的hiera.yaml文件进行测试:

---
version: 5
defaults:
  datadir: data
  data_hash: yaml_data
hierarchy:
  - name: "Host 1"
    paths:
    - host1.yaml

zone_ns类型混淆

您在zone_ns上遇到两个问题-首先,您的YAML中有一个错字(-之后没有空格);其次,您传入了一个区域NS的数组,然后尝试将该数组强制为您定义类型的数组。

区域参数应为名称var

注意,我必须删除您定义的类型中的$zone参数,而改用特殊的$name变量来从标题中获取名称。

重构为使用数据类型验证

请注意,我是如何在定义的类型上对输入使用Puppet的数据类型验证的,那么我就不再需要传统的validate_hash函数和其他相关的验证函数。详细了解该here

我认为仅此而已。希望有帮助!

相关问题