Ask Your Question
0

fully-qualified variable fail with error '...is not a hash or array when accessing it with class_name'

asked 2017-06-28 18:57:41 -0500

mazdajai gravatar image

updated 2017-06-29 10:25:37 -0500

DarylW gravatar image

Hello,

I have the following modules and hiera and failed with error '...is not a hash or array when accessing it with class_name' when using fully-qualified variable $foo::config::writer. If I use just $writer, it will be fine.

I think this is a scoping issue but I am not sure where is it, any thoughts?

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: foo::config::writer is not a hash or array when accessing it with class_name at /etc/puppet/environments/production/modules/foo/manifests/config.pp:10 on node example.com Warning: Not using cache on failed catalog Error: Could not retrieve catalog; skipping run

init.pp
     ----
    class foo {
      include foo::params


  case $foo::params::application {
    'bar': {
      foo::config { 'bar':
        query_array         => [
          $foo::metrics::default_metrics,
          $foo::metrics::bar_metrics],
      }
    }
    default: {
      foo::config { 'default':
        query_array => $foo::metrics::default_metrics,
        writer      => $foo::params::writer,
        interval    => '60',
      }
    }
  }
}

config.pp
---
define foo::config (
  $writer   = hiera_hash('foo::writer'),
  $interval = hiera('foo::interval', '120'),
  ) {
  include foo

  validate_hash($writer)

  $output = {
    class      => $foo::config::writer['class'],
    attributes => $foo::config::writer['attributes'],
  }
}

node.yaml
---
foo::queries: ''
foo::writer:
    class: 'class2'
    attributes:
      fileName: '/tmp/zoo'
      showTimeStamp: 'false'
foo::interval: '60'
edit retag flag offensive close merge delete

Comments

Wouldn't you call the vairable just by isuing $writer. I think maybe the problmem is that it tri's to look up by hiera and will look for the $foo::config::writer but under your yaml you only have foo::writer?

negast gravatar imagenegast ( 2017-06-29 00:42:41 -0500 )edit

As far as I know, line 10 is not doing a lookup it should just accessing the array defined on line 2...?

mazdajai gravatar imagemazdajai ( 2017-06-30 10:18:12 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-07-03 08:39:31 -0500

Henrik Lindberg gravatar image

You cannot access a parameter of a user defined resource type like that. The variable $foo::config::writer is not a reference to the parameter $writer for the define foo::config. It is most likely undef, and if you had --strict_variables turned on, puppet would error.

Try to make the initialization cleaner by using a class and automatic parameter lookup (APL). Use a class as the entry point - include it and set its parameters using APL. Make it use different defines based on conditional logic. It is much easier to reason about the logic when values are given to classes/functions/etc than when they are trying to pull in values. Also avoid using variable references to variables inside if classes (that is also pulling in a value). The "pull-in" style will make you end up with a large spaghetti mess.

If you are on puppet 4 or 5, use hiera 5 and place the default values in hiera in your module.

edit flag offensive delete link more

Comments

I think I am still a bit confused because `$writer` works. I thought this is a scoping issue - `$foo::config::writer` is global scope while `$writer` is local scope. Am I missing something? https://docs.puppet.com/puppet/2.7/scope_and_puppet.html#qualify-your-variables

mazdajai gravatar imagemazdajai ( 2017-07-03 15:59:44 -0500 )edit

>Also avoid using variable references to variables inside if classes (that is also pulling in a value) We are on puppet 3.8.7 and we have been been using 'this' to pull hiera, is there documentation to better do this? Thanks!

mazdajai gravatar imagemazdajai ( 2017-07-03 16:01:32 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-06-28 18:57:41 -0500

Seen: 48 times

Last updated: Jul 03