Ask Your Question
0

Handle Empty Arrays

asked 2017-03-23 10:37:30 -0500

puser gravatar image

I am completely stumped on this one..

I have a module like:

class test (
 $file,
 $list,
){

file { $file:
  ensure  => 'file',
  content => epp('module/list.epp', {'list' => $list}),
  mode    => '0644',
  owner   => '0',
  group   => '0',
}

}

$list and $file are coming in from hiera. $file is a string that is set and $list is a deep merged array.

Basically I want to handle the file's contents based on the list parameter. However not every server is going to get data. For those that do it works fine, for those that do not I am getting an error saying the parameter $list is expecting a value.

Ok fine, How about...

class test (
 $file,
 $list = [''],
){

No dice, it won't take empty strings either...

How can I do the following:
1. create the file on every server (even if blank)
2. have the file be empty for servers without data
3. get the data across for servers with data.

edit retag flag offensive close merge delete

Comments

Conditionals.

Kai Burghardt gravatar imageKai Burghardt ( 2017-03-23 17:16:49 -0500 )edit

that doesn't fix this issue.

puser gravatar imagepuser ( 2017-03-24 06:14:30 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-03-24 06:16:56 -0500

puser gravatar image

I found where the issue was. (This was not my code btw)

There was a parent class like

class parent (
 $file,
 $list,
){
   class {'::test':
      file => $file,
      list => $list,
  }

which then calls the class I mentioned above.

class test (
 $file,
 $list,
){

file { $file:
  ensure  => 'file',
  content => epp('module/list.epp', {'list' => $list}),
  mode    => '0644',
  owner   => '0',
  group   => '0',
}

My issue was I was trying to set the $list in the sub class, where the data was null in the parent class. A simple $list = [] defaulted the value to an empty array which resolved the issue. I am refactoring this bad code to pass the hiera data directly to the subclass.

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: 2017-03-23 10:37:30 -0500

Seen: 39 times

Last updated: Mar 24