Chef Chef est un framework d'automatisation d'infrastructure et de gestion de configuration écrit en Ruby et Erlang.
Installation et exécution autonome (chef-solo)
Installer le client Chef sur les nœuds avec le script officiel Omnitruck :
# Installer le client Chef
curl -L https://omnitruck.chef.io/install.sh | sudo bash
# Vérifier l'installation
chef-solo -v
Exécuter un cookbook de manière autonome :
chef-solo -c solo.rb -j node.json
Commandes de la CLI knife
# Rechercher dans le registre Supermarket
knife supermarket search mysql
# Télécharger l'archive d'un cookbook
knife supermarket download nginx
# Amorcer un nœud distant via SSH
knife bootstrap 192.168.1.50 -x root -P 'password' -N web01
Ressources fondamentales du DSL de recettes
Paquets et services
package 'nginx' do
action :install
end
service 'nginx' do
action [:enable, :start]
supports status: true, restart: true, reload: true
end
Téléchargements de fichiers distants
remote_file '/usr/local/src/node.tar.gz' do
source 'https://nodejs.org/dist/v18.0.0/node-v18.0.0.tar.gz'
checksum 'sha256checksumhere...'
mode '0644'
action :create_if_missing
end
Exécuter des commandes shell
execute 'compile_app' do
command './configure && make && make install'
cwd '/usr/local/src/app'
environment 'PATH' => "/usr/local/bin:#{ENV['PATH']}"
creates '/usr/local/bin/app' # Garde : exécuter uniquement si le binaire n'existe pas
end
Gardes conditionnelles (only_if & not_if)
execute 'migrate_database' do
command 'rails db:migrate'
cwd '/var/www/app/current'
only_if { File.exist?('/var/www/app/current/Rakefile') }
not_if 'pg_isready -h localhost'
end