Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eclecticmiraclecat/9dfc80243ec8b1a81657fcdeeb98c7da to your computer and use it in GitHub Desktop.
Save eclecticmiraclecat/9dfc80243ec8b1a81657fcdeeb98c7da to your computer and use it in GitHub Desktop.

notes from https://learning.oreilly.com/videos/puppet-5/9781789137125

Chapter 1: Getting started with puppet

Getting ready for puppet

$ git clone https://github.com/bitfield/puppet-beginners-guide-3.git

$ cd puppet-beginners-guide-3

$ ./scripts/start_vagrant.sh

$ vagrant ssh

Chapter 2: Creating your first manifest

Hello, puppet

Resource declaration template

RESOURCE_TYPE { TITLE:
    ATTRIBUTE => VALUE,
...
}

Create a text file

$ cat /examples/file_hello.pp 
file { '/tmp/hello.txt':
  ensure  => file,
  content => "hello, world\n",
}

$ sudo puppet apply /examples/file_hello.pp

$ cat /tmp/hello.txt 
hello, world

Managing packages

$ cat /examples/package.pp 
package { 'cowsay':
  ensure => installed,
}

$ sudo puppet apply /examples/package.pp

Services

$ cat /examples/service.pp 
service { 'sshd':
  ensure => running,
  enable => true,
}

$ sudo puppet apply /examples/service.pp

Combine config file, package and service

$ cat /examples/package_file_service.pp 
package { 'mysql-server':
  ensure => installed,
  notify => Service['mysql'],
}

file { '/etc/mysql/mysql.cnf':
  source => '/examples/files/mysql.cnf',
  notify => Service['mysql'],
}

service { 'mysql':
  ensure => running,
  enable => true,
}

Chapter 3: Managing your puppet code with git

Fetching and applying changes automatically

$ cd /etc/puppetlabs/code/environments
$ git clone https://github.com/blue/puppet.git production

$ cat /examples/files/run-puppet.sh 
#!/bin/bash
cd /etc/puppetlabs/code/environments/production && git pull
/opt/puppetlabs/bin/puppet apply manifests/


$ cat /examples/run-puppet.pp 
# Set up regular Puppet runs
file { '/usr/local/bin/run-puppet':
  source => '/etc/puppetlabs/code/environments/production/files/run-puppet.sh',
  mode   => '0755',
}

cron { 'run-puppet':
  command => '/usr/local/bin/run-puppet',
  hour    => '*',
  minute  => '*/15',
}

Chapter 4: Understanding puppet resources

Exec Resources

simulate manual interaction

$ cat /examples/exec.pp 
exec { 'install-cat-picture-generator':
  cwd     => '/tmp/cat-picture-generator',
  command => '/tmp/cat-picture/generator/configure && /usr/bin/make install',
  creates => '/usr/local/bin/cat-picture-generator',
}

onlyif Attribute

depends on onlyif exit status

$ cat /examples/exec_onlyif.pp 
exec { 'process-incoming-cat-pictures':
  command => '/usr/local/bin/cat-picture-generator --import /tmp/incoming/*',
  onlyif  => '/bin/ls /tmp/incoming/*',
}

logoutput Atttibute

for debugging

$ cat /examples/exec_logoutput.pp
exec { 'newaliases':
  command => '/usr/bin/newaliases',
  logoutput => true,
}

Chapter 5: Variables, Expressions and Facts

Introducing variables

String

$ cat /examples/variable_string.pp 
$php_package = 'php7.0-cli'

package { $php_package:
  ensure => installed,
}

Variable types

$ cat /examples/variable_simple.pp 
$my_name = 'Zaphod Beeblebrox'
$answer = 42
$scheduled_for_demolition = true

Variable inside string

$ cat /examples/variable_string_interpolation.pp
$my_name = 'John'
notice("hello, ${my_name}")

Array

$ cat /examples/variable_array.pp
$heights = [193, 120, 181, 164, 172]

$first_height = $heights[0]


$ cat /examples/resource_array.pp 
$dependencies = [
  'php7.0-cgi',
  'php7.0-cli',
  'php7.0-common',
]

package { $dependencies:
  ensure => installed,
}

Hash/Dictionary

$ cat /examples/variable_hash.pp 
$heights = {
  'john'    => 193,
  'rabiah'  => 120,
}

notice("John's height is ${heights['john']}cm.")


$ cat /examples/hash_attributes.pp
$attributes = {
  'owner' => 'ubuntu',
  'group' => 'ubuntu',
  'mode'  => '0644',
}

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

Introducing Expressions

Numeric calculation

$ cat /examples/expression_numeric.pp 
$value = (17 * 8) + (12 / 4) - 1
notice($value)

Boolean expression

$ cat /examples/expression_boolean.pp 
notice(11 > 10)
notice(10 >= 10)
notice('foo' == 'foo')
notice('foo' in 'foobar')
notice('foo' in ['foo', 'bar'])
notice('foo' in { 'foo' => 'bar' })
notice('foo' =~ /oo/)
notice('foo' =~ String)
notice(1 != 2)

Regex

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

Conditional

$ cat /examples/if.pp 
$install_perl = true
if $install_perl {
  package { 'perl':
    ensure => installed,
  }
} else {
  package { 'perl':
    ensure => absent,
  }
}

Finding out facts

$ facter | grep kernel
kernel => Linux

$ facter kernel
Linux
$ cat /examples/facts_hash.pp 
notice($facts['kernel'])

DEPRECATED facts

$ cat /examples/facts_hash_deprecated.pp 
notice($::'kernel')

multiple hash

$ facter os.architecture
amd64
$ cat /examples/facts_architecture.pp 
notice($facts['os']['architecture'])

fact conditional

$ cat /examples/fact_if.pp 
if $facts['os']['selinux']['enabled'] {
  notice('SELinux is enabled')
} else {
  notice('SELinux is disabled')
}

external facts or custom facts

$ cat /opt/puppetlabs/facter/facts.d/fact_external.txt 
cloud=aws

$ sudo facter cloud
aws
$ cat /examples/fact_cloud.pp 
case $facts['cloud'] {
  'aws': {
    notice('This is an AWS cloud server ')
  }
  'gcp': {
    notice('This is a Google cloud server')
  }
  default: {
    notice("I'm not sure which cloud I'm in!")
  }
}

script output fact AKA executable fact

$ cat /opt/puppetlabs/facter/facts.d/date.sh
#!/bin/bash
echo "date=`date +%F`"

$ /opt/puppetlabs/facter/facts.d/date.sh
date=2021-03-27

$ sudo facter date
2021-03-27

Iterating over arrays

bad example, multiple duplicated code

$ cat /examples/iteration_simple.pp 
file { '/usr/local/bin/task1':
  content => "echo I am task1\n",
  mode    => '0755',
}

file { '/usr/local/bin/task2':
  content => "echo I am task2\n",
  mode    => '0755',
}

file { '/usr/local/bin/task3':
  content => "echo I am task3\n",
  mode    => '0755',
}

for each

$ cat /examples/iteration_each.pp 
$tasks = ['task1', 'task2', 'task3']
$tasks.each | $task | {
  file { "/usr/local/bin/${task}":
    content => "echo I am ${task}\n",
    mode    => '0755',
  }
}

for each template

ARRAY.each | ELEMENT | {
BLOCK
}

iteration over hash/dictionary

$ cat /examples/iteration_hash.pp 
$nics = $facts['networking']['interfaces']
$nics.each | String $interface, Hash $attributes | {
  notice("Interface ${interface} has IP ${attributes['ip']}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment