Variables

Define a variable as a string, number, or boolean

$variable_string = 'This is a string'
$variable_number = 42
$variable_boolean = true

Interpolate (insert the value of) a variable in a string

$favorite_food = 'cake'
notice("My favorite food is ${favorite_food}")

 

Arrays and Dictionaries

Create an array

$backpack_items = ['cheese', 'cake', 'machette']

Call the first value in an array

$first_item = $backpack_items[0]

Define a hash (dictionary)
Note: You are assigning a value to the corresponding ‘key’.

$age = {
  'mike'    => 26,
  'jack'    => 45,
  'lindsey' => 31,
}

Call a hash (dictionary)

notice("Mike is ${age['mike']}")

Use the ‘attribute splat operator’ to define attributes in a manifest

 $attributes = {
  'owner' => 'root',
  'group' => 'root',
  'mode'  => '0644',
}

file { '/tmp/testfile':
  ensure => present,
  *      => $attributes,
}

 

Expressions

Use arithmetic operators

$value = (5 * 2) + (100 / 20) - 63
notice($value)

Define arithmetic boolean expressions (evaluates to true or false)

notice(1 > 2)
notice(2 < 1)
notice(2 >= 1)
notice(2 <= 1)
notice(2 == 2)
notice(2 != 1)

Define string comparison boolean expressions (evaluates to true or false)

notice('foo' == 'foo')
notice('foo' in 'foobar')
notice('foo' in ['foo', 'bar'])
notice('foo' in { 'foo' => 'bar' }) # Has to be a key in hash
notice('foo' =~ /oo/) # Regular expression - / acts as the delimiter

Regular Expressions in Puppet (it is the same as ruby)

$check = 'foo'
notice($check ~= /foo/) # literal
notice($check ~= /f/) # substring 
notice($check ~= /f.*/) # f followed by zero or more characters
notice($check ~= /f.o/) # f, any character, o
notice($check ~= /fo+/) # f followed by one or more 'o's
notice($check ~= /[fgh]oo/) # f, g, or h followed by 'oo'

Conditional Expressions

$install_iotop = true
if $install_iotop {
  package { 'iotop':
    ensure => installed,
  }
} else {
  package { 'iotop':
    ensure => absent,
  }
}

Case Statements
This help resolve massive if/else statements

$webserver = 'nginx'
case $webserver {
  'nginx': {
    notice("Good choice for using nginx")
  }
  'apache:': {
    notice("Well, it's not IIS...")
  }
  default: {
    notice("I can't determine your webserver")
  }
}

 

Loops

Iterate over each element. These are called 'each loops' (just like 'for loops')

$tasks = ['task1', 'task2', 'task3']
$tasks.each | $task {
  notice("${task}")
}

Iterate over each element in a hash (dictionary)
When iterating over hashes the each loop takes 2 parameters: Key and Value

$nics = $facts['networking']['interfaces']
$nics.each | String $interface, Hash $attributes | {
  notice("Interface ${interface} has IP ${attributes['ip']}")
}

 

Facts

Show all facts available

facter -p

or
* This one includes 3rd party modules

puppet facts

Define a fact

$distro_name = $facts['os']['distro']['codename'] 

Set variables based on facts, i.e. innodb_buffer_pool_size

$buffer_pool = $facts['memory']['system']['total_bytes'] * 3/4
notice("innodb_buffer_pool size=${buffer_pool}")

Set a custom fact

echo "cloud=aws" >> /opt/puppetlabs/facter/facts.d/additionalfacts

Create an executable fact
vim /opt/puppetlabs/facter/facts.d/date.sh

#!/bin/bash
echo "date=`date`"

chmod +x /opt/puppetlabs/facter/facts.d/date.sh

Leave a Reply

Your email address will not be published. Required fields are marked *