Create a variable variable

31 views
Skip to first unread message

rvlinden

unread,
Oct 15, 2012, 8:12:28 AM10/15/12
to puppet...@googlegroups.com
Hi all,
 
I'm wondering if puppet can create a variable based on another variable ??
Here is an example
 
3 global varibles
 
$::gbl_users_groups = app1,app2
$::gbl_users_accounts_app1 = app1user1,app1user2
$::gbl_users_accounts_app2 = app2user1,app2user2
 
What does work is this
 
$groups_name_array = split($::gbl_users_accounts_app1,',')
 or
$groups_name_array = split($::gbl_users_accounts_app2,',')
 
 
What I really want to do, is replace the static app1 and app2 within the $groups_name_array, to something dynamic like this
 
$groups_array = split($::gbl_users_groups,',')
$groups_name_array = split($::gbl_users_accounts_$groups_array,',')
 
If have tried various combination, but just can seen to find the right one (if it is possible)
 
Any suggestions
 
Regards, Rene

Henrik Lindberg

unread,
Oct 15, 2012, 10:10:29 AM10/15/12
to puppet...@googlegroups.com
Did you consider using a hash?

$x = { 'app1' => 'app1user1,app1user2', 'app2 => '...' }
$group = 'app1'
$y = split($x[$group], ',')

In the DSL it is not possible to reference a variable in a dynamic way
(i.e. compute the variable name), but you can compute a hash key.

e.g.
$group_nrb = 1
$y = split($x["app${group_nbr}"], ',')

Hope that helps
- henrik

On 2012-15-10 14:12, rvlinden wrote:
> Hi all,
> I'm wondering if puppet can create a variable based on another variable ??
> Here is an example
> 3 global varibles
> $::gbl_users_groups = app1,app2
> $::gbl_users_accounts_app1 = app1user1,app1user2
> $::gbl_users_accounts_app2 = app2user1,app2user2
> What does work is this
> $groups_name_array = split($::gbl_users_accounts_app1,',')
> or
> $groups_name_array = split($::gbl_users_accounts_app2,',')
> What I really want to do, is replace the static app1 and app2 within the
> $groups_name_array, to something dynamic like this
> $groups_array = split($::gbl_users_groups,',')
> $groups_name_array = split($::gbl_users_accounts_*$groups_array*,',')
> If have tried various combination, but just can seen to find the right
> one (if it is possible)
> Any suggestions
> Regards, Rene
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/puppet-users/-/OROoQBAoDCUJ.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to
> puppet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en.


John Warburton

unread,
Oct 15, 2012, 7:09:06 PM10/15/12
to puppet...@googlegroups.com
On 15 October 2012 23:12, rvlinden <rene.vand...@gmail.com> wrote:
Hi all,
 
I'm wondering if puppet can create a variable based on another variable ??
Here is an example

A quick google of "puppet variable variables" found me https://blog.kumina.nl/2010/09/puppet-tipstricks-variable-variables/

I've used the method described

John

Reply all
Reply to author
Forward
0 new messages