concating a string from itself

14 views
Skip to first unread message

Ryan Murphy

unread,
Apr 24, 2018, 2:55:19 PM4/24/18
to Puppet Users
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"
  }



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.

Henrik Lindberg

unread,
Apr 24, 2018, 5:22:33 PM4/24/18
to puppet...@googlegroups.com
On 24/04/18 20:55, 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"
>   }
> |
>


You could do something like this:

$contactgroups = [
'Windows Server Admins',

if 'sql' in $hostname.downcase or $wl_server_type == 'db' {
'Microsoft SQL DBAs'
},

if $wl_app in $btssServers or 'adfs' in $hostname.downcase {
'BT Systems Support'
},

if $wl_app == 'hea' {
'HEAT Admins'
}
].filter |$x| { $x =~ NotUndef }.join(',')


It creates an array with the values, each "if" produces a value to
include or undef. The "filter" creates a new array where all undef
values are dropped, and finally, it calls join to separate them with a
comma.

Best,
- henrik

>
>
> 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.
>


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Francois Lafont

unread,
Apr 24, 2018, 5:31:12 PM4/24/18
to puppet...@googlegroups.com
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

Francois Lafont

unread,
Apr 24, 2018, 5:35:23 PM4/24/18
to puppet...@googlegroups.com
On 04/24/2018 11:22 PM, Henrik Lindberg wrote:

> $contactgroups = [
>   'Windows Server Admins',
>
>   if 'sql' in $hostname.downcase or $wl_server_type == 'db' {
>     'Microsoft SQL DBAs'
>   },
>
>   if $wl_app in $btssServers or 'adfs' in $hostname.downcase {
>     'BT Systems Support'
>   },
>
>   if $wl_app == 'hea' {
>     'HEAT Admins'
>   }
> ].filter |$x| { $x =~ NotUndef }.join(',')

I hadn't seen the answer of Henrik but indeed his code is definitively better than my reduce(). ;)

Bye.

--
François
Reply all
Reply to author
Forward
0 new messages