{"id":674,"date":"2022-11-24T03:21:59","date_gmt":"2022-11-24T03:21:59","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2017\/07\/26\/chef-notifies-and-subscribes-explained-with-examples\/"},"modified":"2022-12-23T05:45:27","modified_gmt":"2022-12-23T05:45:27","slug":"chef-notifies-and-subscribes-explained-with-examples","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/chef-notifies-and-subscribes-explained-with-examples\/","title":{"rendered":"Chef Tutorials &#8211; notifies and subscribes explained with examples of Notifications"},"content":{"rendered":"<h3>Chef notifies and subscribes explained with examples<\/h3>\n<div>\u00a0<\/div>\n<div>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).<\/div>\n<div>\u00a0<\/div>\n<h3>Timers<\/h3>\n<div>A timer specifies the point during the chef-client run at which a notification is run. The following timers are available:<\/div>\n<div><strong>:before<\/strong><\/div>\n<div>Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.<\/div>\n<div>\u00a0<\/div>\n<div><strong>:delayed<\/strong><\/div>\n<div>Default. Specifies that a notification should be queued up, and then executed at the very end of the chef-client run.<\/div>\n<div>\u00a0<\/div>\n<div><strong>:immediate, :immediately<\/strong><\/div>\n<div>Specifies that a notification should be run immediately, per resource notified.<\/div>\n<div>\u00a0<\/div>\n<h3>Notifies<\/h3>\n<div>A resource may notify another resource to take action when its state changes. Specify a &#8216;resource[name]&#8217;, 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.<\/div>\n<div>The syntax for notifies is:<\/div>\n<div>notifies :action, &#8216;resource[name]&#8217;, :timer<\/div>\n<div>\u00a0<\/div>\n<div><strong>Example<\/strong><\/div>\n<div>The following examples show how to use the notifies notification in a recipe.<\/div>\n<div>\u00a0<\/div>\n<div><strong>Delay notifications<\/strong><\/div>\n<div>template &#8216;\/etc\/nagios3\/configures-nagios.conf&#8217; do<\/div>\n<div>\u00a0 # other parameters<\/div>\n<div>\u00a0 notifies :run, &#8216;execute[test-nagios-config]&#8217;, :delayed<\/div>\n<div>end<\/div>\n<div>\u00a0<\/div>\n<div><strong>Notify immediately<\/strong><\/div>\n<div>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:<\/div>\n<div>template &#8216;\/etc\/nagios3\/configures-nagios.conf&#8217; do<\/div>\n<div>\u00a0 # other parameters<\/div>\n<div>\u00a0 notifies :run, &#8216;execute[test-nagios-config]&#8217;, :immediately<\/div>\n<div>end<\/div>\n<div>\u00a0<\/div>\n<div>and then the chef-client would immediately run the following:<\/div>\n<div>execute &#8216;test-nagios-config&#8217; do<\/div>\n<div>\u00a0 command &#8216;nagios3 &#8211;verify-config&#8217;<\/div>\n<div>\u00a0 action :nothing<\/div>\n<div>end<\/div>\n<div>\u00a0<\/div>\n<h3>Subscribes<\/h3>\n<div>A resource may listen to another resource, and then take action if the state of the resource being listened to changes. Specify a &#8216;resource[name]&#8217;, the :action to be taken, and then the :timer for that action.<\/div>\n<div>\u00a0<\/div>\n<div>Note that subscribes does not apply the specified action to the resource that it listens to &#8211; for example:<\/div>\n<div>file &#8216;\/etc\/nginx\/ssl\/example.crt&#8217; do<\/div>\n<div>\u00a0 \u00a0mode &#8216;0600&#8217;<\/div>\n<div>\u00a0 \u00a0owner &#8216;root&#8217;<\/div>\n<div>end<\/div>\n<div>\u00a0<\/div>\n<div>service &#8216;nginx&#8217; do<\/div>\n<div>\u00a0 \u00a0subscribes :reload, &#8216;file[\/etc\/nginx\/ssl\/example.crt]&#8217;, :immediately<\/div>\n<div>end<\/div>\n<div>\u00a0<\/div>\n<div>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.<\/div>\n<div>The syntax for subscribes is:<\/div>\n<div>subscribes :action, &#8216;resource[name]&#8217;, :timer<\/div>\n<div>\u00a0<\/div>\n<div><strong>Examples<\/strong><\/div>\n<div>The following examples show how to use the subscribes notification in a recipe.<\/div>\n<div>\u00a0<\/div>\n<div>Prevent restart and reconfigure if configuration is broken<\/div>\n<div>\u00a0<\/div>\n<div>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:<\/div>\n<div>\u00a0<\/div>\n<div>execute &#8216;test-nagios-config&#8217; do<\/div>\n<div>\u00a0 command &#8216;nagios3 &#8211;verify-config&#8217;<\/div>\n<div>\u00a0 action :nothing<\/div>\n<div>\u00a0 subscribes :run, &#8216;template[\/etc\/nagios3\/configures-nagios.conf]&#8217;, :immediately<\/div>\n<div>end<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div><strong>Reference<\/strong><\/div>\n<div><a href=\"https:\/\/docs.chef.io\/resources.html#id4\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.chef.io\/resources.html#id4<\/a><\/div>\n<div>https:\/\/docs.chef.io\/resource_common\/#notifications<\/div>\n<div>\u00a0<\/div>\n<div><strong>Example<\/strong><\/div>\n<div><a href=\"https:\/\/gist.github.com\/nathanhoel\/f5433a0d9cfe2e717c71\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gist.github.com\/nathanhoel\/f5433a0d9cfe2e717c71<\/a><\/div>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_34937\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_43248\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_34937\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Chef notifies and subscribes explained with examples \u00a0 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). \u00a0 Timers A timer specifies the point during the chef-client run at which a notification is run. The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2867,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[88],"tags":[407,604,627,628,602,632,631,629,630],"class_list":["post-674","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chef","tag-chef","tag-chef-guide","tag-chef-notifies","tag-chef-subscribes","tag-chef-tutorials","tag-examples","tag-explanation","tag-notification","tag-subscription"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":7,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":31875,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions\/31875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/2867"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}