Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm hacking away at the puppetlabs-haproxy module (https://github.com/puppetlabs/puppetlabs-haproxy) and I'm trying to add the ability to detect whether a particular haproxy listener should have a stats listener enabled. Here's what I've got:

define haproxy::listen (
  $ports,
  $ipaddress        = $::ipaddress,
  $mode             = 'tcp',
  $collect_exported = true,
  $stats_uri        = false,
  $options          = {
    'option'  => [
      'tcplog',
    ],
    'balance' => 'roundrobin',
  }
) {
  if ($stats_uri) {
    $options['stats'] => 'uri /' #<=======SYNTAX ERROR HERE
  }
  # Template uses: $name, $ipaddress, $ports, $options
  concat::fragment { "${name}_listen_block":
    order   => "20-${name}-1",
    target  => '/etc/haproxy/haproxy.cfg',
    content => template('haproxy/haproxy_listen_block.erb'),
  }

  if $collect_exported {
    Haproxy::Balancermember <<| listening_service == $name |>>
  }
  # else: the resources have been created and they introduced their
  # concat fragments. We don't have to do anything about them.
}

The main difference is that I've added a parameter $stats_uri, and I'm trying to test whether that's set and add a key/value pair into $options if it is.

Currently, I'm getting a syntax error, because you obviously don't do this like I'm doing in the marked line.

So the question is, how do I maipulate the $options object based on whether $stats_uri is set or not?

share|improve this question
add comment

1 Answer

Well, $options['stats'] = 'uri /' probably works. But don't do it that way - depending on an unintended behavior isn't a good idea.

Instead, make the different options their own parameters:

define haproxy::listen (
  $ports,
  $ipaddress        = $::ipaddress,
  $mode             = 'tcp',
  $collect_exported = true,
  $stats_uri        = false,
  $option           = [ 'tcplog' ],
  $option_balance   = 'roundrobin',
  $option_stats     = undef,
) {
  if ($stats_uri) {
    $options_stats = 'uri /'
  }
share|improve this answer
    
Typically, I'd agree this is the right way to go. However, this particular module uses concat with a template, and it literally just loops through the $options array and splurges out the key => value parings into the output file (see github.com/puppetlabs/puppetlabs-haproxy/blob/master/templates/…). Hence wanting to dynamically add something into $options so I can just output it. Is the right answer here 'Change the template and do it properly'? –  growse Dec 21 '12 at 9:57
    
@growse That's what I'd probably go for. But another option might be the merge function in puppet-stdlib - see here. –  Shane Madden Dec 21 '12 at 21:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.