Skip to content

Instantly share code, notes, and snippets.

@WhatsARanjit
Created March 16, 2018 11:12
Show Gist options
  • Save WhatsARanjit/bd5d3bc28a18c8ea888778212f260a09 to your computer and use it in GitHub Desktop.
Save WhatsARanjit/bd5d3bc28a18c8ea888778212f260a09 to your computer and use it in GitHub Desktop.
ENV arrays merge
function test::merge_env_arrays (
Array $array_one,
Array $array_two,
) >> Array {
$hash_one = $array_one.reduce({}) |$memo, $var| {
$v = $var.split('=')
merge($memo, { $v[0] => $v[1] })
}
$hash_two = $array_two.reduce({}) |$memo, $var| {
$v = $var.split('=')
merge($memo, { $v[0] => $v[1] })
}
$merged_hashes = merge($hash_one, $hash_two)
$merged_hashes.map |$item| { "${item[0]}=${item[1]}" }
}
[root@puppet2017 /opt/puppetlabs/puppet/modules]# puppet apply -e 'notice(test::merge_env_arrays(["1=a", "2=b"], ["1=x", "2=y", "3=z"]))'
Notice: Scope(Class[main]): [1=x, 2=y, 3=z]
Notice: Compiled catalog for puppet2017.3.0.puppetlabs.vm in environment production in 0.04 seconds
Notice: Applied catalog in 0.26 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment