Skip to content

Instantly share code, notes, and snippets.

@riton
Created December 19, 2014 13:48
Show Gist options
  • Save riton/332ec8a9a41b18bae12c to your computer and use it in GitHub Desktop.
Save riton/332ec8a9a41b18bae12c to your computer and use it in GitHub Desktop.
puppet_array_operation

How can I transform

['value1', 'value2', 'value3']

into

['something value1 somethingelse', 'something value2 somethingelse', 'something value3 somethingelse']

in puppet ?

@riton
Copy link
Author

riton commented Dec 19, 2014

$service_name = 'kerberos'
$protocol = 'tcp'
$port = 88
$aliases = [ 'kerberos5', 'krb5', 'kerberos-sec' ]

  augeas { "${service_name}_${protocol}":
          context => '/files/etc/services',
          changes => [
                  "defnode node service-name[.='${service_name}'][protocol = '${protocol}'] ${service_name}",
                  "set \$node/port ${port}",
                  "set \$node/protocol ${protocol}",
                  'remove $node/alias',
                  'remove $node/#comment',

                  # for every $element in $aliases,
                  # 'set $node/alias[last()+1] $element

                  "set \$node/#comment '${comment}'"
          ]
  }

@riton
Copy link
Author

riton commented Dec 19, 2014

#
define etc_services (
  $port,
  $comment,
  $aliases = [],
  $ensure = 'present'
)
{
  validate_re($name, '^\w+_#_(tcp|udp)$')
  validate_re($ensure, '^(absent|present)$')
  validate_re($port, '^\d+$')
  validate_array($aliases)
  validate_string($comment)

  $primary_keys = split($name, '_#_')
  $service_name = $primary_keys[0]
  $protocol = $primary_keys[1]

  $augeas_alias_operations = prefix($aliases, 'set $node/alias[last()+1] ')

  $augeas_pre_alias_operations = [
    "defnode node service-name[.='${service_name}'][protocol = '${protocol}'] ${service_name}",
    "set \$node/port ${port}",
    "set \$node/protocol ${protocol}",
    'remove $node/alias',
    'remove $node/#comment'
  ]

  $augeas_post_alias_operations = [
    "set \$node/#comment '${comment}'"
  ]

  augeas { "${service_name}_${protocol}":
          context => '/files/etc/services',
          changes => flatten([
            $augeas_pre_alias_operations,
            $augeas_alias_operations,
            $augeas_post_alias_operations
          ])
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment