Right now I have the following in my Vagrantfile:
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = \"cookbooks\"
chef.add_recipe \"apt\"
just a friendly reminder that adding all those recipes inside the vagrant provision can quickly become unmanageable.
A better pattern is to create a chef role chef/my-fancy-role.rb
# Name of the role should match the name of the file
name "my-fancy-role"
# Run list function we mentioned earlier
run_list(
"recipe[apt]",
"recipe[build-essential]",
"recipe[chef-redis::source]",
"recipe[openssl]"
)
And then add this role to the Vagrantfile
provisioning section:
config.vm.provision :chef_solo do |chef|
chef.roles_path = "chef/roles"
chef.cookbooks_path = ["chef/site-cookbooks", "chef/cookbooks"]
chef.add_role "my-fancy-role"
end
For the recent Chef version, i.e version 14. You could also use https://docs.chef.io/resource_apt_update.html
apt_update
The below output was my experiment running that resource for chef_14.5.33 in local mode (zero):
curl -O https://packages.chef.io/files/stable/chef/14.5.33/ubuntu/18.04/chef_14.5.33-1_amd64.de
sudo dpkg -i chef_14.5.33-1_amd64.deb
mkdir -p cookbooks/hello/recipes/ && echo "apt_update" > cookbooks/hello/recipes/default.rb
sudo sh -c 'chef-client -z -o hello'
[2018-10-12T10:25:30+00:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 14.5.33
[2018-10-12T10:25:32+00:00] WARN: Run List override has been provided.
[2018-10-12T10:25:32+00:00] WARN: Run List override has been provided.
[2018-10-12T10:25:32+00:00] WARN: Original Run List: []
[2018-10-12T10:25:32+00:00] WARN: Original Run List: []
[2018-10-12T10:25:32+00:00] WARN: Overridden Run List: [recipe[hello]]
[2018-10-12T10:25:32+00:00] WARN: Overridden Run List: [recipe[hello]]
resolving cookbooks for run list: ["hello"]
Synchronizing Cookbooks:
- hello (0.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
* apt_update[] action periodic (up to date)
[2018-10-12T10:25:32+00:00] WARN: Skipping final node save because override_runlist was given
[2018-10-12T10:25:32+00:00] WARN: Skipping final node save because override_runlist was given
Running handlers:
Running handlers complete
Chef Client finished, 0/1 resources updated in 01 seconds
Without patching, this is a generic approach to the problem that will update on every run:
bash "update-apt-repository" do
user "root"
code <<-EOH
apt-get update
EOH
end
It may be worth considering that such a run, on every run, ties up a fair bit of system resources for about 30 seconds; you may want to have a special recipe named recipe::update_apt that you have run via cron or via some other event i.e.
chef-client -o "recipe[yourrecipe::update_apt]"
A lot of the other answers posted here are likely to cause warnings about resource cloning.
According to the Apt cookbook documentation, you're supposed to be able to make this happen by setting node['apt']['compile_time_update'] = true
, however I have never had much luck with this approach myself.
Here's what I do instead:
This will load the original apt-get update
resource and ensure that it runs without adding a duplicate entry to the resource collection. This will cause apt-get update
to execute during every Chef run during the compile phase:
# First include the apt::default recipe (so that `apt-get update` is added to the collection)
include_recipe 'apt'
# Then load the `apt-get update` resource from the collection and run it
resources(execute: 'apt-get update').run_action(:run)
Obviously, you'll also want to include the apt
cookbook in your metadata.rb file:
# ./metadata.rb
depends 'apt'
To run apt-get update at compile time, do:
e = execute "apt-get update" do
action :nothing
end
e.run_action(:run)
check https://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time
You can include the apt recipe in the very beginning:
include_recipe 'apt'
this will run the update command.