Correct syntax for adding a key=>value pair to existing object in puppet

puppet

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?

Best 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 /'
  }
Related Topic