In a template, is there a way to use a variable to reference another variable name? For instance, let''s say I have a variable ''interface'', and in my template I want to reference @ipaddress_$interface so that when interface = ''eth0'' I get the value of @ipaddress_eth0, but when interface = ''eth1'' I get the value of @ipaddress_eth1. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
windowsrefund
2013-Feb-21 16:51 UTC
[Puppet Users] Re: Using a variable in another variable name
<%= eval "macaddress_" + my_lan1 %> That will return the appropriate fact based on the value of my_lan1 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Ti Leggett
2013-Feb-21 16:58 UTC
Re: [Puppet Users] Re: Using a variable in another variable name
Many thanks! On Feb 21, 2013, at 10:51 AM, windowsrefund <windowsrefund@gmail.com> wrote:> > <%= eval "macaddress_" + my_lan1 %> > > That will return the appropriate fact based on the value of my_lan1 > > -- > You received this message because you are subscribed to the Google Groups "Puppet Users" group. > To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Nick Fagerlund
2013-Feb-22 00:50 UTC
[Puppet Users] Re: Using a variable in another variable name
Other options: <%= scope.lookupvar("macaddress_#{my_lan1}") %> Also, the stdlib module has a function useful for doing this outside of templates: https://github.com/puppetlabs/puppetlabs-stdlib/blob/master/lib/puppet/parser/functions/getvar.rb On Thursday, February 21, 2013 8:51:15 AM UTC-8, windowsrefund wrote:> > > <%= eval "macaddress_" + my_lan1 %> > > That will return the appropriate fact based on the value of my_lan1 > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Ti Leggett
2013-Feb-26 16:16 UTC
Re: [Puppet Users] Using a variable in another variable name
Is there a good way to check that the fact in question actually exists prior to doing an eval? Doing the below when there is no ''my_lan1'' fact produces an error. On Feb 21, 2013, at 10:51 AM, windowsrefund <windowsrefund@gmail.com> wrote:> > <%= eval "macaddress_" + my_lan1 %> > > That will return the appropriate fact based on the value of my_lan1 > > -- > You received this message because you are subscribed to the Google Groups "Puppet Users" group. > To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Ti Leggett
2013-Feb-26 21:11 UTC
Re: [Puppet Users] Using a variable in another variable name
Nevermind. Figured this out: <% if defined?( eval "macaddress_" + my_lan1 -%> ... <% end -%> On Feb 26, 2013, at 10:16 AM, Ti Leggett <shingle@gmail.com> wrote:> Is there a good way to check that the fact in question actually exists prior to doing an eval? Doing the below when there is no ''my_lan1'' fact produces an error. > > On Feb 21, 2013, at 10:51 AM, windowsrefund <windowsrefund@gmail.com> wrote: > >> >> <%= eval "macaddress_" + my_lan1 %> >> >> That will return the appropriate fact based on the value of my_lan1 >> >> -- >> You received this message because you are subscribed to the Google Groups "Puppet Users" group. >> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. >> To post to this group, send email to puppet-users@googlegroups.com. >> Visit this group at http://groups.google.com/group/puppet-users?hl=en. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Adrien Thebo
2013-Feb-27 01:15 UTC
Re: [Puppet Users] Using a variable in another variable name
Using the scope.lookupvar or getvar functions would be greatly preferable; using `eval` in this context is Really Bad (TM) and could produce some extremely bad behavior depending on the value of my_lan1. On Tue, Feb 26, 2013 at 1:11 PM, Ti Leggett <shingle@gmail.com> wrote:> Nevermind. Figured this out: > > <% if defined?( eval "macaddress_" + my_lan1 -%> > ... > <% end -%> > > On Feb 26, 2013, at 10:16 AM, Ti Leggett <shingle@gmail.com> wrote: > > > Is there a good way to check that the fact in question actually exists > prior to doing an eval? Doing the below when there is no ''my_lan1'' fact > produces an error. > > > > On Feb 21, 2013, at 10:51 AM, windowsrefund <windowsrefund@gmail.com> > wrote: > > > >> > >> <%= eval "macaddress_" + my_lan1 %> > >> > >> That will return the appropriate fact based on the value of my_lan1 > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups "Puppet Users" group. > >> To unsubscribe from this group and stop receiving emails from it, send > an email to puppet-users+unsubscribe@googlegroups.com. > >> To post to this group, send email to puppet-users@googlegroups.com. > >> Visit this group at http://groups.google.com/group/puppet-users?hl=en. > >> For more options, visit https://groups.google.com/groups/opt_out. > >> > >> > > > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to puppet-users+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- Adrien Thebo | Puppet Labs -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Ti Leggett
2013-Feb-27 14:32 UTC
Re: [Puppet Users] Using a variable in another variable name
Can you provide an example of using those safer functions in this case? On Feb 26, 2013, at 7:15 PM, Adrien Thebo <adrien@puppetlabs.com> wrote:> Using the scope.lookupvar or getvar functions would be greatly preferable; using `eval` in this context is Really Bad (TM) and could produce some extremely bad behavior depending on the value of my_lan1. >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.