Puppet - Values & Data Types

 • Most of the things you can do with the Puppet language involve some form of data.

• An individual piece of data is called a value, and every value has a data type, which determines what kind of information that value can contain and how you can interact with it.

• Strings are the most common and useful data type, but you’ll also have to work with others, including numbers, arrays, and some Puppet-specific data types like resource references


Puppet Data Types

• Strings

• Numbers

• Booleans

• Arrays

• Hashes

• Regular Expressions

• Sensitive

• Undef

• Resource References

• Default


Puppet Data Type - String

• Strings are unstructured text fragments of any length

• There are four ways to write literal strings in the Puppet language:

• Bare words:

• Bare word strings are most commonly used with resource attributes that accept a limited number of one-word values.

• Begin with a lower case letter, and contain only letters, digits, hyphens (-), and underscores (_).

• Not be a reserved word

• Single-quoted strings:

• Multi-word strings can be surrounded by single quotes

• Single-quoted strings can’t interpolate values

• Double-quoted strings:

• Strings can also be surrounded by double quotes

• Double-quoted strings can interpolate values. Interpolation allows strings to contain expressions, which will be replaced with their values

• Heredocs

• Heredocs let you quote strings with more control over escaping, interpolation, and formatting.

• They’re especially good for long strings with complicated content.

service { "ntp": ensure => running, # bare word string }


Puppet Data Type – Numbers, Booleans

• Numbers in the Puppet language are normal integers and floating point numbers.

• Booleans are one-bit values, representing true or false.

• The condition of an “if” statement expects an expression that resolves to a boolean value. All of Puppet’s comparison operators resolve to boolean values, as do many functions.


Puppet Data Type – Arrays, Hashes

• Arrays are ordered lists of values

• Resource attributes which accept multiple values (including the relationship metaparameters) generally expect those values in an array

• Many functions also take arrays, including the iteration functions

• You can access items in an array by their numerical index (counting from zero)

• Hashes map keys to values, maintaining the order of the entries according to insertion order

• Hashes are written as a pair of curly braces containing any number of key/value pairs

• A key is separated from its value by a => (arrow, fat comma, or hash rocket), and adjacent pairs are separated by commas

[ 'one', 'two', 'three’ ] # Equivalent: [ 'one', 'two', 'three', ]

{ 'key1' => 'val1', key2 => 'val2’ } # Equivalent: { 'key1' => 'val1', key2 => 'val2', }


Puppet Data Type – Regular Expressions

• A regular expression is a pattern that can match some set of strings, and optionally capture parts of those strings for further use

• Sensitive types in the Puppet language are strings marked as sensitive.

• The value is displayed in plain text in the catalog and manifest, but is redacted from logs and reports.

• Because the value is currently maintained as plain text, you should only use it as an aid to ensure that

if $host =~ /^www(\d+)\./ { notify { "Welcome web server #$1": } }

$secret = Sensitive('myPassword’) notice($secret)


Puppet Data Type – Undef Resource Reference

• Puppet’s special undef value is roughly equivalent to nil in Ruby; it represents the absence of a value.

• Resource references identify a specific Puppet resource by its type and title.

• Several attributes, such as the relationship metaparameters, require resource references

subscribe => File['/etc/ntp.conf’], before => Concat::Fragment['apache_port_header'],


The general form of a resource reference is:

• The resource type, capitalized (every segment must be capitalized if the resource type includes a namespace separator [::])

• An opening square bracket

• The title of the resource as a string, or a comma-separated list of titles


Puppet Data Type – Undef Resource Reference

• Puppet’s special default value usually acts like a keyword in a few limited corners of the language

file { default: mode => '0600’, owner => 'root’, group => 'root’, ensure => file, }

• All of the resources in the block above will inherit attributes from default unless they specifically override them.


Exercise  - Applying Conditional Logic in Puppet

 Create a file conditionals.pp in master terminal window and add the following text to it. In this file we add 3 resources to our system

cd /etc/puppet/manifests

nano conditionals.pp

$my_variable = ‘One’

if $my_variable == ‘One’ {

notify {‘The value of my variable is One’:}

}

 Go to the master terminal and run this command.

puppet apply conditionals.pp

 Go to the master terminal and update apache.pp to add the following

nano conditionals.pp

$my_variable = ‘One’

if $my_variable == ‘One’ {

notify {‘The value of my variable is One’:}

}

elsif $my_variable == ‘Two’ {

notify {‘The value of my variable is Two’:}

}

 Go to the master terminal and run this command.

puppet apply conditionals.pp

 Go to the master terminal and update apache.pp to add the following

nano conditionals.pp

$my_variable = ‘Five’

if $my_variable == ‘One’ {

notify {‘The value of my variable is One’:}

}

elsif $my_variable == ‘Two’ {

notify {‘The value of my variable is Two’:}

}

else {

notify {“The value of my variable is $my_variable”:}

}

 Go to the master terminal and run this command.

puppet apply conditionals.pp

 Go to the master terminal and update apache.pp to add the following

nano conditionals.pp

case $my_variable {

‘One’: {

notify {‘The value of my variable is One’:}

}

default: {

{“The value of my variable is $my_variable in the case statement”:}

}

}

Comments

Popular posts from this blog

Terraform

Scrum Master Interview help - Bootcamp

Kubernetes