Hi,
On 04/24/2018 08:55 PM, Ryan Murphy wrote:
> So I've hit a limitation in puppet where I can't modify a variable after its been set. how do I work around this. I have a manifest that I need to be able to build up a list of contacts based on certain facts about a server.
>
> Here is an example of my (non functioning) code? Can anyone give me a suggestion of how to work around this limitation?
>
> |
> if 'sql' in $hostname.downcase or $wl_server_type == 'db' {
> $contactgroups = 'Windows Server Admins,Microsoft SQL DBAs'
> }
> else {
> $contactgroups = 'Windows Server Admins'
> }
>
> if $wl_app in $btssServers or 'adfs' in $hostname.downcase {
> $contactgroups = "${contactgroups},BT Systems Support"
> }
>
> if $wl_app == 'hea'{
> $contactgroups = "${contactgroups},HEAT Admins"
> }
> |
I know it's maybe satisfying but you can use a temporary variables $var_tmp1 and $var_tmp2 and $contactgroups in the last assignment.
Another way with the reduce() function (Puppet 4 at least is needed):
----------------------------------------
$contactgroups_tests = {
'dba' => 'sql' in $hostname.downcase or $wl_server_type == 'db',
'bt' => $wl_app in $btssServers or 'adfs' in $hostname.downcase,
'heat' => $wl_app == 'hea',
}
$contactgroups = $contactgroups_tests.reduce(['Windows Server Admins']) |$memo, $value| {
[$condition, $is_true] = [$value[0], $value[1]]
case [$condition, $is_true] {
['dba', true]: { $memo + ['Microsoft SQL DBAs'] }
['bt', true]: { $memo + ['BT Systems Support'] }
['heat', true]: { $memo + ['HEAT Admins'] }
default : { $memo }
}
}.join(',')
----------------------------------------
I don't know if it's more readable than the first way.
> In a language like Python I could just append to the string... but I can't modify the variable at all in Puppet after its been created.
Yes, reassignment is forbidden in Puppet.
I would have liked reassignment for "Data" data type to be possible too (it would have simplify me some codes) but I think it's not a feature wanted by developers. As you can see, generally workarounds are possible.
Regards.
--
François