49

I'm learning Chef and I'm going to do right now for Ubuntu:

execute "add-apt-repository ppa:#{node[:some_repo]}" do
  user "root"
end

execute "apt-get update" do
  user "root"
end

but may be there is a better ("chef-style"?) way to do it. Also, I concerned that sometimes add-apt-repository waits for "Enter" key on it's execution, so this approach might not work as is. What is the Right way of doing it?

Edit: I only have ppa link in format: ppa:something/user

Artem
  • 7,275
  • 15
  • 57
  • 97

4 Answers4

67

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
Joel Handwell
  • 742
  • 1
  • 10
  • 18
Leo Gamas
  • 1,231
  • 9
  • 8
  • 2
    How to find out what is key server and key if I only have PPA name/username? – Artem Mar 06 '12 at 18:20
  • I don't know how you could get the key, but the keyserver i'm almost certain is the same as above: "keyserver.ubuntu.com" – Leo Gamas Mar 07 '12 at 15:46
  • The PPA will have information on the keyserver and key to use. – jtimberman Mar 09 '12 at 15:31
  • 4
    Where would one put that code? Could I put it in my own cookbook, which does a bunch of other stuff, for instance? – James Smith Jan 15 '13 at 16:34
  • `node['lsb']['codename']` is not always availabe. The lsb node is empty when running in VirtualBox for example. Any suggestions on how we can determine the distribution reliably? – Peter May 27 '13 at 16:41
  • 1
    Oh, you need to have the lsb-release package installed. And that's not included in Debian minimal :( – Peter May 27 '13 at 16:46
  • It's outdated. Is there any new cookbooks? – Dmitry May 27 '15 at 09:25
  • 1
    The linked documentation is super useful. https://supermarket.chef.io/cookbooks/apt I just used it to add a repo in a slightly non-standard format. A++ would buy again ;- ) – jorfus Jan 22 '16 at 23:46
16

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.

Eric Drechsel
  • 2,694
  • 2
  • 23
  • 24
  • What functionality does the ppa method have over the apt_repository LWRP mentioned in Leo Gamas answer above? – DonBecker Jul 14 '14 at 16:33
  • We used to build our own inhouse as well. And recently moved it all to utilize the community cookbook. There are just too many edge cases around aptitude and discovering repositories efficiently to care. :) In this case, you just have to provide the complete launchpad URL to the repo and not just `user/repo` and it will work with `apt_repository`. – Till Aug 01 '14 at 14:48
7

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

jorfus
  • 2,804
  • 27
  • 23
2

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.

jorfus
  • 2,804
  • 27
  • 23