dynamically named variables?

11 views
Skip to first unread message

Guy Matz

unread,
Dec 15, 2014, 5:57:08 PM12/15/14
to puppet...@googlegroups.com
Hi!  I have a parameterized class that accepts a few parameters, one of which I would like to use in the retrieval of hiera data, e.g.
class foo (  $app_user = 'bar' ) {
    file { "/etc/${app_user::etc_dir}":
    owner   => 'root',
    group   => 'root',
    mode    => '0644
}

so app_user should get passed to the class, then ${app_user}::etc_dir should get resolved by hiera,  If I pass in nagios as app_user, then nagios::etc_dir should be looked up in hiera.  I can't find the right combination of curly braces to get this to work.  Should it work?

Thanks a lot,
Guy

Dan White

unread,
Dec 15, 2014, 6:24:21 PM12/15/14
to puppet...@googlegroups.com
It may take a step in the middle like this:

class foo (  $app_user = 'bar' ) {
  
    $where = "/etc/${app_user::etc_dir}"

    file { $where :
    owner   => 'root',
    group   => 'root',
    mode    => '0644
}

Good luck.  We are all here to share the info.

“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.” (Bill Waterson: Calvin & Hobbes)
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CABnTgtXthijC-uSTARE4rVsodat-Qeq5ON-r2ziN3-xDSwKkqg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Henrik Lindberg

unread,
Dec 15, 2014, 6:43:10 PM12/15/14
to puppet...@googlegroups.com
On 2014-15-12 18:57, Guy Matz wrote:
> Hi! I have a parameterized class that accepts a few parameters, one
> of which I would like to use in the retrieval of hiera data, e.g.
> class foo ( $app_user = 'bar' ) {
> file { "/etc/${app_user::etc_dir}":
What do you expect the result to be here, are you trying to do an
indirection to the variable $bar::etc_dir ? (That will not work, it will
interpolate
the value of $app_user::etc_dir irrespective of what the value of the
$app_user parameter is set to. You probably wanted ${app_user}::etc_dir.
> owner => 'root',
> group => 'root',
> mode => '0644
> }
>
> so app_user should get passed to the class, then ${app_user}::etc_dir
> should get resolved by hiera, If I pass in nagios as app_user, then
> nagios::etc_dir should be looked up in hiera. I can't find the right
> combination of curly braces to get this to work. Should it work?
>
You can lookup the key specifically using one of the hiera functions.
This way you can construct the key you want out of the given parameter
and the additional part (::etc_dir).

Regards
- henrik
> Thanks a lot,
> Guy
> --
> 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...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> <https://groups.google.com/d/msgid/puppet-users/CABnTgtXthijC-uSTARE4rVsodat-Qeq5ON-r2ziN3-xDSwKkqg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

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


Stephen Marlow

unread,
Dec 15, 2014, 6:44:24 PM12/15/14
to puppet...@googlegroups.com
I would personally take the route of explicitly looking up the hiera data, along these lines:

class foo ($app_user = 'bar') {

  $dir = hiera("${app_user}::etc_dir")

  file { "/etc/${dir}":
    ...
  }
}

Reply all
Reply to author
Forward
0 new messages