Skip to content

Instantly share code, notes, and snippets.

View hlindberg's full-sized avatar

Henrik Lindberg hlindberg

  • Bebalon Ltd
  • Għarb, Gozo, Malta
  • X @hel
View GitHub Profile
@hlindberg
hlindberg / example.pp
Created February 12, 2019 21:20
mapping a hash
$h = { 'b' => ['banana', 'bandana'], 'o' => ['orange', 'ovaltine'] }
$h2 = $h.map |$k, $v | { [$k,$v[0]] }
notice "h2 = ${h2}"
notice Hash($h2)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="generator" content="Railroad Diagram Generator 1.52.1729" />
<style type="text/css">
::-moz-selection
{
color: #FFFEF8;
background: #1E1800;
}
@hlindberg
hlindberg / any2bool.rb
Created November 2, 2018 15:14
Example: Puppet function calling new to create a Boolean from a String
Puppet::Functions.create_function(:any2bool) do
dispatch :any2bool do
param 'Any', :to_be_converted
end
def any2bool(to_be_converted)
call_function('new', Puppet::Pops::Types::PBooleanType::DEFAULT, to_be_converted)
end
end
@hlindberg
hlindberg / functional_puppet.pp
Created October 5, 2018 09:32
A fun example using Deferred to write puppet code in a functional style
# Resolves a given value - if given a Deferred it will be called and all other values are returned as is.
#
function resolve(Any $x) {
if $x =~ Deferred { $x.call } else { $x }
}
# Resolves and evaluates a condition and either resolves and evaluates the given when_true,
# or the given when_false depending on the outcome of the evaluation of the condition.
# The result is undef if the selected when_true/when_false is undefined.
#
@hlindberg
hlindberg / comma_separated_list.java
Last active September 17, 2018 18:39
Java Snippets
StringJoiner joiner = new StringJoiner(",");
for (Item item : list) {
joiner.add(item.toString());
}
return joiner.toString();
@hlindberg
hlindberg / output.md
Last active September 2, 2018 17:02
Stringified output
expression type example output
literal undef nil
literal default "default"
literal integer 1
literal float 3.14
literal false false
literal true true
literal string "hello"
literal regexp "/this|that.*/"
@hlindberg
hlindberg / myjoin.pp
Last active September 4, 2017 15:19
function myjoin(Array $a, String $delimiter) {
$formats = {
Array => {
# no delimiters = '% ', and 'a' means array
format => '% a',
separator => $delimiter,
# format strings without delimiters = '%s'
string_formats => { String => '%s' }
}
function mymodule::my_privates (
Variant[String, Numeric] $key,
Hash $options,
Puppet::LookupContext $context,
) {
case $key {
'mymodule::secret1' : { Sensitive('There is no Santa on the evening coach') }
'mymodule::secret2' : { Sensitive('All your base are belong to us') }
default: { $context.not_found }
}
<%-
$pin = "release a=${name}" # default value
if $pin_release.length > 0
$options = [
if $release.length > 0 { "a=${release}" },
if $codename.length > 0 { "n=${codename}" },
if $release_version.length > 0 { "v=${release_version}"},
if $component.length > 0 { "c=${component}" },
if $originator.length > 0 { "o=${originator}" },
if $label.length > 0 { "l=${label}" },
@hlindberg
hlindberg / exampleepp.pp
Created December 2, 2016 18:06
An example of using a local variable in EPP
# Example of local variables set inside of an EPP template
#
$template = @(EOF)
<%
# this is an element with arbitrary puppet code
$hash = { a => 1, b => 2, c => 3 }
-%>
The keys are:
<%- $hash.each |$key, $value| { -%>