Ohai,Linux Commands and Users and Groups in Chef

Share via:

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

Get hostname from Ohai store

Get memory/total from ohai store

Get cpu/0/mhz from ohai store

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”

Run below command to create recipe “ktexperts-sample-recipe” inside cookbook “ktexperts-apache-cookbook”

To see the list of files and directories in a tree structure

Go to cookbooks directory

Open the recipe “ktexperts-sample-recipe.rb” and write script to update configuration details

Note

:wq! — to quit.

Verify syntax of recipe

Execute the Recipe/call chef-client

Verify file “/robofile”

To see the content of the file “/robofile”

Execute Linux Commands

Open existing recipe “ktexperts-recipe.rb” and write script to execute linux commands

Note

#— It will comment the line and it won’t execute the line.

:wq! — to quit.

Verify syntax of recipe

Execute the Recipe/call chef-client

Verify file “ktexpertsdir” and directory “ktexpertsfile”

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.

Create users and Groups 

Create a new user “ramesh”

Open the recipe “ktexperts-recipe.rb” and write script to create a new user

Note

:wq! — to quit

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.

Note

:wq! — to quit

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.

Note

:wq! — to quit

Verify syntax of recipe

Execute the Recipe/call chef-client

Verify user and group

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.

Note

:wq! — to quit

Verify syntax of recipe

Execute the Recipe/call chef-client

Verify user,group and file

Verify user “Rammy”

Verify group “ktexperts”

Verify file “rammyfile”

 

Thank you for giving your valuable time to read the above information.

Follow us on 

Website :  www.ktexperts.com

Facebook Page : KTexperts

Linkedin Page : KT EXPERTS

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

2 thoughts on “Ohai,Linux Commands and Users and Groups in Chef

Add Comment