Ask Your Question
2

check if a directory exists

asked 2013-09-24 15:10:41 -0500

diamondz gravatar image

updated 2013-09-25 12:21:39 -0500

Stefan gravatar image

I want to check if a directory exists, if so, set a variable for that directory, if it does not exists, I still want to set a variable. I want to be able to reuse that variable as a reference.

Update: I want to be able to re-use a script whereby the cwd => could either be /home/vagrant or /home/webdev. My shell script needs to find out if the machine being setup has a dir that exists being either /home/vagrant or /home/webdev.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-09-24 16:26:51 -0500

Stefan gravatar image

updated 2013-09-25 12:33:59 -0500

The compilation of the catalog (catalog = the desired state of the machine where your puppet agent runs) is done on your puppet master. If you want to set a variable in your manifest that depends on the current state of the target machine (the presence of a certain directory), the puppet master must be aware of that fact ("is directory X present or absent on the agent").

You can do that by writing a custom fact (documentation can be found here) which can be as simple as

Facter.add(:directory_foo_present) do
  setcode do
    if File.directory? '/opt/foo'
      'yes'
    else ...
(more)
edit flag offensive delete link more

Comments

1

Exactly. I always tell people "Tell puppet what you want, don't try to have it react." That being said, true and false would be better style than yes and no :)

lavaman gravatar imagelavaman ( 2013-09-24 16:53:44 -0500 )edit

if you use true and false be aware that facter will convert them to strings (http://projects.puppetlabs.com/issues/3704) so if you have `if $directory_foo_present {}` in your puppet ...(more)

Stefan gravatar imageStefan ( 2013-09-25 12:17:05 -0500 )edit
0

answered 2013-09-25 07:15:26 -0500

diamondz gravatar image

Thanks. That's a good point. I want to be able to re-use a script whereby the cwd => '' could either be /home/vagrant or home/webdev. My shell script needs to find out if the machine being setup has a dir that exists being either home/vagrant or home/webdev.

edit flag offensive delete link more

Comments

I get this error when I add the following to my pp file:

Error: Could not parse for environment production: Syntax error at '.' at /home/vagrant/manifest.pp:12 on ...
(more)
diamondz gravatar imagediamondz ( 2013-09-25 08:12:36 -0500 )edit
1

the custom fact is written in ruby and should be placed in `your_module/lib/facter/installpath.rb`: Facter.add(:installpath) do setcode do if File.directory? '/home/admin' '/home/admin ...(more)

Stefan gravatar imageStefan ( 2013-09-25 12:26:11 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-09-24 15:10:41 -0500

Seen: 4,777 times

Last updated: Sep 25 '13