Ask Your Question

Can I pass an array to a function requiring multiple arguments?

asked 2013-02-21 08:44:07 -0600

I'm trying to do the following:

$hosts_templates = [ 'files/hosts.txt' ]
if "serverB" in $hostname {
  $hosts_templates += 'files/hosts_serverB.txt'

file { "/etc/hosts":
  ensure  => file,
  mode    => 444,
  owner   => root,
  group   => root,
  content => template($hosts_templates),

Does Puppet support unrolling an array into function arguments?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-02-21 16:56:27 -0600

Stefan gravatar image

Some functions may flatten the argument list but the template function does not. I am not aware of a way to unroll the arguments in the Puppet DSL.

But even if it was possible your example would not work because you are not allowed to define a variable twice (the Language Guide explains why that is)

But you can do something like

$generic_content = template('files/hosts.txt')
$special_content = $some_conditional ? 
  'foo'   => template('files/hosts.specialA.txt'),
  'bar'   => template('files/hosts.specialB.txt'),
  default => ''
file { '/etc/hosts':
  ensure  => file,
  mode    => '0444',
  owner   => 'root',
  group   => 'root',
  content => "${generic_content}${special_content}",
edit flag offensive delete link more

answered 2013-02-26 14:01:45 -0600

Ancillas gravatar image

Hiera seems like a good solution here. You can pull specific data based on the value of $hostname.

If you don't want to use hiera, you could use a parameterized class, and pass in your array when you include the class.

I suppose you could also build a case statement and assign $hosts_template based on the value of $hostname, but in the long run, I got bitten by solutions like that (I kept having to expand the case statements as our environment grew).

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



Asked: 2013-02-21 08:44:07 -0600

Seen: 416 times

Last updated: Feb 26 '13