Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chef cookbook - copy complete directory from files/default location in cookbook to a new location

Tags:

chef-infra

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.

like image 582
Web evangelist Avatar asked May 17 '16 03:05

Web evangelist


1 Answers

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

like image 149
Tim Brandes Avatar answered Sep 18 '22 14:09

Tim Brandes