Your Puppet server is just installed, and of course, you want to use it now !

[root@puppetserver ~]# systemctl status puppetserver
● puppetserver.service - puppetserver Service
   Loaded: loaded (/usr/lib/systemd/system/puppetserver.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2016-10-29 11:09:06 CEST; 18min ago
 Main PID: 2548 (java)
   CGroup: /system.slice/puppetserver.service
           └─2548 /usr/bin/java -Xms512m -Xmx1g -XX:MaxPermSize=256m -XX:OnOutOfMemoryError=kill -9 %p -Djava.security.egd=/dev/urandom -...

Oct 29 11:07:31 puppetserver.argonay.wou systemd[1]: Starting puppetserver Service...
Oct 29 11:07:31 puppetserver.argonay.wou java[2548]: OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support wa...in 8.0
Oct 29 11:09:06 puppetserver.argonay.wou systemd[1]: Started puppetserver Service.
Hint: Some lines were ellipsized, use -l to show in full.

Your first manifest

puppetlabs-stdlib

We need at least “puppetlabs-stdlib” module.

Default module paths :

[root@puppetserver ~]# puppet config print modulepath
/etc/puppetlabs/code/environments/production/modules:/etc/puppetlabs/code/modules:/opt/puppetlabs/puppet/modules

Modules will come from Puppet forge :

[root@puppetserver ~]# puppet config print module_repository
https://forgeapi.puppetlabs.com

Install this module :

[root@puppetserver ~]# puppet module install --modulepath=/opt/puppetlabs/puppet/modules puppetlabs-stdlib
Notice: Preparing to install into /opt/puppetlabs/puppet/modules ...
Notice: Downloading from https://forgeapi.puppetlabs.com ...
Notice: Installing -- do not interrupt ...
/opt/puppetlabs/puppet/modules
└── puppetlabs-stdlib (v4.13.1)

We currently only have this module :

[root@puppetserver ~]# puppet module list
/etc/puppetlabs/code/environments/production/modules (no modules installed)
/etc/puppetlabs/code/modules (no modules installed)
/opt/puppetlabs/puppet/modules
└── puppetlabs-stdlib (v4.13.1)

A simple class

Where to put manifests ?

[root@puppetserver ~]# puppet config print manifest
/etc/puppetlabs/code/environments/production/manifests

All manifests in this directory will be executed on all Puppet nodes (in order)

Go there :

[root@puppetserver ~]# cd $(puppet config print manifest)
[root@puppetserver manifests]#

Edit your first manifest :

[root@puppetserver manifests]# cat initial_manifest.pp
# class declaration :
class initial_manifest {
  notify { "we are executing \"${name}\" class on \"${::networking['fqdn']}\" node!": }
}

# class execution :
class { '::initial_manifest': }

Check syntax :

[root@puppetserver manifests]# puppet parser validate initial_manifest.pp

Execute this class (without using the Puppet server) :

[root@puppetserver manifests]# puppet apply initial_manifest.pp
Notice: Compiled catalog for puppetserver.argonay.wou in environment production in 0.14 seconds
Notice: we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!
Notice: /Stage[main]/Initial_manifest/Notify[we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!]/message: defined 'message' as 'we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!'
Notice: Applied catalog in 0.08 seconds

Ask Puppet server to execute this class :

[root@puppetserver manifests]# puppet agent -tv
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for puppetserver.argonay.wou
Info: Applying configuration version '1477737661'
Notice: we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!
Notice: /Stage[main]/Initial_manifest/Notify[we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!]/message: defined 'message' as 'we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!'
Notice: Applied catalog in 0.08 seconds

hiera

hiera configuration

Hiera configuration file :

[root@puppetserver manifests]# puppet config print hiera_config
/etc/puppetlabs/puppet/hiera.yaml

which contains (by default) :

[root@puppetserver manifests]# cat $(puppet config print hiera_config)
---
:backends:
  - yaml
:hierarchy:
  - "nodes/%{::trusted.certname}"
  - common

:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /etc/puppetlabs/code/environments/%{environment}/hieradata on *nix
# - %CommonAppData%\PuppetLabs\code\environments\%{environment}\hieradata on Windows
# When specifying a datadir, make sure the directory exists.
  :datadir:

Create “/etc/puppetlabs/code/environments/%{environment}/hieradata/common.yaml” file :

[root@puppetserver manifests]# cat /etc/puppetlabs/code/environments/production/hieradata/common.yaml
# hiera "common" file (production environment) :
---

classes:
  - '::common_module'

Module creation

Create a working directory :

[root@puppetserver manifests]# mkdir -p /examples/modules && cd /examples/modules

Generate required directories and files :

[root@puppetserver modules]# puppet module generate --skip-interview me-common_module

Notice: Generating module at /examples/modules/common_module...
Notice: Populating templates...
Finished; module generated in common_module.
common_module/Gemfile
common_module/Rakefile
common_module/examples
common_module/examples/init.pp
common_module/manifests
common_module/manifests/init.pp
common_module/spec
common_module/spec/classes
common_module/spec/classes/init_spec.rb
common_module/spec/spec_helper.rb
common_module/README.md
common_module/metadata.json

Here are created directories and files :

[root@puppetserver modules]# tree
.
└── common_module
    ├── examples
    │   └── init.pp
    ├── Gemfile
    ├── manifests
    │   └── init.pp
    ├── metadata.json
    ├── Rakefile
    ├── README.md
    └── spec
        ├── classes
        │   └── init_spec.rb
        └── spec_helper.rb

5 directories, 8 files

Add some instructions in the main manifest, for example :

[root@puppetserver modules]# grep -v '#' common_module/manifests/init.pp
class common_module {

  notify { "we are working on \"${::os['name']}\" release \"${::os['release']['full']}\"": }

}

The syntax looks good :

[root@puppetserver modules]# puppet parser validate common_module/manifests/init.pp

We execute this class without asking Puppet server :

[root@puppetserver modules]# puppet apply --modulepath=/examples/modules common_module/examples/init.pp
Warning: ModuleLoader: module 'common_module' has unresolved dependencies - it will only see those that are resolved. Use 'puppet module list --tree' to see information about modules
   (file & line not available)
Notice: Compiled catalog for puppetserver.argonay.wou in environment production in 0.18 seconds
Notice: we are working on "OracleLinux" release "7.2"
Notice: /Stage[main]/Common_module/Notify[we are working on "OracleLinux" release "7.2"]/message: defined 'message' as 'we are working on "OracleLinux" release "7.2"'
Notice: Applied catalog in 0.09 seconds

Good !

Create module archive :

[root@puppetserver modules]# puppet module build common_module
Notice: Building /examples/modules/common_module for release
Module built: /examples/modules/common_module/pkg/me-common_module-0.1.0.tar.gz

Install this module :

[root@puppetserver modules]# puppet module install --modulepath=/etc/puppetlabs/code/environments/production/modules --ignore-dependencies /examples/modules/common_module/pkg/me-common_module-0.1.0.tar.gz
Notice: Preparing to install into /etc/puppetlabs/code/environments/production/modules ...
Notice: Installing -- do not interrupt ...
/etc/puppetlabs/code/environments/production/modules
└── me-common_module (v0.1.0)

We have now following modules installed :

[root@puppetserver modules]# puppet module list
/etc/puppetlabs/code/environments/production/modules
└── me-common_module (v0.1.0)
/etc/puppetlabs/code/modules (no modules installed)
/opt/puppetlabs/puppet/modules
└── puppetlabs-stdlib (v4.13.1)

Tell Puppet server to use hiera

For the moment, our module will not be executed on nodes, we need to create a special class for that.

Go back to the manifest directory :

[root@puppetserver modules]# cd $(puppet config print manifest)

We still have our first manifest :

[root@puppetserver manifests]# ls -1
initial_manifest.pp

Create a manifest, for example “use_hiera.pp” :

[root@puppetserver manifests]# cat use_hiera.pp
# tell Puppet server to use hiera :
node default {
  hiera_include('classes')
}

Manifests will be executed by Puppet server in this order (“initial_manifest.pp” first, then “use_hiera.pp”) :

[root@puppetserver manifests]# ls -1
initial_manifest.pp
use_hiera.pp

Now, we are ready to ask Puppet server to do the job :

[root@puppetserver manifests]# puppet agent -tv
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for puppetserver.argonay.wou
Info: Applying configuration version '1477747548'
Notice: we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!
Notice: /Stage[main]/Initial_manifest/Notify[we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!]/message: defined 'message' as 'we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!'
Notice: we are working on "OracleLinux" release "7.2"
Notice: /Stage[main]/Common_module/Notify[we are working on "OracleLinux" release "7.2"]/message: defined 'message' as 'we are working on "OracleLinux" release "7.2"'
Notice: Applied catalog in 0.07 seconds
[root@puppetserver manifests]#

You want to upgrade this module ?

OK, go back to “/examples/modules” :

[root@puppetserver manifests]# cd /examples/modules

Update the manifest, for example :

[root@puppetserver modules]# grep -v '#' common_module/manifests/init.pp
class common_module {

  notify { "we are working on \"${::os['name']}\" release \"${::os['release']['full']}\"": }
  notify { 'added in the new version': }

}

Change the version :

[root@puppetserver modules]# cat common_module/metadata.json
{
  "name": "me-common_module",
  "version": "0.1.1",
  "author": "me",
  "summary": null,
  "license": "Apache-2.0",
  "source": "",
  "project_page": null,
  "issues_url": null,
  "dependencies": [
    {"name":"puppetlabs-stdlib","version_requirement":">= 1.0.0"}
  ],
  "data_provider": null
}

Rebuild this new version :

[root@puppetserver modules]# puppet module build common_module
Notice: Building /examples/modules/common_module for release
Module built: /examples/modules/common_module/pkg/me-common_module-0.1.1.tar.gz

Then install it :

[root@puppetserver modules]# puppet module install --force --modulepath=/etc/puppetlabs/code/environments/production/modules --ignore-dependencies /examples/modules/common_module/pkg/me-common_module-0.1.1.tar.gz
Notice: Preparing to install into /etc/puppetlabs/code/environments/production/modules ...
Notice: Installing -- do not interrupt ...
/etc/puppetlabs/code/environments/production/modules
└── me-common_module (v0.1.1)

We have now this new version “0.1.1” installed :

[root@puppetserver modules]# puppet module list
/etc/puppetlabs/code/environments/production/modules
└── me-common_module (v0.1.1)
/etc/puppetlabs/code/modules (no modules installed)
/opt/puppetlabs/puppet/modules
└── puppetlabs-stdlib (v4.13.1)

Ask Puppet server to execute :

[root@puppetserver modules]# puppet agent -tv
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for puppetserver.argonay.wou
Info: Applying configuration version '1477748830'
Notice: we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!
Notice: /Stage[main]/Initial_manifest/Notify[we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!]/message: defined 'message' as 'we are executing "initial_manifest" class on "puppetserver.argonay.wou" node!'
Notice: we are working on "OracleLinux" release "7.2"
Notice: /Stage[main]/Common_module/Notify[we are working on "OracleLinux" release "7.2"]/message: defined 'message' as 'we are working on "OracleLinux" release "7.2"'
Notice: added in the new version
Notice: /Stage[main]/Common_module/Notify[added in the new version]/message: defined 'message' as 'added in the new version'
Notice: Applied catalog in 0.09 seconds

As expected !

 

PDF24    Send article as PDF   

Leave a Reply

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


*