Ask Your Question

create_resources and hiera_hash (Cannot reassign variable)

asked 2015-04-23 03:49:36 -0600

djsd123 gravatar image

Hi, I keep getting this error when provisioning a vagrant machine.

Error: Could not find data item etc_host in any Hiera data file and no default supplied at /vagrant/...

I have seen various posts about this with the same solution, which is to set an empty hash as the default. However I cannot seem to get this to work with the puppetlabs postgresql module and I now get " **Cannot reassign variable postgres_databases at...." error with the code below.. Help???

    class profiles::postgresql(

  $port     = 5432,
  $version  = '9.3',
  $remote   = true,
  $dbroot   = '/postgres',
  $postgres_databases = 'test',
  $postgres_users = 'test1',


  # Set bind address to if remote is enabled, if not
  # Merge remote into an address array if it's anything other than a boolean
  if $remote == true {
    $bind = '*'
  } elsif $remote == false {
    $bind = ''
  } else {
    $bind_array = delete(any2array($remote),'')
    $bind = join(concat([''],$bind_array),',')

  class { 'postgresql::globals':
    manage_package_repo => true,
    version             => $version,
    datadir             => "${dbroot}/data",
    confdir             => "${dbroot}/data",
    needs_initdb        => true,
    service_name        => 'postgresql', # confirm on ubuntu
    require             => File[$dbroot]
  } ->

  class { 'postgresql::server':
    port                    => $port,
    listen_addresses        => $bind,
    # The following needs to be replaced with propper hba managment
    ip_mask_allow_all_users => '',

  user { 'postgres':
    ensure     => present,
    comment    => 'PostgreSQL Database Server',
    system     => true,
    home       => $dbroot,
    managehome => true,
    shell      => '/bin/bash',

  file { $dbroot:
    ensure  => 'directory',
    owner   => 'postgres',
    group   => 'postgres',
    mode    => '0750',
    require => User[postgres]

  file { 'PSQL History':
    ensure  => 'file',
    path    => "${dbroot}/.psql_history",
    owner   => 'postgres',
    group   => 'postgres',
    mode    => '0750',
    require => File[$dbroot]

  if $::osfamily == 'RedHat' {
    file { '/usr/lib/systemd/system/postgresql.service':
      ensure => link,
      target => "/usr/lib/systemd/system/postgresql-${version}.service",
      force  => true,
      before => Class[Postgresql::Server]

  include stdlib
  include postgresql::client
  include postgresql::server::contrib
  include postgresql::server::postgis
  include postgresql::lib::devel
  $postgres_databases = hiera_hash('postgres_databases', {} )
  create_resources('postgresql::server::db', $postgres_databases)
  create_resources('postgresql::server::role', hiera_hash('postgres_users'))


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-04-23 18:42:50 -0600

lupin gravatar image

The error logs on your subject is different from what on your description so I can't tell which really is the issue you're having, anyway I think the issue lies on your re-assignment of variable value.

So instead of "if" clause try to use "selector"

$bind = $remote ? { 'true' => '*', 'false' => '', default => 'bla', }

edit flag offensive delete link more

answered 2015-04-23 23:29:36 -0600

GregLarkin gravatar image

If some other class includes your profiles::postgresql class with no parameters supplied, then $postgres_databases will be set to the value "test".

At the bottom of your code, you are trying to reassign the value of that variable with the output of a hiera_hash() function call. Puppet does not allow variable reassignment, hence you receive the error that you noted.

What you might want to do is move the assignment of the variable with hiera_hash() directly into the parameter list. That way, if the parameter is not supplied, the variable will be set to the return value of the function. If a parameter is supplied, its value will be used instead.

edit flag offensive delete link more

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


Asked: 2015-04-23 03:49:36 -0600

Seen: 966 times

Last updated: Apr 23 '15