3

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?

1 Answer 1

1

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 /'
  }
3
  • 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, 2012 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. Dec 21, 2012 at 21:21
  • @ShaneMadden was moved here.
    – Tgr
    Dec 10, 2016 at 3:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .