What is the right way to do add-apt-repository via Chef?

If you use chef v12.9 and above, Use the apt_repository resource for managing apt repositories. If you use chef lower than v12.8, you can use APT Cookbook provided by Chef Software, Inc. This cookbook provides same LWRP Following is the example usage of the resource:

apt_repository "nginx-php" do
  uri "http://ppa.launchpad.net/nginx/php5/ubuntu"
  distribution node['lsb']['codename']
  components ["main"]
  keyserver "keyserver.ubuntu.com"
  key "C300EE8C"
end

There is also a third-party apt cookbook that provides a ppa method:

ppa "user/repo"

https://github.com/sometimesfood/chef-apt-repo

Ideally this functionality should be added to the opscode apt cookbook.


Adding another answer since I just found myself back here. If you just have the URL for a key and not the key signature you can simply specify the URL in the key attribute:

apt_repository 'some_repo' do
  uri          'http://some_url/ubuntu/precise/amd64/'
  arch         'amd64'
  distribution 'precise'
  components   ['contrib']
  key          'https://some_key_url.com/debian/release.key'
end

From the documentation


One additional note is that once you have added the apt cookbook you should add a dependency statement to your cookbook. Update metadata.rb (should be in the base of your cookbook dir)

depends 'apt', '>= 2.7.0'

This will prevent the failure mode where a node can't update because it doesn't have the apt cookbook in it's runlist.