Ohai,Linux Commands and Users and Groups in Chef
In this article we will see Ohai(update system configuration details,execute linux commands instead of ruby script and create users
and groups in Chef.
What is Ohai?
It is a system discovery tool.
It gathers system information.
It stores current information of your machine.
The data stores in key and value phase.
Ohai is a tool that is used to collect system configuration data, which is provided to the chef-client for use within cookbooks.\
Ohai is run by the chef-client at the beginning of every Chef run to determine system state.
Get IP Address from Ohai stored
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ohai ipaddress [ "172.31.42.243" ] |
Get hostname from Ohai store
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ohai hostname [ "ip-172-31-42-243" ] |
Get memory/total from ohai store
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ohai memory/total [ "1007272kB" ] |
Get cpu/0/mhz from ohai store
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ohai cpu/0/mhz [ "2400.000" ] |
What are Attributes?
Attributes represent information about your node.
Attributes are collected by Ohai.
We have a web application to be deployed into 1000 nodes.
we need to know some details of each server.
Because we need to mention that in configuration file of each node.
This information is vary from system to system.
These details we call as Attributes.
chef-client too gathers these Attributes from ohai store and puts in configuration files.
Instead of hard coding these Attributes , we mention as variables.
The types of attributes Ohai collects include but are not limited to:
Operating System
Network
Memory
Disk
CPU
Kernel
Host names
Fully qualified domain names
Virtualization
Cloud provider metadata
Implementation Steps
To get Configuration Details of your machine
Create a new Recipe “ktexperts-sample-recipe” inside cookbook “ktexperts-apache-cookbook”.
Go to cookbooks directory.
Open the recipe “ktexperts-sample-recipe.rb” and write script to update configuration details.
Verify syntax of recipe.
Execute the Recipe/call chef-client.
Verify file “/robofile”
Execute Linux Commands
Open existing recipe “ktexperts-recipe.rb” and write script to execute linux commands
Verify syntax of recipe.
Execute the Recipe/call chef-client.
Verify file “ktexpertsdir” and directory “ktexpertsfile”.
Execute the Recipe/call chef-client Again
Create users and Groups
Create a new user “ramesh”
Open the recipe “ktexperts-recipe.rb” and write script to create a new user
Verify syntax of recipe
Execute the Recipe/call chef-client
Verify user “ramesh”
Create a new group “DevOps”
Open the recipe “ktexperts-recipe.rb” and write script to create a new group.
Verify syntax of recipe
Execute the Recipe/call chef-client
Verify group “DevOps”
Add user “ramesh’ to group “DevOps”
Open the recipe “ktexperts-recipe.rb” and write script to add existing user to existing group.
Verify syntax of recipe.
Execute the Recipe/call chef-client.
Verify user and group.
Another way to create user and group
Create user “Rammy” ,group “ktexperts” and file “rammyfile”
Open the recipe “ktexperts-recipe.rb” and write script to create user,group and file.
Verify syntax of recipe.
Execute the Recipe/call chef-client.
Verify user,group and file.
To get Configuration Details of your machine
Create a new Recipe “ktexperts-sample-recipe” inside cookbook “ktexperts-apache-cookbook”
Go inside the cookbook “ktexperts-apache-cookbook”
1 2 |
[root@ip-172-31-42-243 cookbooks]# cd ktexperts-apache-cookbook/ [root@ip-172-31-42-243 ktexperts-apache-cookbook]# |
Run below command to create recipe “ktexperts-sample-recipe” inside cookbook “ktexperts-apache-cookbook”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-243 ktexperts-apache-cookbook]# chef generate recipe ktexperts-sample-recipe Recipe: code_generator::recipe * directory[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/unit/recipes] action create (up to date) * cookbook_file[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/spec_helper.rb] action create_if_missing (up to date) * template[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/unit/recipes/ktexperts-sample-recipe_spec.rb] action create_if_missing - create new file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/unit/recipes/ktexperts-sample-recipe_spec.rb - update content in file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/unit/recipes/ktexperts-sample-recipe_spec.rb from none to 14352c (diff output suppressed by config) * directory[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/test/integration/default] action create (up to date) * template[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/test/integration/default/ktexperts-sample-recipe_test.rb] action create_if_missing - create new file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/test/integration/default/ktexperts-sample-recipe_test.rb - update content in file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/test/integration/default/ktexperts-sample-recipe_test.rb from none to 05953f (diff output suppressed by config) * template[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-sample-recipe.rb] action create - create new file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-sample-recipe.rb - update content in file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-sample-recipe.rb from none to 4b6bd4 (diff output suppressed by config) |
To see the list of files and directories in a tree structure
1 2 3 4 5 |
[root@ip-172-31-42-243 ktexperts-apache-cookbook]# tree .├── recipes │ ├──<strong> default.rb │ ├── ktexperts-apache-recipe.rb │ └── <span style="color: #ff0000;">ktexperts-sample-recipe.rb</span></strong> |
Go to cookbooks directory
1 2 |
[root@ip-172-31-42-243 ktexperts-apache-cookbook]# cd .. [root@ip-172-31-42-243 cookbooks]# |
Open the recipe “ktexperts-sample-recipe.rb” and write script to update configuration details
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-apache-cookbook/recipes/ktexperts-sample-recipe.rb file '/robofile' do content "This is to get Attributes HOSTNAME: #{node['hostname']} IPADDRESS: #{node['ipaddress']} CPU: #{node['cpu']['0']['mhz']} MEMORY: #{node['memory']['total']}" owner 'root' group 'root' action :create end |
Note
:wq! — to quit.
Verify syntax of recipe
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-apache-cookbook/recipes/ktexperts-sample-recipe.rb Syntax OK |
Execute the Recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr recipe"[ktexperts-apache-cookbook::ktexperts-sample-recipe]" [2019-12-25T07:01:09+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-apache-cookbook::ktexperts-sample-recipe"] Synchronizing Cookbooks: - ktexperts-apache-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 1 resources Recipe: ktexperts-apache-cookbook::ktexperts-sample-recipe * file[/robofile] action create - create new file /robofile - update content in file /robofile from none to 560f10 --- /robofile 2019-12-25 07:01:11.223169707 +0000 +++ /.chef-robofile20191225-4482-kx9z3i 2019-12-25 07:01:11.223169707 +0000 @@ -1 +1,6 @@ +This is to get Attributes + HOSTNAME: ip-172-31-42-243 + IPADDRESS: 172.31.42.243 + CPU: 2400.000 + MEMORY: 1007272kB - change owner from '' to 'root' - change group from '' to 'root' Running handlers: Running handlers complete Chef Infra Client finished, 1/1 resources updated in 01 seconds |
Verify file “/robofile”
1 2 3 |
[root@ip-172-31-42-243 cookbooks]# ls / bin home ktexperts-file2 media proc root sys cal opt robofile srv var |
To see the content of the file “/robofile”
1 2 3 4 5 6 |
[root@ip-172-31-42-243 cookbooks]# cat /robofile This is to get Attributes HOSTNAME: ip-172-31-42-243 IPADDRESS: 172.31.42.243 CPU: 2400.047 MEMORY: 1007272kB |
Execute Linux Commands
Open existing recipe “ktexperts-recipe.rb” and write script to execute linux commands
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts-recipe.rb #file '/ktexperts-file1' do # content 'Ktexperts is a knowledge sharing platform #It will help you to share knowledge together' # action :create #end execute "run a script" do command <<-EOH mkdir /ktexpertsdir touch /ktexpertsfile EOH end |
Note
#— It will comment the line and it won’t execute the line.
:wq! — to quit.
Verify syntax of recipe
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb Syntax OK |
Execute the Recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr recipe"[ktexperts-cookbook::ktexperts-recipe]" [2019-12-25T07:14:01+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 1 resources Recipe: ktexperts-cookbook::ktexperts-recipe * execute[run a script] action run - execute mkdir /ktexpertsdir touch /ktexpertsfile Running handlers: Running handlers complete Chef Infra Client finished, 1/1 resources updated in 01 seconds |
Verify file “ktexpertsdir” and directory “ktexpertsfile”
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ls / bin etc ktexpertsfile lib media proc run sys var boot home ktexperts-file1 lib64 mnt robofile sbin tmp dev ktexpertsdir ktexperts-file2 local opt root srv usr |
Note
Idempotency won’t work when we execute linux commands.
If we execute recipe again it will be overwrite the file and directory.
Execute the Recipe/call chef-client Again
we can see 1/1 resources means the recipe has been executed successfully and it has override file and directory and create new file and directory with same name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr recipe"[ktexperts-cookbook::ktexperts-recipe]" [2019-12-25T07:17:27+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 1 resources Recipe: ktexperts-cookbook::ktexperts-recipe * execute[run a script] action run - execute mkdir /ktexpertsdir touch /ktexpertsfile Running handlers: Running handlers complete Chef Infra Client finished, 1/1 resources updated in 01 seconds |
Create users and Groups
Create a new user “ramesh”
Open the recipe “ktexperts-recipe.rb” and write script to create a new user
1 2 3 4 5 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts-recipe.rb user 'ramesh' do action :create end |
Note
:wq! — to quit
Verify syntax of recipe
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb Syntax OK |
Execute the Recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr recipe"[ktexperts-cookbook::ktexperts-recipe]" [2019-12-25T08:37:09+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 1 resources Recipe: ktexperts-cookbook::ktexperts-recipe * linux_user[ramesh] action create - create user ramesh Running handlers: Running handlers complete Chef Infra Client finished, 1/1 resources updated in 01 seconds |
Verify user “ramesh”
1 2 |
[root@ip-172-31-42-243 cookbooks]# tail -1 /etc/passwd ramesh:x:1001:1001::/home/ramesh:/bin/bash |
Create a new group “DevOps”
Open the recipe “ktexperts-recipe.rb” and write script to create a new group.
1 2 3 4 5 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts-recipe.rb group "DevOps" do action :create end |
Note
:wq! — to quit
Verify syntax of recipe
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb Syntax OK |
Execute the Recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-cookbook::ktexperts-recipe]" [2019-12-28T07:53:44+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 2 resources Recipe: ktexperts-cookbook::ktexperts-recipe * linux_user[ramesh] action create (up to date) * group[DevOps] action create - create group DevOps Running handlers: Running handlers complete Chef Infra Client finished, 1/2 resources updated in 01 seconds |
Verify group “DevOps”
1 2 |
[root@ip-172-31-42-243 cookbooks]# tail -1 /etc/group DevOps:x:1006: |
Add user “ramesh’ to group “DevOps”
Open the recipe “ktexperts-recipe.rb” and write script to add existing user to existing group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts-recipe.rb user 'ramesh' do action :create end group "DevOps" do action :create end group "DevOps" do members"ramesh" append true end |
Note
:wq! — to quit
Verify syntax of recipe
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb Syntax OK |
Execute the Recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-cookbook::ktexperts-recipe]" [2019-12-28T08:04:05+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 3 resources Recipe: ktexperts-cookbook::ktexperts-recipe * linux_user[ramesh] action create (up to date) * group[DevOps] action create (up to date) * group[DevOps] action create - alter group DevOps - add missing member(s): ramesh Running handlers: Running handlers complete Chef Infra Client finished, 1/3 resources updated in 01 seconds |
Verify user and group
1 2 |
[root@ip-172-31-42-243 cookbooks]# tail -1 /etc/group DevOps:x:1006:ramesh |
Another way to create user,group and file
Create user “Rammy” ,group “ktexperts” and file “rammyfile”
Open the recipe “ktexperts-recipe.rb” and write script to create user,group and file.
1 2 3 4 |
root@ip-172-31-42-243 cookbooks]# vi ktexperts-cookbook/recipes/ktexperts-recipe.rb user "Rammy" group "ktexperts" file "/rammyfile" |
Note
:wq! — to quit
Verify syntax of recipe
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb Syntax OK |
Execute the Recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr recipe"[ktexperts-cookbook::ktexperts-recipe]" [2019-12-25T08:48:42+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-cookbook::ktexperts-recipe"] Synchronizing Cookbooks: - ktexperts-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 3 resources Recipe: ktexperts-cookbook::ktexperts-recipe * linux_user[Rammy] action create - create user Rammy * group[ktexperts] action create - create group ktexperts * file[/rammyfile] action create - create new file /rammyfile Running handlers: Running handlers complete Chef Infra Client finished, 3/3 resources updated in 01 seconds |
Verify user,group and file
Verify user “Rammy”
1 2 |
[root@ip-172-31-42-243 cookbooks]# tail -1 /etc/passwd Rammy:x:1002:1003::/home/Rammy:/bin/bash |
Verify group “ktexperts”
1 2 |
[root@ip-172-31-42-243 cookbooks]# tail -1 /etc/group ktexperts:x:1004: |
Verify file “rammyfile”
1 2 3 4 |
[root@ip-172-31-42-243 cookbooks]# ls / bin etc ktexpertsfile lib media proc root srv usr boot home ktexperts-file1 lib64 mnt rammyfile run sys var dev ktexpertsdir ktexperts-file2 local opt robofile sbin tmp |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
priya
I would highly recommend beginners to definitely go through this article. thank you….ramesh