Skip to content

Instantly share code, notes, and snippets.

@runswithd6s
Last active August 29, 2015 14:03
Show Gist options
  • Save runswithd6s/ef926f56b5c9e473324a to your computer and use it in GitHub Desktop.
Save runswithd6s/ef926f56b5c9e473324a to your computer and use it in GitHub Desktop.
puppetlabs/stdlib and delete on undef elements
$array1 = undef
$array2 = ['a','b','c']
$array3 = [$array1, $array2]
$flat = flatten([$array3])
$pruned = delete($flat,undef)
$plist = join($pruned,',')
notice("Pruned array contains ${plist}")
# Expected Result - order not guaranteed
# "Pruned array contains a,b,c"
# Actual Results - order not guaranteed
# "Pruned array contains a,b,c,undef"
# The actual code snippet that is failing for me is
if $hostgroups and is_array($hostgroups) {
$prune_undef = delete($hostgroups,undef)
$prune_undef_str = delete($prune_undef,'undef')
$hlist = join($prune_undef_str,',')
} elsif $hostgroups {
notice("Received hostgroups as a string")
$hlist = $hostgroups
}
notice("Hostgroups to apply for ${host_name} are ${hlist}")
# I still get a list that includes the string "undef" in the array
@runswithd6s
Copy link
Author

I believe my solution is to add a known string, which I can prune later.

# $hostgroups is passed in as an array at the class
$not_present = 'not_present'
$hg_vmware_guest = $::virtual ? {
    /vmware/ => 'vmware_guest',
    default     => $not_present
}
$hg_os_linux = $::kernel ? {
    /Linux/ => 'os_linux',
    default => $not_present,
}
$flat = flatten([$hg_vmware_guest, $hg_os_linux, $hostgroups])
$uniq = unique($flat)
$hg = delete($uniq,$not_present)
$hlist = join($hg,',')

nagios_host { $::hostname: hostgroups => $hlist }

@teaforthecat
Copy link

I think this calls for adding a function called 'append'

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