Puppet is build to use modules which encloses one or many manifests. It’s not really convenient when, for example, you just want to install few packages on some nodes (you don’t want to create a full module to just do that).

In this lab, we will create a module “like”, and put a manifest inside.

modulepath

After Puppet 4.x server installation, you got this default value :

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

This values is in fact :

$codedir/environments/$environment/modules:$basemodulepath

where :

  • $codedir :
[root@puppetserver ~]# puppet config print codedir
/etc/puppetlabs/code
  • $environment :
[root@puppetserver ~]# puppet config print environment
production
  • $basemodulepath :
[root@puppetserver ~]# puppet config print basemodulepath
/etc/puppetlabs/code/modules:/opt/puppetlabs/puppet/modules

Add a modulepath

Edit “puppet.conf” :

[root@puppetserver ~]# egrep "^basemodulepath" $(puppet config print confdir)/puppet.conf
basemodulepath = $codedir/modules:/opt/puppetlabs/puppet/modules:/examples/standalone_manifests

So, we have now :

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

Create a pseudo module

Create this directory :

[root@puppetserver ~]# mkdir -p /examples/standalone_manifests && cd /examples/standalone_manifests

Create a “pseudo module” directory :

[root@puppetserver standalone_manifests]# mkdir easy && cd easy

Then create a “manifests” directory :

[root@puppetserver easy]# mkdir manifests

Add some metadata :

[root@puppetserver easy]# cat metadata.json
{
  "name": "me-pseudo_module",
  "version": "ersion: no version managed! Just some manifests outside any module!",
  "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
}

This will be displayed :

[root@puppetserver easy]# puppet module list --modulepath=/examples/standalone_manifests
Warning: Missing dependency 'puppetlabs-stdlib':
  'me-pseudo_module' (ersion: no version managed! Just some manifests outside any module!) requires 'puppetlabs-stdlib' (>= 1.0.0)
/examples/standalone_manifests
└── me-pseudo_module (version: no version managed! Just some manifests outside any module!)

An example

We create this very simple class :

[root@puppetserver easy]# cat manifests/my_manifest.pp
class easy::my_manifest {
  notify { "we are executing \"${name}\" class": }
}

To execute this class :

[root@puppetserver easy]# puppet apply -e 'class { "easy::my_manifest": }'
Notice: Compiled catalog for puppetserver.argonay.wou in environment production in 0.19 seconds
Notice: we are executing "easy::my_manifest" class
Notice: /Stage[main]/Easy::My_manifest/Notify[we are executing "easy::my_manifest" class]/message: defined 'message' as 'we are executing "easy::my_manifest" class'
Notice: Applied catalog in 0.09 seconds

Use hiera to ask Puppet server to execute this class

Don’t forget to restart Puppet server :

[root@puppetserver easy]# systemctl restart puppetserver

Here is my default hiera configuration :

[root@puppetserver easy]# 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:

I just created “common.yaml” file :

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

classes:
  - 'easy::my_manifest'

and I wrote this manifest to execute hiera instructions :

[root@puppetserver easy]# cat $(puppet config print manifest)/use_hiera.pp
# tell Puppet server to use hiera :
node default {
  hiera_include('classes')
}

Now, it’s time to ask Puppet server to execute my class :

[root@puppetserver easy]# 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 '1478122399'
Notice: we are executing "easy::my_manifest" class
Notice: /Stage[main]/Easy::My_manifest/Notify[we are executing "easy::my_manifest" class]/message: defined 'message' as 'we are executing "easy::my_manifest" class'
Notice: Applied catalog in 0.13 seconds

 

www.pdf24.org    Send article as PDF   

Leave a Reply

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


*