How to check if a given "directory" exists??

6,167 views
Skip to first unread message

Sans

unread,
Sep 28, 2011, 9:37:35 AM9/28/11
to Puppet Users
Dear all,

I have a module like this:

class mom_priv_config{
file {
'config':
owner => 'root', group => 'root', mode => '0644',
name => '/var/torque/mom_priv/config',
content => template('w_nodes/mom_priv-config.tpl'),
#notify => Service['pbs_mom']
}
}

which is working great by its own. But all I want is to carry on with
this *ONLY IF* "/var/torque/mom_priv" directory exists on the client.
If there is no such directory presents, just ignore. How can I do
this? I looked in the net but nothing came out as a solution to me.
Any one can help me with please? Cheers!!

Peter Bukowinski

unread,
Sep 28, 2011, 11:33:50 AM9/28/11
to puppet...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.


I'd create a custom fact that checks for the existence of the mom_priv directory -- something like this:

# mom_priv_test.rb
if FileTest.directory?("/var/torque/mom_priv")
    Facter.add("mom_priv_test") do
        setcode { true }
    end
end
#

Now you can use this fact to wrap your file resource in an if statement:

class mom_priv_config {
    if $mom_priv_test == 'true' {
        file { '/var/torque/mom_priv/config':
            ensure  => present,
            owner    => 'root',
            group    => 'root',
            mode    => '0644',

            content => template('w_nodes/mom_priv-config.tpl'),
            #notify  => Service['pbs_mom'],
        }
    }
}

-- 
Peter M. Bukowinski
Sr. Systems Engineer
Janelia Farm Research Campus, HHMI

Sans

unread,
Sep 28, 2011, 6:38:30 PM9/28/11
to Puppet Users
Thanks Peter!
Custom fact is a great idea but the downside is one needs to create a
custom-fact each for every check you wanna perform. Isn't there
anything a bit more dynamic, like checking the location on fly ( bash
equivalent: if [ -d "/var/torque/mom_priv" ]; ) ?? Cheers!!

Dominik Zyla

unread,
Sep 28, 2011, 7:58:31 PM9/28/11
to puppet...@googlegroups.com

Hello,

You can work it around with something like:

exec { 'mkdir -p 0644':
path => "/usr/bin:/usr/sbin:/bin",
unless => "[ -d '/var/torque/mom_priv' ]"
}

Hope this helps.

--
Dominik Zyla

jcbollinger

unread,
Sep 29, 2011, 9:55:18 AM9/29/11
to Puppet Users


On Sep 28, 5:38 pm, Sans <r.santanu....@gmail.com> wrote:
> Thanks Peter!
> Custom fact is a great idea but the downside is one needs to create a
> custom-fact each for every check you wanna perform. Isn't there
> anything a bit more dynamic, like checking the location on fly ( bash
> equivalent: if [ -d "/var/torque/mom_priv" ]; ) ?? Cheers!!


The idea in the first place runs somewhat against the grain for
Puppet: in general, you should prefer Puppet to tell, rather than to
ask. Thus, the question is why Puppet doesn't already know whether /
var/torque/mom_priv should exist. By design, Puppet DSL has no direct
mechanism for specifying client-side logic.

That leaves you with three alternatives for configuring clients based
on unmanaged state details:

1) Use custom facts to report client state details to the master, and
perform the logic there.
2) Use an Exec to run a script, either as literal code or from a file.
3) Write a custom type and provider in Ruby that does what you want.

Where possible, I recommend avoiding all of those and instead managing
the state in question.


John
Reply all
Reply to author
Forward
0 new messages