How to Check If a Folder Exists in Chef

How to check if a directory exists in a Chef recipe NOT_IF?

I would suggest checking out out the ark cookbook for handling remote archive packages.

include_recipe "ark"

ark 'RevoMath' do
url 'https://mran.revolutionanalytics.com/install/RevoMath-1.0.1.tar.gz'
end

Which will install the tar package content into the /usr/local/RevoMath-1.0.1 directory. These defaults can be overridden.

Check for existing directory fails in Ruby + Chef

I was not so attentive earlier, I thought you are checking for file existence in not_if or only_if block. Your problem is similar to the one in this question: Chef LWRP - defs/resources execution order. See the detailed explanation there.

Your problem is that !File.exist?("/vol/postgres/data") code gets executed straight away - (because it's pure ruby), before any resource is executed and thus before the postgress is installed.

The solution should be to move the check to not_if block.

execute "mv /var/lib/postgresql/9.1/main /vol/postgres/data" do
not_if { File.exist?("/vol/postgres/data") }
end

Chef - Dir.exists? guard treating symlink as directory

The first time, the guard checks if it's a directory. Consequent run it can check if the file directory is a symlink. Try

directory "#{ENV['GS_HOME']}/logs/" do
action :delete
only_if { ::Dir.exist?("#{ENV['GS_HOME']}/logs/") || !::File.symlink?("#{ENV['GS_HOME']}/logs/") }
end

Chef Recipe How To Check If File Exists

As mentioned in the comments, for a deletion action, the if statement is unnecessary, as mentioned, because if chef doesn't find the file to be deleted, it will assume it was already deleted.

Otherwise, you generally want to use guard properties in the resource (available for all resources), rather than wrapping a resource in an if-then.

file '/var/www/html/login.php' do
only_if { ::File.exist?('/var/www/html/login.php') }
action :touch
end

And you probably also want to familiarize yourself with the Ruby File class methods.

How to check if the directory is symlink in chef

The selected answer will not work on Windows or systems where Bash is the default interpreter. You should use a Ruby solution to be cross-platform (and faster, since there's no process spawning):

directory '/var/www/html' do
action :delete
not_if { File.symlink?('/var/www/html') }
end

Check if some files in array exist in directory

This is my solution after numerous attempts:

origin_files = ['test1.rb', 'test2.rb']
dir_path = "C:\\Test"

ruby_block "Rename file" do
block do
for filename in origin_files
newname = filename.split(".")[0] + '.origin'
if ::File.exist?("#{dir_path}\\#{newname}")
Chef::Log.info("### Your file: #{newname} already renamed ###")
else
::File.rename("#{dir_path}\\#{filename}", "#{dir_path}\\#{newname}")
end
end
end
end


Related Topics



Leave a reply



Submit