Basic Puppet Template

RESOURCE_TITLE { 'TITLE':
  ATTRIBUTE => VALUE,
}

 

File Attribute

Ensure a file has specified content

file { '/tmp/hello.txt':
  ensure  => file,
  owner   => 'nikko',
  group   => 'wheel',
  mode    => '0644',
  content => "hello world\n",
}

Ensure a file exists with the contents from a URL

file { '/tmp/test':
  source => 'https://url.com/page',
}

Ensure a file exists with the contents from another file

file { '/tmp/test':
  source => '/path/to/file',
}

Ensure a directory exists

file { '/tmp/directory1':
  ensure => directory,
}

Copy contents of directory ‘recursively’

file { '/etc/mysql':
  source => '/git/repo/mysql_files',
  recurse => true,
}

Create a symbolic link

 file { '/usr/bin/python':
  ensure => link,
  target => '/usr/python',
}

 

Package Attribute

Verify a package is installed

package { 'iotop':
  ensure => installed,
  allow_virtual => false,
}

Verify a package is not installed

package { 'apparmor':
  ensure => absent,
}

Verify a package is not installed and the configuration files are removed

package { 'apparmor':
  ensure => purged,
}

Install Puppet-Ruby Gems

package { 'r10k':
  ensure   => installed,
  provider => 'puppet_gem,
}

Install a large amount of packages from an array

$dependencies [
  'php7.0-cgi',
  'php7.0-cli',
  'php7.0-common',
  'php7.0-gd',
  'php7.0-json',
  'php7.0-mcrypt',
  'php7.0-mysql',
  'php7.0-soap',
]

package { $dependencies:
  ensure => installed,
}

 

Service Attribute

Ensure a service is enabled at boot and running

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

Fix Puppet keeps restarting a service
The reason for this is because it cannot detect the status of the service. You need to define the pattern for it look for.

service { 'ntp':
  ensure    => running,
  enable    => true,
  hasstatus => false,
  pattern   => 'nptd',
}

Fix Puppet kills services
This is happening because Puppet tries to stop and then restart services. Puppet will at times timeout causing this issue. You can manually configure the way puppet restarts services.

service { 'mysql':
  ensure  => running,
  enable  => true,
  restart => '/bin/echo "Restarting MySQL" >> /var/log/messages && systemctl restart mysql',
}

 

User and Group Attributes

Create a User

user { 'nikko':
  ensure => present,
  uid    => 3005,
  home   => '/home/nikko',
  shell  => '/bin/bash',
  groups => ['wheel'],
}

Create a Group

group { 'devs':
  ensure => present,
  gid    => '6001',
}

Remove a user

user { 'nikko':
  ensure => absent,
}

Add SSH Keys

ssh_authorized_key { 'MyName':
  user => 'nikko',
  type => 'ssh-rsa',
  key  => 'AAAAB3NzaC1yc2EAAAADAQABAAABAQDHW67uuIyA0WfMsmKj7lEdncpbVivSBS+HkGllmCSrrZX61lYXFpuNSqCDDOSIVhutN0+VFKDBlequFRI4Nccl9E3vQS4NScWBfD5hoJ5+OCAdESjzxVGK5ytcJ7wkPKpKGqkdeSh/aC8RaQyHXXCoy4Dz8jmmxmKVFEK6ODPLQ0vzAslp5aQ8fkW9Zoob7QC1PqSsiXKvWhCvj5lMs+Vg5BJ0UOk16REr4TVbAY1xp/XA2PDidHdyUkZIa4IHyupEdCZKXyTT7fET1urWSU/cSbbAh16TIELox46bBKWmsySGd7cF9Gwv7lZgVMiNnIrFy4qvd/5J8gQkjGSGp28Z',
}

 

Other Attributes

Add a Cron Job

cron { 'updates':
  command => '/bin/yum update -y',
  hour    => '0',
  minute  => '0',
  weekday => 'Sunday',
}

Run any custom commands
Note: This will be run every time puppet runs. To resolve this have your commands create file and set this to “creates” as seen below.

 exec { 'install-some-custom-stuff':
  cwd      => '/tmp', # Current working directory
  command  => 'echo "Meow" > /tmp/meow-file',
  creates  => '/tmp/meow-file', # Alternatively, use: onlyif => '/bin/ls /tmp/me*' or unless => 'bin/ls /tmp/me*' for the opposite
}

 

Full examples

Full installation and configuration of MySQL via manifest

package { 'mysql-server':
  ensure => installed, ## or ensure => '10.2.12-1.el7.centos',
  notify => Service['mysql'],
}

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

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

Leave a Reply

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