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

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 /

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

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

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.

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.

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

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