Install chef on Linux: Step by step guide

  1. Install Chef Workstation: Install the Chef Workstation on your local machine using the following commands:
sudo rpm -Uvh https://packages.chef.io/files/stable/chef-workstation/21.7.553/el/7/chef-workstation-21.7.553-1.el7.x86_64.rpm
  1. Configure the Chef Workstation: Create a directory for your Chef repository and configure the Chef knife configuration file:
mkdir ~/chef-repo
cd ~/chef-repo
echo 'current_dir = File.dirname(__FILE__)
log_level                :info
log_location             STDOUT
node_name                "your_user_name"
client_key               "#{current_dir}/your_user_name.pem"
chef_server_url          "https://your_chef_server_url/organizations/your_org_name"
cookbook_path            ["#{current_dir}/../cookbooks"]' > ~/.chef/knife.rb

Replace your_user_name, your_chef_server_url, and your_org_name with your own values.

  1. Install Chef Client: Install the Chef Client on your Red Hat Linux machine using the following commands:
sudo rpm -Uvh https://packages.chef.io/files/stable/chef/17.6.14/el/7/chef-17.6.14-1.el7.x86_64.rpm
  1. Configure the Chef Client: Create a client configuration file and register the client with the Chef Server:
sudo mkdir /etc/chef
sudo touch /etc/chef/client.rb
sudo bash -c 'echo "log_level :info
log_location STDOUT
chef_server_url 'https://your_chef_server_url/organizations/your_org_name'
validation_client_name 'your_org_name-validator'
node_name 'your_client_name'
client_key '/etc/chef/your_client_name.pem'" >> /etc/chef/client.rb'
sudo chef-client -S https://your_chef_server_url/organizations/your_org_name -K /path/to/your_client_key.pem

Replace your_chef_server_url, your_org_name, your_client_name, and /path/to/your_client_key.pem with your own values.

  1. Create Cookbooks: Create a cookbook using the following command:
chef generate cookbook cookbooks/my_cookbook

Replace my_cookbook with the name of your cookbook.

  1. Write Recipes: Write recipes to configure your Red Hat Linux machine using the Chef DSL. For example, to install and configure Apache:
# cookbooks/my_cookbook/recipes/default.rb
package 'httpd'

service 'httpd' do
  action [:enable, :start]
end

file '/var/www/html/index.html' do
  content '<html><body><h1>Hello World</h1></body></html>'
end
  1. Test and Deploy: Test your cookbook using the Chef Test Kitchen and deploy it to your Red Hat Linux machine using the following command:
chef-client --local-mode --runlist 'recipe[my_cookbook]'

This will apply the cookbook to your local machine. To apply the cookbook to a remote machine, use the chef-client command with the --chef-zero-port and --chef-zero-host options to specify the Chef Server and the --node-name and --client-key options to specify the client configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *