accessing puppet config variables inside a manifest

984 views
Skip to first unread message

Gabriel Filion

unread,
Jan 31, 2011, 12:06:29 PM1/31/11
to puppet...@googlegroups.com
Hello,

I'm currently using a module named "common" which is based off of David
Schmitt's work.

I'm having issues with it in OpenBSD, and the reason is that the
directory created to contain module-specific files is hardcoded to
/var/lib/pupppet/modules.

Now, I could easily special-case the path for OpenBSD and the other OSes
that don't keep their $vardir under this directory, but I was wondering
if it was possible to access variables from puppet.conf into the manifests.
If it could be possible, it would be more elegant to use the $vardir
value directly.

I've done a quick test to print (notify{}) the value of $vardir and it's
empty. Do you guys know of a way I could access this value within the
module's manifests?

--
Gabriel Filion

Nan Liu

unread,
Jan 31, 2011, 1:09:15 PM1/31/11
to puppet...@googlegroups.com

Yeah, that would make manifests a lot more flexible. Here's a function
that should work for this purpose and potentially other puppet
configuration variables as well:

# puppet_vardir.rb
module Puppet::Parser::Functions
newfunction(:puppet_vardir, :type => :rvalue) do
Puppet[:vardir]
end
end

# vardir.pp
notice (puppet_vardir())

# puppet apply vardir.pp
notice: Scope(Class[main]): /var/lib/puppet
# puppet apply vardir.pp --vardir=/tmp/
notice: Scope(Class[main]): /tmp

Thanks,

Nan

donavan

unread,
Feb 1, 2011, 4:10:25 AM2/1/11
to Puppet Users
On Jan 31, 10:09 am, Nan Liu <n...@puppetlabs.com> wrote:
> On Mon, Jan 31, 2011 at 9:06 AM, Gabriel Filion <lelu...@gmail.com> wrote:
> Yeah, that would make manifests a lot more flexible. Here's a function
> that should work for this purpose and potentially other puppet
> configuration variables as well:

You can also wedge this in to an inline template like:
inline_template('<%= Puppet.settings[:vardir] %>')

The first (large) caveat is that this checks the setting on the puppet
master. I'm not sure if you want the value from the client. If so I
suspect you'd need to create a facter fact that parsed your
puppet.conf.

I'm also not sure on the best way to access Puppet settings. It's been
a while since I've done this off hand. I know (previously?) settings
could be accessed differently based on their section(s) in
puppet.conf. [master] and [main] may have different settings, for
example.

R.I.Pienaar

unread,
Feb 1, 2011, 4:16:11 AM2/1/11
to puppet...@googlegroups.com

Since 2.6.0 you can just do notice($settings::vardir)

Gabriel Filion

unread,
Feb 3, 2011, 3:20:38 AM2/3/11
to puppet...@googlegroups.com, Nan Liu
On 11-01-31 01:09 PM, Nan Liu wrote:
>> I've done a quick test to print (notify{}) the value of $vardir and it's
>> > empty. Do you guys know of a way I could access this value within the
>> > module's manifests?
> Yeah, that would make manifests a lot more flexible. Here's a function
> that should work for this purpose and potentially other puppet
> configuration variables as well:
>
> # puppet_vardir.rb
> module Puppet::Parser::Functions
> newfunction(:puppet_vardir, :type => :rvalue) do
> Puppet[:vardir]
> end
> end

hmm, that's interestingly very simple. I would personally use a fact
instead of a function, but now that I know the ruby part to fetch the
config value, writing a fact shouldn't be too difficult.

thanks,

--
Gabriel Filion

Gabriel Filion

unread,
Feb 3, 2011, 3:35:56 AM2/3/11
to puppet...@googlegroups.com, R.I.Pienaar
On 11-02-01 04:16 AM, R.I.Pienaar wrote:
> Since 2.6.0 you can just do notice($settings::vardir)

oh .. I've just tried it out and it works. hurray for 2.6, then! :)

and, I'll also keep Nan Liu's suggestion in mind for 0.25.x

Thanks to everyone for the feedback!

--
Gabriel Filion

Reply all
Reply to author
Forward
0 new messages