Reputation: 135
I am a beginner to Chef. Can any one please advise if there is a way to copy a directory inside cookbook's files/default
directory to a different location.
E.g. I have a directory structure with files a.txt
and b.txt
in files/
directory as follows cookbook_name/files/default/folder-name/[a.txt,b.txt]
. I want them both files to be a location /home/user/work/somelocation/folder-name/[a.txt,b.txt]
I have tried cookbook_file resource as follows:
cookbook_file '/home/user/work/somelocation/' do
source ['folder-name']
mode "0644"
action :create
end
and
cookbook_file '/home/user/work/somelocation/' do
source ['folder-name/a.txt',''folder-name/b.txt'']
mode "0644"
action :create
end
I am aware of other means of copying files between arbitrary directories by looping through, but I am keen to know if there is more elegant way to handle directories akin to how cookbook_file handles files from standard folders inside cookbook's files/default
directory.
Upvotes: 12
Views: 15510
Reputation: 3488
I'm facing a similar problem and ATM the near thing to a solution I have is that in a recipe run_context.cookbook_collection[<cookbook-name>].file_filenames
is an array with the complete path in destination for all the files under <cookbook>/files
for example in my config for a directory like this:
files/
default/
foo.txt
bar.txt
baz/foo.txt
It returns a directory like
[
"/opt/kitchen/cache/cookbooks/cookbook/files/default/foo.txt",
"/opt/kitchen/cache/cookbooks/cookbook/files/default/bar.txt",
"/opt/kitchen/cache/cookbooks/cookbook/files/default/baz/foo.txt"
]
But this is not very useful to me
Upvotes: 0
Reputation: 2213
remote_directory allows you to copy a whole directory to a location of your choice. For example:
remote_directory "/etc/some_target_directory" do
source 'local_directory' # <-- this is your directory in files/default/local_directory
files_owner 'root'
files_group 'root'
files_mode '0750'
action :create
recursive true
end
Further reading: https://docs.chef.io/resource_remote_directory.html
Upvotes: 20
Reputation: 37580
There is AFAIK no "good/clean" way to copy all files that are contained in a cookbook.
In order to create multiple files, you can apply simple ruby logic to loop over these files:
['a.txt', 'b.txt'].each do |file|
cookbook_file "/home/user/work/somelocation/#{file}" do
source "folder-name/#{file}"
mode "0644"
end
end
This will create multiple cookbook_file
resources (what @jamesgaddum is talking of).
P.S. the default/
part in files/default/
is optional since couple of Chef versions.
Upvotes: 2
Reputation: 19
Unlikely as cookbook_file
uses a checksum to compare the existing and new file, so requires a unique cookbook_file
resource for each.
Upvotes: 1