Hiera is the easiest way to store your variables for Puppet manifests.

We will run following examples on a Puppet server (Puppet 4.7.0).

Our server has a default hiera configuration :

[root@puppetserver ~]# cat /etc/puppetlabs/puppet/hiera.yaml
---
: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:

First example

We will add entries in “common.yaml” file :

[root@puppetserver ~]# cat /etc/puppetlabs/code/environments/production/hieradata/common.yaml
---

my_application::files:
  - file1
  - file2

Now, we can retrieve those variables :

[root@puppetserver ~]# puppet apply -e "notice(hiera('my_application::files'))"
Notice: Scope(Class[main]): [file1, file2]
Notice: Compiled catalog for puppetserver.argonay.wou in environment production in 0.31 seconds
Notice: Applied catalog in 0.10 seconds

Second example

Create this directory :

[root@puppetserver ~]# mkdir -p /etc/puppetlabs/code/environments/production/hieradata/nodes

Create this yaml file :

[root@puppetserver ~]# cat /etc/puppetlabs/code/environments/production/hieradata/nodes/anyhost.anydomain.net.yaml
---

my_configuration:
  my_files:
    - file1
    - file2
    - file3
    - file4

Execute this command :

[root@puppetserver ~]# puppet apply --certname=anyhost.anydomain.net -e "notice(hiera('my_configuration'))"
Notice: Scope(Class[main]): {my_files => [file1, file2, file3, file4]}
Notice: Compiled catalog for anyhost.anydomain.net in environment production in 0.17 seconds
Notice: Applied catalog in 0.06 seconds

Using a manifest

We can create a directory :

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

Here a simple manifest :

[root@puppetserver examples]# cat read_data.pp
class read_data {
  $my_hiera_data = hiera('my_configuration')
  notify { "$my_hiera_data": }
}

include ::read_data

We execute this manifest :

[root@puppetserver examples]# puppet apply --certname=anyhost.anydomain.net read_data.pp
Notice: Compiled catalog for anyhost.anydomain.net in environment production in 0.22 seconds
Notice: {my_files => [file1, file2, file3, file4]}
Notice: /Stage[main]/Read_data/Notify[{my_files => [file1, file2, file3, file4]}]/message: defined 'message' as '{my_files => [file1, file2, file3, file4]}'
Notice: Applied catalog in 0.07 seconds

This manisfest is now a little bit more sofisticated :

[root@puppetserver examples]# cat read_data.pp
class read_data {
  $my_hiera_data = hiera('my_configuration')
  notify { "Here list of files: ${my_hiera_data[my_files]}": }

  $command_argument = join ($my_hiera_data[my_files],' ')
  notify { "we're going to create \"${command_argument}\" files in \"/tmp\"": }
  exec { 'create_gaga_files':
    command => "touch ${command_argument}",
    cwd     => '/tmp',
    path    => '/usr/bin',
    timeout => 10,
  }
}

include ::read_data

Execute this manifest :

[root@puppetserver examples]# puppet apply --certname=anyhost.anydomain.net read_data.pp
Notice: Compiled catalog for anyhost.anydomain.net in environment production in 0.22 seconds
Notice: Here list of files: [file1, file2, file3, file4]
Notice: /Stage[main]/Read_data/Notify[Here list of files: [file1, file2, file3, file4]]/message: defined 'message' as 'Here list of files: [file1, file2, file3, file4]'
Notice: we're going to create "file1 file2 file3 file4" files in "/tmp"
Notice: /Stage[main]/Read_data/Notify[we're going to create "file1 file2 file3 file4" files in "/tmp"]/message: defined 'message' as 'we're going to create "file1 file2 file3 file4" files in "/tmp"'
Notice: /Stage[main]/Read_data/Exec[create_gaga_files]/returns: executed successfully
Notice: Applied catalog in 0.07 seconds

We check :

[root@puppetserver examples]# ll /tmp/file?
-rw-r--r--. 1 root root 0 Sep 29 15:47 /tmp/file1
-rw-r--r--. 1 root root 0 Sep 29 15:47 /tmp/file2
-rw-r--r--. 1 root root 0 Sep 29 15:47 /tmp/file3
-rw-r--r--. 1 root root 0 Sep 29 15:47 /tmp/file4

Yes ! It works !

 

PDF24 Tools    Send article as PDF   

Leave a Reply

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


*