Chef notifies and subscribes explained with examples
A notification is a property on a resource that listens to other resources in the resource collection and then takes actions based on the notification type (notifies or subscribes).
Timers
A timer specifies the point during the chef-client run at which a notification is run. The following timers are available:
:before
Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
:delayed
Default. Specifies that a notification should be queued up, and then executed at the very end of the chef-client run.
:immediate, :immediately
Specifies that a notification should be run immediately, per resource notified.
Notifies
A resource may notify another resource to take action when its state changes. Specify a ‘resource[name]’, the :action that resource should take, and then the :timer for that action. A resource may notify more than one resource; use a notifies statement for each resource to be notified.
The syntax for notifies is:
notifies :action, ‘resource[name]’, :timer
Example
The following examples show how to use the notifies notification in a recipe.
By default, notifications are :delayed, that is they are queued up as they are triggered, and then executed at the very end of a chef-client run. To run an action immediately, use :immediately:
and then the chef-client would immediately run the following:
execute ‘test-nagios-config’ do
command ‘nagios3 –verify-config’
action :nothing
end
Subscribes
A resource may listen to another resource, and then take action if the state of the resource being listened to changes. Specify a ‘resource[name]’, the :action to be taken, and then the :timer for that action.
Note that subscribes does not apply the specified action to the resource that it listens to – for example:
In this case the subscribes property reloads the nginx service whenever its certificate file, located under /etc/nginx/ssl/example.crt, is updated. subscribes does not make any changes to the certificate file itself, it merely listens for a change to the file, and executes the :reload action for its resource (in this example nginx) when a change is detected.
The syntax for subscribes is:
subscribes :action, ‘resource[name]’, :timer
Examples
The following examples show how to use the subscribes notification in a recipe.
Prevent restart and reconfigure if configuration is broken
Use the :nothing action (common to all resources) to prevent the test from starting automatically, and then use the subscribes notification to run a configuration test when a change to the template is detected:
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
What is Metadata in chef? In order to understand cookbooks without evaluating them every files, we generate metadata about cookbooks What is metadata.rb? Before unerstanding about metadata.json, we need to understand about metadata.rb. Every cookbook requires a small amount of metadata. Metadata is stored in a file called metadata.rb that lives at the top of…
Write a cookbook using install version of latest tomcat and deploy a Jenkins.war file into tomcat web directory. Write a cookbook using you can install apache http server in CentOs and Ubuntu, enable the services and start the service. Write a cookbook to unzip a file, and then move a files from one location to…
Bash vs execute For a single script, use an execute. The bash resource is for including the script contents inline in the recipe code. Use the bash resource to execute scripts using the Bash interpreter. Commands that are executed with this resource are (by their nature) not idempotent, as they are typically unique to the…
Deployment tools with useful features are vital for end-to-end automation and streamlining of the product development process for various platforms including constant updates. Software Developers are eager to automate the process and take maximum advantage of their time in solving real-world problems. There are almost 20 application deployment tools available in the market. Every tool has…
Chef Infra Server The preferred way to run Chef in a large server farm is with each server (“node” in chef parlance) talking to a centrally-managed Chef server. You can use Opscode’s hosted chef server, or you can use an on-premises chef server. See https://manage.opscode.com/ for more details. The Chef Infra Server acts as a…
Submit the assignment on www.Debug.School! Rajesh Kumar I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO…