Continuation of the previous lab(3rd)
Chef allows you to execute Linux commands inside recipes using the execute
resource. This is useful for tasks like creating directories, files, or running shell commands.
Login to Amazon Linux Machine:
sudo su
cd cookbooks
ls
apache-cookbook test-cookbook
Create a Recipe to Run Linux Commands:
vi test-cookbook/recipes/test-recipe.rb
Add the following code:
execute "run a script" do
command <<-EOH
mkdir /Zeeshandir
touch /Ahmadfile
EOH
end
Explanation:
execute
resource runs shell commands.EOH
(End of Here Document) allows writing multiple shell commands.Run the Recipe:
chef-client -zr "recipe[test-cookbook::test-recipe]"
Verify the Changes:
ls /
Chef allows creating system users and groups using the user
and group
resources.
Edit the Recipe:
vi test-cookbook/recipes/test-recipe.rb
Add the following code:
#The below code will create a new user
user "rockstar" do
action :create
end
Run the Recipe:
chef-client -zr "recipe[test-cookbook::test-recipe]"
Edit the Recipe:
vi test-cookbook/recipes/test-recipe.rb
Add the following code:
# The following code will create a group and add a member to it
group "Devops group" do
action :create
members 'shan'
append true
end
Run the Recipe:
chef-client -zr "recipe[test-cookbook::test-recipe]"
Verify the Group Creation:
cat /etc/group