Puppet&Hiera:访问 $variable 时,它​​不是哈希或数组

Puppet&Hiera:访问 $variable 时,它​​不是哈希或数组

我使用的是 Puppet 3.2.2 和 Hiera 1.2.1。我编写了一个 puppet 模块,init.pp 的内容如下:

class install(
  $common_instanceconfig = hiera_hash('common_instanceconfig'),
  $common_instances = hiera('common_instances')
)
{

  define instances {

    common { $title:
      name       => $title,
      path       => $common_instanceconfig[$title]['path'],
      version    => $common_instanceconfig[$title]['version'],
      files      => $common_instanceconfig[$title]['files'],
      pre        => $common_instanceconfig[$title]['pre'],
      after      => $common_instanceconfig[$title]['after'],
      properties => $common_instanceconfig[$title]['properties'],
      require    => $common_instanceconfig[$title]['require'] ,

    }
  }

  instances {$common_instances:}
}

hieradata文件如下:

classes:
  - install

common_instances:
  - common_instance_1
  - common_instance_2

common_instanceconfig:
  common_instance_1
    path      : '/opt/common_instance_1'
    version   : 1.0
    files     : software-1.bin
    pre       : pre_install.sh
    after     : after_install.sh
    properties: "properties"

common_instance_2:
    path      : '/opt/common_instance_2'
    version   : 2.0
    files     : software-2.bin
    pre       : pre_install.sh
    after     : after_install.sh
    properties: "properties"

当 Puppet Agent 运行时我总是收到一条错误消息

Error: common_instanceconfig String is not an hash or array when accessing it with common_instance_1 at /etc/puppet/modules/install/manifests/init.pp:16 on node puppet.agent1.tmp

看似$common_instances可以正确获取,但$commono_instanceconfig总是被视为字符串。

我曾经YAML.load_file加载 hieradata 文件,并得到了一个正确的哈希对象。

有人可以帮忙吗?

答案1

最后我发现这是一个变量作用域的问题,正确的init.pp应该是:

class install(
  $common_instances = hiera_array('common_instances'),
  $common_instanceconfig = hiera_array('common_instanceconfigs'),
) 
{
  define instances (
  $common_instanceconfig
  ) {   

  common { $common_instances[0]: 
    name       => $title,
    path       => $common_instanceconfig[0],
    version    => $common_instanceconfig[1],
    files      => $common_instanceconfig[2],
    pre        => $common_instanceconfig[3],
    after      => $common_instanceconfig[4],
    properties => $common_instanceconfig[5],
    require    => $common_instanceconfig[$title]['require'] ,
   }   
  }

  instances {$common_instances: 
    common_instanceconfig => $common_instanceconfig
  }

}

答案2

甚至我也遇到了嵌套哈希的相同问题。我不得不编写代码来创建 nginx vhosts。这是我的 YAML 文件

vhost_array:
 - host1
 - host2

vhost_hash:
  host1:
        hostname: "one"
        docroot: "england"
        php_fpm: 2097
        db_name: "noidea"
        db_passwd: "sheeit"

  host2:
        hostname: "two"
        docroot: "nagaland"
        php_fpm: 3000
        db_name: "awesm"
        db_passwd: "pisss"

这是我的清单

$vhost_array = hiera_array("vhost_array")

define hash_extract(){
                $vhost_hash = hiera_hash("vhost_hash")
                $vhost = $vhost_hash[$name]     ## TRICK lies in $name variable
                notice($vhost['hostname'])
                notice($vhost['docroot'])
                notice($vhost['php_fpm'])
                notice($vhost['db_name'])
                notice($vhost['db_passwd'])
}

hash_extract{$vhost_array:}

$name 是从数组中获取值的变量,这些值用于访问哈希值。您可以在此处找到有关 $name 变量的更多信息http://docs.puppetlabs.com/puppet/2.7/reference/lang_defined_types.html

相关内容