6

I was using Puppet 3.2.2 and Hiera 1.2.1. And I wrote a puppet module and the content of init.pp was:

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:}
}

And the hieradata file was:

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"

I always got a error message When puppet agent run

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

It seems $common_instances can be got correctly, but $commono_instanceconfig always be treated as a string.

I used YAML.load_file to load the hieradata file, and got a correct hash object.

Can anybody help?

txworking
  • 479
  • 2
  • 6
  • 15
  • Which line is line 16? – Shane Madden Jun 30 '13 at 01:54
  • Hi, @ShaneMadden. I update the question, so former line 16 was `path => $common_instanceconfig[$title]['path'],`. – txworking Jul 01 '13 at 02:12
  • @ShaneMadden Finally I found this was a variable scope problem. In the scope of define type `instances` `$common_instanceconfig` could not be accessed. So there should be a parameter to do it. – txworking Jul 01 '13 at 06:47

2 Answers2

4

Finally I found this was a variable scope problem, the correct init.pp should be:

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
  }

}
txworking
  • 479
  • 2
  • 6
  • 15
4

Even I faced the same problem with nested hashes. I had to write code to make nginx vhosts.This is my YAML file

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"

And this is my manifest

$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 is the variable that gets values from array, and those values are being used to access the hashes.You can find more info about $name variable here http://docs.puppetlabs.com/puppet/2.7/reference/lang_defined_types.html

EquinoX
  • 141
  • 5