ruby commands inside puppet function are not working right

asked 2018-07-12 05:35:40 -0500

Dhananjay.j.p gravatar image

I have created custom function to check if directory exists. code below

   
Puppet::Functions.create_function(:'Xbase::folder_exists') do

  dispatch :exists do
    param 'String', :folder_path
    #return_type 'Boolean'
  end

  def exists(folder_path)
    File.directory? folder_path
  end

end

If a call this function by passing existing directory path it returns true but if I create a new directory and pass that directory path then it returns false although directory is available.

Does anyone know why this is happening?

edit retag flag offensive close merge delete

Comments

Functions are executed on the master, not on the client, so it is checking if the directory exists on the puppet master while the catalog is being compiled, not if it exists on the client while it is being applied.

DarylW gravatar imageDarylW ( 2018-07-12 11:31:17 -0500 )edit

@DaryIW , Ok got your point. Can you suggest someway directory can be checked? thing is I have a patch to apply and if target directory is not there it fails,I dont want to create target directory as that directory is created by another process when it completes. So want to make sure directory exist

Dhananjay.j.p gravatar imageDhananjay.j.p ( 2018-07-12 23:49:50 -0500 )edit
1

In that case, what you can do, is create a custom fact whose value is true or false based on that condition. Then use it in your code to decide whether you need to enforce the other resources or not.

fvoges gravatar imagefvoges ( 2018-07-16 08:50:41 -0500 )edit

echo what fvoges said above, it's ugly, but it's the only way to do it, other than a custom resource which would be able to check those things for you (like a directory existing) as a part of it doing other things, but that probably is overkill for what you need.

DarylW gravatar imageDarylW ( 2018-07-16 11:00:14 -0500 )edit