Ask Your Question
0

create_resources and hiera_hash (Cannot reassign variable)

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

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 0.0.0.0 if remote is enabled, 127.0.0.1 if not
  # Merge remote into an address array if it's anything other than a boolean
  if $remote == true {
    $bind = '*'
  } elsif $remote == false {
    $bind = '127.0.0.1'
  } else {
    $bind_array = delete(any2array($remote),'127.0.0.1')
    $bind = join(concat(['127.0.0.1'],$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 => '0.0.0.0/0',
  }

  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'))

Th

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

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

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' => '127.0.0.1', default => 'bla', }

edit flag offensive delete link more
0

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

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

Stats

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

Seen: 760 times

Last updated: Apr 23 '15