Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Chef resources are an example of an operating system component in their ideal state. It is a declaration of configuration policy that explains the desired state of a node to be reached by resource providers using the existing configuration. Using Chef's Ohai mechanism helps to know the target machine's current condition.
This article explains the details of windows_choco_installer Resource along with the details of its syntax, actions, Common Resource Functionality, notifications, and guards.
Without further ado, let's get started.
Syntax
The windows choco installer resource can access the following properties, and their entire syntax is as follows:
Syntax:
windows_choco_installer 'name' do
action Symbol # defaults to :install if not specified
end
Let's look into the details of actions.
Actions
The actions of the resource windows_choco_installer are as follows:
:install It installs the Chocolatey package manager.
:nothing Unless prompted to do so by another resource, this resource block does nothing. This resource block is queued to run at the end of a Chef Infra Client run or runs immediately after being informed.
Let's dive into the details of common resource functionality.
Common Resource Functionality
Chef resources include resource guards, notifications, and common properties.
compile time
Ruby Type: false, true
Default Value : false
It controls the stage of the node's resource execution. You can run while the resource collection is being constructed by setting the value to true (the compile phase). You can also run when the Chef Infra Client is configuring the node by setting false (the converge phase).
ignore failure
Ruby Type: true, false, :quiet
Default value : false
If a resource fails for any reason, the recipe will still be executed. :quiet won't show the complete stack trace if a resource fails.
retries
Ruby Type: Integer
Default value : 0
The number of times the resource will be tried once any exceptions are caught.
retry_delay
Ruby Type: Integer
Default value : 2
It sets the seconds that pass between retry attempts.
sensitive
Ruby Type: false, true
Default Value : false
It makes sure that Chef Infra Client does not record critical resource information.
Let's dive into the details of notifications.
Notifications
When one resource's state changes, it may alert another resource to take appropriate action. Name the resource 'resource[name]', :action specifies the activity it should perform, and :timer sets a timer for that action. When notifying multiple resources, use a notified statement for each resource that needs to be informed. An error is triggered if the referenced resource is missing. In contrast, if the source resource cannot be located, subscribers will not fail. When a notification is run during the course of a Chef Infra Client run is determined by a timer. The following timers are available:
:before
It specifies that the action on a resource that has received a notification should be executed before processing the resource block that contains the notification.
:delayed
It is set by default. It specifies that an execution of a notification should be queued up for execution at the conclusion of a run of the Chef Infra Client.
:immediate,:immediately
It specifies that for each resource informed, a notification should be run right away.
The notifications syntax is:
Syntax:
notifies :action, 'resource[name]', :timer
subscribes
If the status of the resource being listened to changes, the resource listening to it may take action. Name the resource 'resource[name]', :action specifies the activity it should perform, and :timer sets a timer for that action.
Be aware that when you use subscribes, the resource you are listening to is not affected by the action you specify. For instance:
Code:
file '/etc/nginx/ssl/example.crt' do
mode '0600'
owner 'root'
end
service 'nginx' do
subscribes :reload, 'file[/etc/nginx/ssl/example.crt]', :immediately
end
In this instance, whenever the certificate file for nginx, found at /etc/nginx/ssl/example.crt, is modified, the subscribes property reloads the nginx service. The :reload action is executed for the resource (in this case, nginx) when a change is noticed by subscribes, which makes no modifications to the certificate file itself. There won't be a problem with the subscription if the other resource is nonexistent. In contrast, notifications have stricter semantics and will report an error if the other resource isn't there.
Let's look at the details of Guards.
Guards
During the execution stage of a Chef Infra Client run, the condition of a node can be assessed using a guard property. A guard property is then used to inform Chef Infra Client whether it should keep running a resource in response to the findings of this assessment. Both a Ruby block value and a string value are acceptable values for a guard property:
A string is used to run a shell command. The guard is applied if the command returns 0. The guard property is not used if the command returns any other value. In a powershell script, string guards can return true in addition to 0 and launch Windows PowerShell commands.
The Ruby code that executes a block must return either true or false. The guard property is used if the block returns true. The guard attribute is not used if the block returns false.
By allowing a resource to check for the desired state as it is being performed and then do nothing if the required state is present, a guard property is helpful for guaranteeing that a resource is idempotent.
Let's look at the properties of the resource.
Properties
During the execution stage of a Chef Infra Client run, a guard can be defined and evaluated using the following properties:
not_if It prevents the execution of a resource when a condition evaluates to true.
only_if It only permits the use of a resource if the condition returns true.
Let's look at a small example.
Example
The example below shows different methods for utilising the windows choco installer resource in recipes:
Code:
windows_choco_installer 'Install Chocolatey Package Manager' do
action :install
end
Frequently Asked Questions
What is windows_choco_installer?
The windows_choco_installer is a resource to install Chocolatey package manager.
What is the use of guards in chef resources?
A guard property is used to inform Chef Infra Client whether it should keep running a resource in response to the findings of this assessment.
What is the use of not_if guard property?
The not_if guard property prevents the execution of a resource when a condition evaluates to true.
Conclusion
In this article, we have extensively discussed the details of windows_choco_installer Resource along with the details of its syntax, actions, Common Resource Functionality, notifications, and guards.