Puppet&Hiera: $variable не является хешем или массивом при доступе к нему

2 ответа

Наконец, я обнаружил, что это проблема с переменной областью видимости, правильный 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
  }

}
3
ответ дан 3 December 2019 в 00:25

Даже я столкнулся с той же проблемой с вложенными хэшами. Мне пришлось написать код для создания 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

4
ответ дан 3 December 2019 в 00:25

Теги

Похожие вопросы