Using “hiera”, it’s really simple to add structured variables in “.yaml” files.

“hiera” files

For this lab, I would like to create ma own “hiera” structure, so I create 2 directories :

[root@puppetserver ~]# mkdir -p /examples/my_hieradata

I put 2 “hiera” files in “/examples/my_hieradata” directory :

[root@puppetserver ~]# cd /examples/my_hieradata

The master “hiera” file :

[root@puppetserver my_hieradata]# cat my_hiera.yaml
---
:backends:
  - yaml
:hierarchy:
  - added_values

:yaml:
  :datadir: /examples/my_hieradata

Where I say :

  • Puppet must read “*.yaml” files
  • Those files will be in “/examples/my_hieradata” directory
  • Currently I defined only “added_values.yaml” configuration file

“my_values.yaml” file :

[root@puppetserver my_hieradata]# cat added_values.yaml
---

my_values:
  message: 'hello everybody !'
  array:
    - one
    - two
    - three

welcome_message: "I would like to say \"%{hiera('my_values.message')}\""

As you can see, it’s possible to ask “hiera” to retrieve a value (here : ‘my_values.message’) inside a “.yaml” file. ‘my_values.message’ could be defined in another “.yaml” file (be sure thath Puppet will read this file).

My manifest

I just wrote a short manifest containing a single class that I execute :

[root@puppetserver my_hieradata]# cd /examples
[root@puppetserver examples]# cat read_my_hieradata.pp
# declare the class :
class read_my_hieradata {
  $all_values_from_hiera = hiera('my_values')


  $my_array_from_hiera = hiera('my_values.array')
  notify { "just my array : ${my_array_from_hiera}": }

  notify { "my array : ${all_values_from_hiera['array']}": }

  $my_welcome_message = hiera('welcome_message')
  notify { "here is my message : ${my_welcome_message}": }

}

# execute the class :
class { ::read_my_hieradata: }

To retrieve my “array”, I used 2 methods :

  1. I used “hiera” to retrieve “my_values” hash, then using “[]” syntax, I got “array”
  2. I asked “hiera” to retrieve “array” using “.” syntax.

“welcome_message” construction was done in “added_values.yaml” YAML file

Manifest execution

I launched this command :

[root@puppetserver examples]# puppet apply read_my_hieradata.pp --hiera_config=/examples/my_hieradata/my_hiera.yaml
Notice: Compiled catalog for puppetserver.argonay.wou in environment production in 0.19 seconds
Notice: just my array : [one, two, three]
Notice: /Stage[main]/Read_my_hieradata/Notify[just my array : [one, two, three]]/message: defined 'message' as 'just my array : [one, two, three]'
Notice: my array : [one, two, three]
Notice: /Stage[main]/Read_my_hieradata/Notify[my array : [one, two, three]]/message: defined 'message' as 'my array : [one, two, three]'
Notice: here is my message : I would like to say "hello everybody !"
Notice: /Stage[main]/Read_my_hieradata/Notify[here is my message : I would like to say "hello everybody !"]/message: defined 'message' as 'here is my message : I would like to say "hello everybody !"'
Notice: Applied catalog in 0.11 seconds

Exactly what I expected …

 

en.pdf24.org    Send article as PDF   

Leave a Reply

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


*