Puppet module and Hiera variable access

82 views
Skip to first unread message

Tobias Köck

unread,
Jul 18, 2016, 2:55:09 PM7/18/16
to Puppet Users
Hi,

for testing Hiera a have written a common.yaml with

qmonitoring::db::icinga2_ido_password: "mypwd34"
qmonitoring::db::icinga2_webdb_password: "mypwd544"

and I have a module named qmonitoring with a class named

class qmonitoring::db {

$new_var = $icinga2_ido_password,

}

where I try to automatically access the

qmonitoring::db::icinga2_ido_password

Hiera variable.

With $icinga2_ido_password it doesn't work with hiera( ...) it works.

How can I automatically access the Hiera variable in my class with the
priority lookup without having to use hiera(...)?

Greetings and thanks,
Tobias



signature.asc

Rob Nelson

unread,
Jul 18, 2016, 3:41:11 PM7/18/16
to puppet...@googlegroups.com
You should investigate hiera automatic parameter lookup (https://docs.puppet.com/hiera/latest/puppet.html#automatic-parameter-lookup). You need to make `icinga2_ido_password` a parameter of your class and then APL will "magically" populate that properly. Check out the article, which gets into details of default values and whatnot as well.



--
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/486eacc6-36c2-da49-c958-def735f2f6cb%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Ramin K

unread,
Jul 18, 2016, 4:19:18 PM7/18/16
to puppet...@googlegroups.com
You can use parameters as Rob suggests or specify the lookup yourself.

$new_var = hiera('icinga2_ido_password')

Also the var will be $::qmonitoring::db::new_var not the key you used.

I recommend matching var to lookup key to make it easier to trace
through the code.

$icinga2_ido_password = hiera('icinga2_ido_password')

Ramin

Robert Story

unread,
Jul 19, 2016, 3:16:10 AM7/19/16
to Tobias Köck, Puppet Users
On Mon, 18 Jul 2016 20:54:43 +0200 Tobias wrote:
TK> for testing Hiera a have written a common.yaml with
TK>
TK> qmonitoring::db::icinga2_ido_password: "mypwd34"
TK> qmonitoring::db::icinga2_webdb_password: "mypwd544"
TK>
TK> and I have a module named qmonitoring with a class named
TK>
TK> class qmonitoring::db {
TK>
TK> $new_var = $icinga2_ido_password,
TK>
TK> }
TK> [...]
TK> With $icinga2_ido_password it doesn't work with hiera( ...) it works.
TK>
TK> How can I automatically access the Hiera variable in my class with the
TK> priority lookup without having to use hiera(...)?

Try making it a parameter:

class qmonitoring::db ( $icinga2_ido_password = 'default' ) {
...


Robert

--
Senior Software Engineer @ Parsons

Tobias Koeck

unread,
Jul 20, 2016, 5:42:59 AM7/20/16
to Robert Story, Puppet Users
Hi

that worked. Thanks.

So if I want to force the user to set the variables in Hiera I have to use the hiera( .. ) function?

Greetings
Tobias

jcbollinger

unread,
Jul 20, 2016, 8:53:09 AM7/20/16
to Puppet Users


On Wednesday, July 20, 2016 at 4:42:59 AM UTC-5, Tobias Koeck wrote:
Hi

that worked. Thanks.

So if I want to force the user to set the variables in Hiera I have to use the hiera( .. ) function?


That's an odd way to put it, but if I understand you correctly then yes.  In other words, if you want the variables to receive their values specifically from Hiera and not from anywhere else, then you should make them ordinary class variables, not parameters, and use the hiera() function to initialize them.  That seems pretty natural to me.


John

Reply all
Reply to author
Forward
0 new messages