PUPPET – Working with cases and variables

Since we are aware about creating basic modules and concept of classes as well. Let’s extend our knowledge to use variables and cases.
 
USING CASE STATEMENT:
 
Conditional statements let Puppet codes behave differently in different situations. They are most helpful when combined with facts or with data from an external source.
 
Like “if” statements, case statements choose one of several blocks of arbitrary Puppet code to execute. They take a control expression and a list of cases and code blocks, and will execute the first block whose case value matches the control expression.
 
Case {
                :  { }
                :  { }
                …
                :  { }
                <Non-Matching value> : { custom operation }
                }
 
USING VARIABLES:
 
$variables always start with a dollar sign. You assign to variables with the = operator.
Variables can hold strings, numbers, booleans, arrays, hashes, and the special undef value.
Variable can be of two types : A short local name / A long fully-qualified name
 
PUPPET CODE DEPLOYMENT
 
In below example we have used $testvalue as variable to store value.
 
Let’s have a look on puppet module “case_study” written to explain the concept. Unlike our previous example of cron resource, this time we are picking file resource here. Notice, the use of case here:

 

   1.  Create a module “case_study” , with a file type resource. The filename here is /etc/hostinfo . Do not confuse it with testfile given in title, it’s just a name that puppet clients will refer (it can be same or different as actual path of file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Notice :We have used a $hostname as a matching pattern here. Now the question here is if $hostname is a variable , from where it’s picking it’s value. The answer to the question here is, we are using $hostname as a facter.
FACTER:  Puppet uses a tool called Facter, which discovers some system information, normalizes it into a set of variables, and passes them off to Puppet. Puppet’s compiler then has access to those facts when it’s reading a manifest.
To see the actual available facts (including plugins) and their values on any of your system : ‘facter –p
 
 
 
 
 
 
 
 
 
    2. Like previous examples, update site.pp with further class information, to be available globally for all clients. (Refer : http://learningtechnix.blogspot.in/2016/08/puppet-writing-our-first-cron-module.html)
 
3. Testing on clinet : ‘puppet agent –tv’ : Client here will provide its facter value (asked here as $hostname ) to puppet master to have value of testfile variable updated accordingly.
 
NOTE : Puppet clients provide their information to puppet master in terms of facter only in normal run as well, in order to enable master to prepare a catalogue for each client accordingly.
    (a)    Testing on test4.hgangwar.com
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
(b)    Testing on clientvm.hgangwar.com
 
 
 
 
 
 
 
 
Just to confirm, each node have its separate facter values
 
 
 
 
 
 
 
 
 
Enjoy the values are updated . Hoping this article is useful for you.
Share feedback/queries in comments below.
 
References:
Facter: https://docs.puppet.com/facter/
Cases: https://docs.puppet.com/puppet/latest/reference/lang_conditional.html
Variables: https://docs.puppet.com/puppet/latest/reference/lang_variables.html

Leave a comment