Skip to content

Commit 779595a

Browse files
authored
Merge pull request #9347 from garrettrowell/example_update
(maint) - Update Hiera Example
2 parents ca922ca + 213c6c2 commit 779595a

File tree

20 files changed

+148
-110
lines changed

20 files changed

+148
-110
lines changed

examples/hiera/README.md

+68-57
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,102 @@
1-
A working demo of Hiera with YAML and Puppet backends.
1+
A working demo of Hiera with YAML backend.
22
======================================================
33

44
This demo consists of:
55

6-
* A NTP module that has defaults for pool.ntp.org servers
7-
* A common data module where module users can create override data in pp files
8-
* A YAML data source in etc/hieradb where users can override data in yaml files
9-
* A couple of users modules that just notify the fact that they are being included
10-
* In Hiera data files a key called _classes_ that decides what to include on a node
6+
- A **NTP** module that has defaults for *pool.ntp.org* servers
7+
- A **YAML** data source in the *data/* directory where users can override data in yaml files
8+
- A **Users** module that has a few manifests that simply notify that they are being included
9+
- In Hiera data files a key called **classes** that decides what to include on a node
1110

1211
Below various usage scenarios can be tested using this module.
1312

14-
The examples below assume you have Hiera already installed and that you have
15-
hiera-puppet cloned from github and running these commands in _hiera-puppet/example_ as cwd.
13+
The examples below assume you:
14+
- Have the puppet-agent already installed
15+
- You have this repository cloned from github
16+
- Are running these commands from within the *examples/hiera* directory as cwd.
1617

1718
Module from forge with module defaults
1819
--------------------------------------
1920

20-
* Move the _modules/data_ directory to _modules/data.bak_ to avoid overrides
21-
used further in the example
22-
* Run puppet, creates _/etc/ntp.conf_ with ntp.org addresses
23-
* The _hiera\_include()_ function includes just _users::common_
24-
25-
<pre>
26-
$ mv modules/data modules/data.bak
27-
$ puppet apply --config etc/puppet.conf site.pp
28-
notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/ensure: defined content as '{md5}7045121976147a932a66c7671939a9ad'
29-
notice: /Stage[main]/Users::Common/Notify[Adding users::common]/message: defined 'message' as 'Adding users::common'
21+
- Comment out lines 6-8 of [data/common.yaml](data/common.yaml#L6-8) to avoid overrides used further in the example
22+
- Run a `puppet apply` to create a */tmp/ntp.conf* file containing the two *pool.ntp.org* addresses
23+
- The *users::common* class should also be present in your catalog
24+
25+
```shell
26+
$ sed -i '6,8 s/^/#/' data/common.yaml
27+
$ puppet apply site.pp --hiera_config=hiera.yaml --modulepath=modules
28+
Notice: Compiled catalog for node.corp.com in environment production in 0.04 seconds
29+
Notice: Adding users::common
30+
Notice: /Stage[main]/Users::Common/Notify[Adding users::common]/message: defined 'message' as 'Adding users::common'
31+
Notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/ensure: defined content as '{sha256}949c7247dbe0870258c921418cc8b270afcc57e1aa6f9d9933f306009ede60d0'
32+
Notice: Applied catalog in 0.02 seconds
3033
$ cat /tmp/ntp.conf
3134
server 1.pool.ntp.org
3235
server 2.pool.ntp.org
33-
</pre>
36+
```
3437
3538
Site wide override data in _data::common_
3639
-----------------------------------------
3740
38-
* Restore the _modules/data_ directory that has a class _data::common_ that declares site wide overrides
39-
* The _hiera_include()_ function includes just _users::common_
40-
41-
<pre>
42-
$ mv modules/data.bak modules/data
43-
$ puppet apply --config etc/puppet.conf site.pp
44-
notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/content: content changed '{md5}7045121976147a932a66c7671939a9addc2' to '{md5}8f9039fe1989a278a0a8e1836acb8d23'
45-
notice: /Stage[main]/Users::Common/Notify[Adding users::common]/message: defined 'message' as 'Adding users::common'
41+
- Remove the comments on lines 6-8 of [data/common.yaml](data/common.yaml#L6-8)
42+
- Run a `puppet apply` to update */tmp/ntp.conf* to contain the two *ntp.example.com* addresses
43+
- The *users::common* class should also be present in your catalog
44+
45+
```shell
46+
$ sed -i '6,8 s/^#//' data/common.yaml
47+
$ puppet apply site.pp --hiera_config=hiera.yaml --modulepath=modules
48+
Notice: Compiled catalog for node.corp.com in environment production in 0.04 seconds
49+
Notice: Adding users::common
50+
Notice: /Stage[main]/Users::Common/Notify[Adding users::common]/message: defined 'message' as 'Adding users::common'
51+
Notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/content: content changed '{sha256}949c7247dbe0870258c921418cc8b270afcc57e1aa6f9d9933f306009ede60d0' to '{sha256}28ced955a8ed9efd7514b2364fe378ba645ab947f26e8c0b4d84e8368f1257a0'
52+
Notice: Applied catalog in 0.02 seconds
4653
$ cat /tmp/ntp.conf
4754
server ntp1.example.com
4855
server ntp2.example.com
49-
</pre>
56+
```
5057
5158
Fact driven overrides for location=dc1
5259
--------------------------------------
5360
54-
* Set a fact location=dc1 that uses the YAML data in _etc/hieradb/dc1.yaml_ to override
55-
* Show that machines in dc2 would use site-wide defaults
56-
* The _hiera_include()_ function includes _users::common_ and _users::dc1_ as the data file for dc1 adds that
57-
58-
<pre>
59-
$ FACTER_location=dc1 puppet apply --config etc/puppet.conf site.pp
60-
notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/content: content changed '{md5}8f9039fe1989a278a0a8e1836acb8d23' to '{md5}074d0e2ac727f6cb9afe3345d574b578'
61-
notice: /Stage[main]/Users::Common/Notify[Adding users::common]/message: defined 'message' as 'Adding users::common'
62-
notice: /Stage[main]/Users::Dc1/Notify[Adding users::dc1]/message: defined 'message' as 'Adding users::dc1'
61+
- Override the location fact to `dc1` to demonstrate *data/dc1.yaml* overrides the *ntp::config::ntpservers* values in *data/common.yaml*
62+
- `dc1` nodes will
63+
- have the *users::common* and *users::dc1* in their catalogs
64+
- */tmp/ntp.conf* will contain the two *ntp.dc1.example.com* addresses
65+
- Show that the nodes in `dc2` would use the site-wide defaults
66+
67+
```shell
68+
$ FACTER_location=dc1 puppet apply site.pp --hiera_config=hiera.yaml --modulepath=modules
69+
Notice: Compiled catalog for node.corp.com in environment production in 0.04 seconds
70+
Notice: Adding users::dc1
71+
Notice: /Stage[main]/Users::Dc1/Notify[Adding users::dc1]/message: defined 'message' as 'Adding users::dc1'
72+
Notice: Adding users::common
73+
Notice: /Stage[main]/Users::Common/Notify[Adding users::common]/message: defined 'message' as 'Adding users::common'
74+
Notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/content: content changed '{sha256}28ced955a8ed9efd7514b2364fe378ba645ab947f26e8c0b4d84e8368f1257a0' to '{sha256}39227f1cf8d09623d2e66b6622af2e8db01ab26f77a5a2e6d6e058d0977f369b'
75+
Notice: Applied catalog in 0.02 seconds
6376
$ cat /tmp/ntp.conf
6477
server ntp1.dc1.example.com
6578
server ntp2.dc1.example.com
66-
</pre>
67-
68-
Now simulate a machine in _dc2_, because there is no data for dc2 it uses the site wide defaults and
69-
does not include the _users::dc1_ class anymore
70-
71-
<pre>
72-
$ FACTER_location=dc2 puppet apply --config etc/puppet.conf site.pp
73-
warning: Could not find class data::dc2 for nephilim.ml.org
74-
notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/content: content changed '{md5}074d0e2ac727f6cb9afe3345d574b578' to '{md5}8f9039fe1989a278a0a8e1836acb8d23'
75-
notice: /Stage[main]/Users::Common/Notify[Adding users::common]/message: defined 'message' as 'Adding users::common'
79+
```
80+
81+
Now simulate a machine in `dc2`, because there is no data for `dc2` it uses the site wide defaults and
82+
does not include the *users::dc1* class anymore
83+
84+
```shell
85+
$ FACTER_location=dc2 puppet apply site.pp --hiera_config=hiera.yaml --modulepath=modules
86+
Notice: Compiled catalog for node.corp.com in environment production in 0.04 seconds
87+
Notice: Adding users::common
88+
Notice: /Stage[main]/Users::Common/Notify[Adding users::common]/message: defined 'message' as 'Adding users::common'
89+
Notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/content: content changed '{sha256}39227f1cf8d09623d2e66b6622af2e8db01ab26f77a5a2e6d6e058d0977f369b' to '{sha256}28ced955a8ed9efd7514b2364fe378ba645ab947f26e8c0b4d84e8368f1257a0'
90+
Notice: Applied catalog in 0.02 seconds
7691
$ cat /tmp/ntp.conf
7792
server ntp1.example.com
7893
server ntp2.example.com
79-
</pre>
94+
```
8095
81-
You could create override data in the following places for a machine in _location=dc2_, they will be searched in this order and the first one with data will match.
96+
You could create override data in the following places for a machine in *location=dc2*, they will be searched in this order and the first one with data will match.
8297
83-
* file etc/hieradb/dc2.yaml
84-
* file etc/hieradb/common.yaml
85-
* class data::dc2
86-
* class data::production
87-
* class data::common
88-
* class ntp::config::data
89-
* class ntp::data
98+
- file data/dc2.yaml
99+
- file data/&lt;environment&gt;.yaml
100+
- file data/common.yaml
90101
91-
In this example due to the presence of _common.yaml_ that declares _ntpservers_ the classes will never be searched, it will have precedence.
102+
In this example due to the presence of *common.yaml* that declares *ntpservers* the classes will never be searched, it will have precedence.

examples/hiera/data/common.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
classes:
3+
- users::common
4+
- ntp::config
5+
6+
ntp::config::ntpservers:
7+
- 'ntp1.example.com'
8+
- 'ntp2.example.com'
9+
10+
lookup_options:
11+
classes:
12+
merge: unique

examples/hiera/data/dc1.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
ntp::config::ntpservers:
3+
- 'ntp1.dc1.example.com'
4+
- 'ntp2.dc1.example.com'
5+
classes:
6+
- users::dc1

examples/hiera/etc/hiera.yaml

-15
This file was deleted.

examples/hiera/etc/hieradb/common.yaml

-3
This file was deleted.

examples/hiera/etc/hieradb/dc1.yaml

-6
This file was deleted.

examples/hiera/etc/hieradb/development.yaml

-2
This file was deleted.

examples/hiera/etc/puppet.conf

-3
This file was deleted.

examples/hiera/hiera.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
version: 5
3+
defaults:
4+
datadir: data
5+
data_hash: yaml_data
6+
7+
hierarchy:
8+
- name: 'Per Location'
9+
path: "%{facts.location}.yaml"
10+
11+
- name: 'Per Environment'
12+
path: "%{facts.environment}.yaml"
13+
14+
- name: 'Common Data'
15+
path: 'common.yaml'

examples/hiera/modules/data/manifests/common.pp

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
ntp::config::ntpservers:
3+
- '1.pool.ntp.org'
4+
- '2.pool.ntp.org'

examples/hiera/modules/ntp/hiera.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
version: 5
3+
defaults:
4+
datadir: data
5+
data_hash: yaml_data
6+
7+
hierarchy:
8+
- name: 'Common Data'
9+
path: 'common.yaml'
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
# lookup ntpservers from hiera, or allow user of class to provide other value
2-
class ntp::config($ntpservers = hiera('ntpservers')) {
3-
file{'/tmp/ntp.conf':
4-
content => template('ntp/ntp.conf.erb')
1+
# @summary Manage /tmp/ntp.conf file
2+
#
3+
# Given an array of ntpservers, manage the /tmp/ntp.conf file
4+
#
5+
# @example
6+
# include ntp::config
7+
#
8+
# @param ntpservers
9+
# An array of ntpserver(s) that should be present in the conf file
10+
class ntp::config(
11+
Array[String[1], 1] $ntpservers = undef,
12+
) {
13+
14+
file { '/tmp/ntp.conf':
15+
content => epp('ntp/ntp.conf.epp')
516
}
17+
618
}

examples/hiera/modules/ntp/manifests/data.pp

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<% $ntp::config::ntpservers.each |$server| { -%>
2+
server <%= $server %>
3+
<% } -%>

examples/hiera/modules/ntp/templates/ntp.conf.erb

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# notifies
1+
# @summary Notify to demonstrate users::common in catalog
2+
#
3+
# A common Class that all examples should include
4+
#
5+
# @example
6+
# include users::common
27
class users::common {
3-
notify{'Adding users::common': }
8+
notify { 'Adding users::common': }
49
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# notifies
1+
# @summary Notify to demonstrate users::dc1 in catalog
2+
#
3+
# A Class that should be present in dc1 node(s) catalog
4+
#
5+
# @example
6+
# include users::dc1
27
class users::dc1 {
3-
notify{'Adding users::dc1': }
8+
notify { 'Adding users::dc1': }
49
}

examples/hiera/modules/users/manifests/development.pp

-4
This file was deleted.

examples/hiera/site.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node default {
2-
hiera_include('classes')
2+
include lookup('classes')
33
}

0 commit comments

Comments
 (0)