{"id":10896,"date":"2019-05-17T16:20:11","date_gmt":"2019-05-17T16:20:11","guid":{"rendered":"http:\/\/shivaprogramming.com\/blog\/?p=10896"},"modified":"2019-07-25T19:28:48","modified_gmt":"2019-07-25T19:28:48","slug":"puppet-syntax-for-selector-and-class","status":"publish","type":"post","link":"http:\/\/shivaprogramming.com\/blog\/puppet-syntax-for-selector-and-class\/","title":{"rendered":"Puppet syntax for   if condition, variable, selector and class"},"content":{"rendered":"<h3>Puppet Site:<a href=\"https:\/\/puppet.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/puppet.com\/<\/a><\/h3>\n<h3>Puppet Forge :<a href=\"https:\/\/forge.puppet.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/forge.puppet.com\/<\/a><\/h3>\n<h3>git and puppet&nbsp;<a href=\"https:\/\/puppet.com\/blog\/how-to-use-git-commit-hooks-puppet-enterprise\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/puppet.com\/blog\/how-to-use-git-commit-hooks-puppet-enterprise<\/a><\/h3>\n<p><\/p>\n<p><strong>Puppet variables=A&nbsp;variable&nbsp;hold the value for e.g. &nbsp;x=1 where x is a variable which holds value 1.<\/strong><\/p>\n<p><strong>Example that displays how to create and use variable in puppet<\/strong><\/p>\n<pre class=\"lang:default decode:true\">class linux{\n\n    $admintools=['git','nano','othersoftware']\n    \n   package { $admintools:\n      ensure=&gt;'installed',\n   }\n}<\/pre>\n<p><strong>&nbsp;Puppet selectors =A selector assigns one of a set of possible values to a defined variable based on the condition<\/strong><br \/>$ntpservice= variable<br \/>$osfamily = one of the fact from puppet<\/p>\n<pre class=\"lang:default decode:true\">$ntpservice=$osfamily ? {\n     'redhat' =&gt; 'ntpd',\n     'debian' =&gt; 'ntp',\n     'default' =&gt; 'ntp',\n}\n<\/pre>\n<p><strong>How to use it?<\/strong><\/p>\n<pre class=\"line-height:25 lang:default decode:true\">Replace \nservice {'ntpd':\n       ensure=&gt;'running',\n       enable =&gt; true,\n}\nWith\nservice{$ntpservice:\n       ensure =&gt; 'running',\n       enable =&gt;  true,\n}<\/pre>\n<p><strong>Class in puppet != Object Oriented Programming (oop) class &nbsp;in puppet class represent &nbsp;a named collection of resources declaration such as variable, selector or any other puppet code.&nbsp;<\/strong><strong>It is simply a code container. Class does help to maintain DRY(Don&#8217;t repeat yourself principle).<\/strong><\/p>\n<p><strong>How to create a class in puppet?<\/strong><\/p>\n<pre class=\"lang:default decode:true\">class linux{\n   package{ 'ntp':\n   ensure=&gt; 'installed',\n   }\n}<\/pre>\n<p><strong>How to use\/reuse a class?<\/strong><\/p>\n<pre class=\"lang:default decode:true\">node 'wiki'{ {class 'linux':} }\n\nnode 'wikiexample'{ {class 'linux':} }<\/pre>\n<p><strong>Use of If condition in puppet<\/strong><\/p>\n<pre class=\"lang:default decode:true \">\/\/checking a condition if osfamily is redhat then install package otherwise skip the block of code\nif $osfamily=='redhat'{\n  package{'php-xml':\n       ensure=&gt;'present',\n  }\n}<\/pre>\n<p><strong>A complete example showing &nbsp;if condition, variable, selector and class usages in a file init.pp (,pp is the extension for the puppet file)<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\/\/define this code under init.pp\nclass mediawiki{\n\n   $phpmysql=$osfamily ? {\n    'redhat' =&gt; 'php-mysql'.\n    'debian' =&gt; 'php5-mysql',\n    default  =&gt;  'php-mysql',\n  }\n  \n  package{$phpmysql:\n   ensure=&gt;'present',\n }\nif $osfamily=='redhat'{\n    package{'php-xml':\n    ensure=&gt;'present', \n   }\n }\n}\n\/\/end of code for init.pp\n\n\/\/create nodes.pp file\n\nnode 'wiki'{\n   class {'mediawiki':}\n}\n\nnode 'wikiTest'{\n   class {'mediawiki':}\n}\n\nnode 'wikiStage'{\n   class {'mediawiki':}\n}\n\nnode 'wikiProd'{\n   class {'mediawiki':}\n}<\/pre>\n<pre class=\"epub__programlisting \">RESOURCE_TYPE { TITLE:\n  ATTRIBUTE =&gt; VALUE,\n  ...\n}<\/pre>\n<p><\/p>\n\n\n<p class=\"wp-block-paragraph\">Puppet Commands: <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">List all certificates<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>puppet cert list --all  # This code will show list of certificates \npuppet cert sing -a  #Sign all certificate<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puppet Site:https:\/\/puppet.com\/ Puppet Forge :https:\/\/forge.puppet.com\/ git and puppet&nbsp;https:\/\/puppet.com\/blog\/how-to-use-git-commit-hooks-puppet-enterprise Puppet variables=A&nbsp;variable&nbsp;hold the value for e.g. &nbsp;x=1 where x is a variable which holds value 1. Example that&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,1],"tags":[],"class_list":["post-10896","post","type-post","status-publish","format-standard","hentry","category-puppet-configuration-management-tool","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/10896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/comments?post=10896"}],"version-history":[{"count":29,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/10896\/revisions"}],"predecessor-version":[{"id":11557,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/10896\/revisions\/11557"}],"wp:attachment":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/media?parent=10896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/categories?post=10896"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/tags?post=10896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}