I am wondering if there is a resource to download multiple files from remote using chef resource. I want to use:

remote_file 'Download remote file' do
  path /opt/
  source http:///xxx
  mode '0644'
  action :create

...for one file. What if I want to download multiple files in a same place with different urls? Or how can I modify this resource to make multiple downloads which have different URLs?

This is how I added attributes in recipe:

node['file']['name'].each do |pkg|
    remote_file "path/plugins/#{pkg}" do
        source "node['file']['url']/#{pkg}"
        action :create

in attributes

default['file']['name'] = %w(
  • See the remote_directory resource, documentation : docs.chef.io/resource_remote_directory.html – Tensibai Apr 27 '17 at 19:13
  • @Tensibai I couldn't see there anything with multiple URL's – pandey Apr 27 '17 at 19:38
  • Aww, sorry, I did miss this point, a loop sounds the way to go, you'll have to specify early h file BTW. I'll try an answer, but there can be typo – Tensibai Apr 27 '17 at 19:52

what about something like that:

}.each do |pkg|
        remote_file "/tmp/#{pkg}" do
          source "https://s3.amazonaws.com/tmp/mysql/#{pkg}"

       rpm_package pkg do
        source "/tmp/#{pkg}"
        action :install

another way:

urllist = {
  { 'url': 'http://some.url1/', 'path': '/some/path1/', 'filename': 'some.file' },
  { 'url': 'https://some.url2/', 'path': '/some/path2/', 'filename': 'another.file'}

urllist.each do |urlinfo|
  remote_file "#{urlinfo['path']}/#{urlinfo['filename']}" do
    source "#{urlinfo['url']}/#{urlinfo['filename']}"
    owner 'someowner'
    group 'somegroup'
    mode 0755
| improve this answer | |
  • looks great @Berlin. is there ant way I can setup path to install them. I would like to install some plugins for Jenkins in a specified path like eg: '/opt/XXX/plugins/'. I want to download those plugins into plugins folder but some times it says plugins exists but its a folder – pandey Apr 27 '17 at 20:03
  • @Berlin it worked for me. Is there any way that I can write attribute for those rpm that you mentioned – pandey Apr 27 '17 at 20:12
  • mysql-community-common-5.7.16-1.el7.x86_64.rpm mysql-community-libs-5.7.16-1.el7.x86_64.rpm mysql-community-client-5.7.16-1.el7.x86_64.rpm mysql-community-server-5.7.16-1.el7.x86_64.rpm How can I write this thing as attribute – pandey Apr 27 '17 at 20:26
  • 1
    see below @Tensibai answer attributes/default.rb, is it what you looking for ? – Berlin Apr 27 '17 at 20:29
  • 1
    @berlin for information you can paste indented code, select it an press the code button (the {} ) or ctrl+k to indent a block with 4 spaces and keep original format – Tensibai Apr 27 '17 at 20:43

What I do for this case is using node attributes hash for a key value mapping as follow :

In attributes/default.rb


Or with hash notation :

default['namespace']['files']={'file1' => 'http://server1/path/source_fileX',

Then in recipe:

node['namespace']['files'].each do |filename,src|
  remote_file "/path/destinaton/#{filename}" do
    source src
    mode '0644'
    action :create

This way you can map a file with its source taking advantage that node attributes are a hash structure.

If course you can set the full path instead of just the file name and avoid the string interpolation in the recipe remote_file.

(Typed on phone, please forgive typos)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.