environmentpath in manifest

70 views
Skip to first unread message

Daniele Palumbo

unread,
May 9, 2016, 9:45:38 AM5/9/16
to puppet...@googlegroups.com
Hi,

i need to check if a file exists on the puppet master.

No big deal:
http://stackoverflow.com/questions/18784329/how-to-test-for-existence-of-a-file-on-the-puppet-master

So i made:
--
cat ./modules/braveconf/lib/puppet/parser/functions/file_exists.rb
require"puppet"
module Puppet::Parser::Functions
newfunction(:file_exists, :type => :rvalue) do |args|
if File.exists?(args[0])
return 1
else
return 0
end
end
end
--

Now, my problem is about path.

Since i have environmentpath, as seen is
puppet config print environmentpath --section master --environment development
/etc/puppetlabs/code/environments

I would like to use, in the manifest:
--
if ( file_exists ("${::environmentpath}/${::environment}/modules/module/files/configfile-${::hostname}") == 1 ) {
--

But ${::environmentpath} is empty.

How can i manage this topic, avoiding to hardcode /etc/puppetlabs/code/environments in the code?
Is there a way to get variable from puppet master config?

Thanks,
Daniele


Martin Alfke

unread,
May 12, 2016, 4:19:53 AM5/12/16
to puppet...@googlegroups.com
Hi Daniele,
You might want to access the Puppet settings:

Puppet.initialize_settings unless Puppet[:confdir]

and then check for the environmentpath setting.


>
> How can i manage this topic, avoiding to hardcode /etc/puppetlabs/code/environments in the code?
> Is there a way to get variable from puppet master config?
>
> Thanks,
> Daniele
>
>
> --
> 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/6429762B-377B-4B9D-B8B4-526859465283%40retaggio.net.
> For more options, visit https://groups.google.com/d/optout.

Daniele Palumbo

unread,
May 12, 2016, 8:13:46 PM5/12/16
to puppet...@googlegroups.com
Il giorno 12/mag/2016, alle ore 10:19, Martin Alfke <tux...@gmail.com> ha scritto:
> You might want to access the Puppet settings:
>
> Puppet.initialize_settings unless Puppet[:confdir]
>
> and then check for the environmentpath setting.


Hi Martin,

Thank you very much for your hint.

But I am not able to understand how to write it.
How can I interlock between this ruby code and the manifest?

Sorry if this is a FAQ question :)
Do you have some link or example to understand how this should work?

Thank you very much,
Daniele

Martin Alfke

unread,
May 13, 2016, 3:37:02 AM5/13/16
to puppet...@googlegroups.com
Hi Daniele,

On 13 May 2016, at 02:13, Daniele Palumbo <dan...@retaggio.net> wrote:

> Il giorno 12/mag/2016, alle ore 10:19, Martin Alfke <tux...@gmail.com> ha scritto:
>> You might want to access the Puppet settings:
>>
>> Puppet.initialize_settings unless Puppet[:confdir]
>>
>> and then check for the environmentpath setting.
>
>
> Hi Martin,
>
> Thank you very much for your hint.
>
> But I am not able to understand how to write it.
> How can I interlock between this ruby code and the manifest?

The Ruby part must be a function - like in the example you provided in your first email.
You can then use the function inside your manifest.

Functions are always executed on the Master.

Best,
Martin

>
> Sorry if this is a FAQ question :)
> Do you have some link or example to understand how this should work?
>
> Thank you very much,
> Daniele
>
> --
> 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/0329312A-9935-4B1F-A2CF-19E0E4D484BB%40retaggio.net.

watt...@gmail.com

unread,
May 13, 2016, 11:16:19 AM5/13/16
to puppet...@googlegroups.com
You may need to create a custom fact to determine the environment when
the agent runs. For example, here's a
custom fact that reads the environment from a file.

Facter.add("environment") do
setcode do
Facter::Util::Resolution.exec('/bin/cat /usr/local/etc/environment')
end
end

Your site manifest can also be set to update the file contents when it runs.

Michael Watters

unread,
May 13, 2016, 11:16:20 AM5/13/16
to Puppet Users
You may need to create a custom fact to determine the environment when 
the agent runs.  For example, here's a
custom fact that reads the environment from a file.

Facter.add("environment") do
   setcode do
     Facter::Util::Resolution.exec('/bin/cat /usr/local/etc/environment')
   end
end

Your site manifest can also be set to update the file contents when it runs.


Daniele Palumbo

unread,
May 17, 2016, 2:48:37 PM5/17/16
to puppet...@googlegroups.com
Hi Michael,

there are 2 possibilities:
1) i have not got what you mean.

2) you have not got what i mean.

To better explain:
your suggestion is to look over a fact in the agents.
my needed environmentpath variable is the master one.
i need to check over the existence of a file, but i want to have the full path as variables.
in example: $confdir in puppet.conf, but master section, not agent one.

which of the 2 cases? :)

Thanks in any case!

Daniele
> --
> 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/9b1f21ea-a95c-4bd3-a44d-8ba85b32a636%40googlegroups.com.
signature.asc

Daniele Palumbo

unread,
May 17, 2016, 4:48:52 PM5/17/16
to puppet...@googlegroups.com
Hi Martin,

Il giorno 13/mag/2016, alle ore 09:36, Martin Alfke <tux...@gmail.com> ha scritto:
> The Ruby part must be a function - like in the example you provided in your first email.
> You can then use the function inside your manifest.
>
> Functions are always executed on the Master.

After a couple of try, i understood it.

for anyone looking at the same solution, my code:

manifest:
$masterenvpath = print_environmentpath()
$path = "${masterenvpath}/${::environment}/modules/braveconf/files/puppet/puppet.conf-${::hostname}"
if ( file_exists ("${path}") == 1 ) {

function:
lib/puppet/parser/functions/print_environmentpath.rb
require"puppet"
module Puppet::Parser::Functions
newfunction(:print_environmentpath, :type => :rvalue) do |args|
Puppet.initialize_settings unless Puppet[:environmentpath]
path = Puppet[:environmentpath]
if File.exists?(path)
path
else
nil
end
end
end


Thank you very much Martin!

Daniele
signature.asc
Reply all
Reply to author
Forward
0 new messages