How to create a directory with the server name where the cookbook is deployed to?

#1

Hello Professionals,

I am trying to create a directory with the node name where the cookbook is deployed to, I am trying to do this but no luck -

directory "F:\StagedSoftware\FS\#{node['hostname']}" do
action :create
not_if { ::Dir.exist?("F:\StagedSoftware\FS\#{node['hostname']}") }
end

This creates a directory named - #{node['hostname']} Instead im looking for the server name.

Can anyone please advice on how can this be done /

#2

In ruby, you need to escape backslashes with a double backslash when in double quotes. "F:\\StagedSoftware\\FS\\#{node['hostname']}" I think should work?

1 Like
#3

I have tried that as well wouldn't work still creates the directory - StagedSoftwareFS#{node['hostname']}

#4

You mentioned that:
This creates a directory named - #{node['hostname']} Instead im looking for the server name.

That sounds like you might be using single quotes instead of double quotes.

#5

directory "F:\StagedSoftware\FS\#{node['hostname']}" do
action :create
not_if { ::Dir.exist?("F:\StagedSoftware\FS\#{node['hostname']}") }
end

I'm using the double quotes but still it does not create the directory with the server name.

#6

The backslash is escaping the #

try

directory "F:\StagedSoftware\FS\#{node['hostname']}" do

action :create

not_if { ::Dir.exist?("F:\StagedSoftware\FS#{node['hostname']}") } # The directory resource is idempotent, so the guard is not needed

end

or

directory ::File.join(‘F:’, ‘StagedSoftware’, ’FS’, node['hostname']) do

action :create

end

#7

Thanks Dan, tried doing it the below way and it works -

directory "F:\\StagedSoftware\\FS\\#{node['hostname']}" do
action :create
not_if { ::Dir.exist?("F:\\StagedSoftware\\FS\\#{node['hostname']}") }
end