Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

CIS 527

Lecture 8 - Puppet 2

Learning Puppet

Much of this lecture's content is adapted from the Learning Puppet series on http://docs.puppetlabs.com/learning/

Variables

$my_variable = "A bunch of text"
notify {$my_variable:}

Variable Notes

  • Always starts with a dollar sign $
  • Assign with an equals sign =
  • Can hold strings, numbers, arrays, booleans, etc.
  • Can use variables as any value inside a resource declaration, including the title

Variable Notes

  • Variables are interpolated in double-quoted strings
    $username = "russfeld"
    notify {"Your home directory is /home/${username}": }
  • Variables can only be assigned once

Facts

  • Puppet uses Facter to learn about systems
  • Facter "facts" are stored as variables
  • Facts can be used in manifest files

Facter

#> facter -p

Facter

#> facter -p

architecture => i386
bios_version => 6.00
block_devices => sda
domain => localdomain
id => root
...

Facter

List of facts

Conditionals

if <boolean> {
    <code>
}
elsif <boolean> {
    <code>
} 
else {
    <code>
}

Booleans

$boolean = "false"
if $boolean {
    notify{"This is true":}
}
else {
    notify{"This is false":}
}

Booleans

$boolean = "false"
if $boolean {
    notify{"This is true":}
}
else {
    notify{"This is false":}
}

Notify: This is true

Booleans

  • All facts are strings
  • All non-empty strings are true
  • Use str2bool(<string>)

Booleans

$boolean = "false"
if str2bool("$boolean") {
    notify{"This is true":}
}
else {
    notify{"This is false":}
}

Booleans

$boolean = "false"
if str2bool("$boolean") {
    notify{"This is true":}
}
else {
    notify{"This is false":}
}

Notify: This is false

Case Statement

case $operatingsystem {
  centos: { $apache = "httpd" }
  # Note that these matches 
  # are case-insensitive.
  redhat: { $apache = "httpd" }
  debian: { $apache = "apache2" }
  ubuntu: { $apache = "apache2" }
  default: { fail("Unrecognized OS") }
}

Case Statement

case $operatingsystem {
  centos, redhat: { $apache = "httpd" }
  debian, ubuntu: { $apache = "apache2" }
  default: { fail("Unrecognized OS") }
}

Can also use Regular Expressions

Case Statement

$apache = $operatingsystem ? {
  centos                => 'httpd',
  redhat                => 'httpd',
  /(?i)(ubuntu|debian)/ => 'apache2',
  default               => undef,
}

Classes

class my_class {
  notify {"This does something":}
}

include my_class

Classes

  • Classes can contain any Puppet code
  • Names must be all lowercase, with numbers and underscores allowed
  • Classes are defined to make them available, and declared to be evaluated

Variable Scope

  • Classes create a new variable scope
  • Use $<class_name>::<var_name> to access
  • They cannot be assigned outside the declaring class

Modules

  • You can separate classes into modules for organization
  • Each module has a specific file structure
  • Modules are automatically loaded by Puppet from certain folders
  • /etc/puppetlabs/puppet/puppet.conf

Module Structure

/etc/puppetlabs/puppet/modules/
  module_name/
    manifests/
      init.pp
    files/
    templates/
    lib/

Module Structure

  • init.pp must contain a single class definition with the same name as the module
  • Modules can contain other classes, files, and templates

Site Manifest

/etc/puppetlabs/puppet/manifests/site.pp

  • Global manifest file for client/server
  • Includes class / module declarations

include ntp
include apache
include mysql

Puppet Forge

Assignments

  • Lab Help Session Friday 9 - 10 AM!
  • Lab 2 - Configuration Management - Due Sunday, Feb 23th by 11:59 PM
  • Automate configuration using Puppet
  • Turn in:
    • CIS Transient Storage
    • via Storage Media in class Monday Feb 24th