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

Posted on Nov 30, 2022

Question

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

Answer

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