Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
All of us know who a chef is. It’s someone who cooks food, isn’t it? But, just as Python is a type of snake and a programming language, Chef doesn’t only mean a person who cooks.
The Chef we are talking about here is an open-source technology that can make servers and manage them. It has recipes and cookbooks which are analogous to configuring elements and fundamental working units.
Now, let’s consider our Mom is the chef. Doesn’t she ask us to taste the food for salt? Then, based on our feedback, she modifies the recipe accordingly.
A chef resource works similarly. It uses the Othai mechanism to find the current state of a system to reach the desired state.
Now that we know what Chef resources are let us learn about a particular resource - the rescue_account resource.
Syntax
The general syntax for all the properties in the rescue_account resource is as follows:
Rescue_account ‘name’ do
account_name String
password String
action Symbol
end
Here, “rescue_account” is the resource and “name” is the name of the resource block. “account_name” and “password” are properties of the resource. “action” specifies the necessary steps the Chef Infra Client will take to bring the node to its desired state. It defaults to :create if nothing is specified.
Actions
The rescue_account resource can perform certain actions. They are specified below.
(i) :create - It is used to create a user-specified in a property field.
(ii) :delete - It deletes the named user.
(iii) :disable - It turns off an account if it was enabled.
(iv) :enable - It turns on an account if it was disabled.
(v) :nothing - Here, the resource block does not act unless notified by another resource. When another resource notifies the block, it either runs immediately or waits in the queue to run at the end of a Chef Infra Client run.
Properties
Every single Chef resource has specific properties. The properties of rescue_account resources are:
(i) account_name - The user’s name is to be created as a rescue_account resource.
(ii) password - The user’s password for their rescue_account resource.
Common Resource Functionality
The common resource functionalities of Chef resources are basic functions that the resource uses to run. Namely, they are common properties, notifications, and guards. Let us see what they are in detail.
Common Properties
Common properties are the properties that are common to all resources in Chef. These properties are:
(i) compile_time - This property controls the phase during which a resource is run. The compile_time is set to true while building the resource (compile phase) and false when the Chef Infra Client configures the node (converge phase). Its default value is false.
(ii) ignore_failure - This property is used to ignore a failure and continue running a recipe. Its default value is false.
(iii) retries - It specifies the number of attempts to catch exceptions and retry the resource. Its default value is 0.
(iv) retry_delay - This is the delay in seconds between two retry attempts. Its default value is 2.
(v) sensitive - This property ensures that the Chef Infra Client does not log sensitive resource data. Its default value is false.
Notifications
We are familiar with getting notifications on our phones. For example, a WhatsApp notification alerts us about a new message. This prompts us to take some action, that is, to reply to the message.
A notification in rescue_account resources is similar. They are used to notify a resource when to change its state. Some examples are:
(i) notifies - This function is used to notify a resource to take action. A resource may notify more than one resource using separate “notifies” statements for each resource. Its syntax is:
notfies :action, ‘resource[name]’, :timer
(ii) subscribes - The function executes the action mentioned only if there is a change in the resource specified. Its syntax is:
subscribes :action, ‘resource[name]’, :timer
In both the functionalities above, :timer represents when a particular action must be executed. These timers are of the following types:
(i) :before - This specifies that the action notified should run before processing the resource block.
(ii) :delayed - This specifies that the actions should be kept in queue and executed at the end of the Chef Infra Client run.
(iii) :immediate or :immediately - This specifies that the notified action should be executed immediately.
Guards
Guards make us think of someone who watches over an area to protect.
Guards in the rescue_account resource play a similar role. A guard property checks the state of a node during the execution phase of a Chef Infra Client run. It ensures the desired state is being executed by accepting a string value of a Ruby block value.
In case of string values, the command returns 0 if the guard is applied and any other value otherwise. For Ruby block values, true means the guard is used, while false means it isn’t.
The guard functionality has specific properties to define it during execution.
(i) not_if - This prevents a resource from executing when the condition returns true.
(ii) only_if - This allows a resource to execute only if the condition returns true.
Examples of rescue_account Resources in Recipes
Now that we have a general understanding of the rescue_account resource let us see some different applications in recipes.
Creating a Managed User Account
rescue_account ‘Configuring an account’ do
account_name ‘MyName’
password ‘12345678’
action :create
end
Deleting a Managed User Account
rescue_account ‘Deleting an account’ do
account_name ‘MyName’
action :delete
end
Enabling an Existing Managed User Account
rescue_account ‘Enabling an account’ do
account_name ‘MyName’
action :enable
end
Disabling an Existing Managed User Account
rescue_account ‘Disabling an account’ do
account_name ‘MyAdmin’
action :disable
end
Frequently Asked Questions
What is Chef?
Chef is an open-source technology that can make servers and manage them. It has recipes and cookbooks that are used to configure elements and are the fundamental working units in the servers.
What is a Chef resource?
A chef resource is a tool that uses the Othai mechanism to find the current state of a system and execute actions so it can reach its desired state.
Name some actions in the rescue_account resource.
Some actions in the rescue_account resource are: (i):create, (ii) :delete, (iii) :disable, (iv) :enable, (v) :nothing
Name the common properties of Chef resources.
The common properties of Chef resources are: (i) compile_time, (ii) ignore_failure, (iii) retries, (iv) retry_delay, (v) sensitive
What are the different timers available in the rescue_account resources?
The timers available in rescue_account resources are: (i) :before, (ii) :delayed, (iii) :immediate or :immediately
Conclusion
Chef has many resources available for use. In this article, we learned about the rescue_account resource in particular. We read about its actions, properties, and common functionalities. We also saw a few examples of the rescue_account resource in recipes.