官术网_书友最值得收藏!

Configuring Hiera

Hiera is an information repository for Puppet. Using Hiera you can have a hierarchical categorization of data about your nodes that is maintained outside of your manifests. This is very useful for sharing code and dealing with exceptions that will creep into any Puppet deployment.

Getting ready

Hiera should have already been installed as a dependency on your Puppet master. If it has not already, install it using Puppet:

root@puppet:~# puppet resource package hiera ensure=installed
package { 'hiera':
 ensure => '1.3.4-1puppetlabs1',
}

How to do it...

  1. Hiera is configured from a yaml file, /etc/puppet/hiera.yaml. Create the file and add the following as a minimal configuration:
    ---
    :hierarchy:
     - common
    :backends:
     - yaml
    :yaml:
     :datadir: '/etc/puppet/hieradata'
    
  2. Create the common.yaml file referenced in the hierarchy:
    root@puppet:/etc/puppet# mkdir hieradata
    root@puppet:/etc/puppet# vim hieradata/common.yaml
    ---
    message: 'Default Message'
    
  3. Edit the site.pp file and add a notify resource based on the Hiera value:
    node default {
     $message = hiera('message','unknown')
     notify {"Message is $message":}
    }
    
  4. Apply the manifest to a test node:
    t@ckbk:~$ sudo puppet agent -t
    Info: Retrieving pluginfacts
    Info: Retrieving plugin
    ...
    Info: Caching catalog for cookbook-test
    Info: Applying configuration version '1410504848'
    Notice: Message is Default Message
    Notice: /Stage[main]/Main/Node[default]/Notify[Message is Default Message]/message: defined 'message' as 'Message is Default Message'
    Notice: Finished catalog run in 0.06 seconds
    

How it works...

Hiera uses a hierarchy to search through a set of yaml files to find the appropriate values. We defined this hierarchy in hiera.yaml with the single entry for common.yaml. We used the hiera function in site.pp to lookup the value for message and store that value in the variable $message. The values used for the definition of the hierarchy can be any facter facts defined about the system. A common hierarchy is shown as:

:hierarchy:
  - hosts/%{hostname}
  - os/%{operatingsystem}
  - network/%{network_eth0}
  - common

There's more...

Hiera can be used for automatic parameter lookup with parameterized classes. For example, if you have a class named cookbook::example with a parameter named publisher, you can include the following in a Hiera yaml file to automatically set this parameter:

cookbook::example::publisher: 'PacktPub'

Another often used fact is environment you may reference the environment of the client node using %{environment} as shown in the following hierarchy:

:hierarchy:
hosts/%{hostname}
os/%{operatingsystem}
environment/%{environment}
common
Tip

A good rule of thumb is to limit the hierarchy to 8 levels or less. Keep in mind that each time a parameter is searched with Hiera, all the levels are searched until a match is found.

The default Hiera function returns the first match to the search key, you can also use hiera_array and hiera_hash to search and return all values stored in Hiera.

Hiera can also be searched from the command line as shown in the following command line (note that currently the command line Hiera utility uses /etc/hiera.yaml as its configuration file whereas the Puppet master uses /etc/puppet/hiera.yaml):

root@puppet:/etc/puppet# rm /etc/hiera.yaml 
root@puppet:/etc/puppet# ln -s /etc/puppet/hiera.yaml /etc/
root@puppet:/etc/puppet# hiera message
Default Message

Note

For more information, consult the Puppet labs website at https://docs.puppetlabs.com/hiera/1/.

主站蜘蛛池模板: 永寿县| 平和县| 巴彦淖尔市| 盱眙县| 克什克腾旗| 南汇区| 曲沃县| 陇川县| 岚皋县| 章丘市| 丰顺县| 通道| 大新县| 宁远县| 广州市| 潍坊市| 满洲里市| 繁昌县| 晋中市| 惠来县| 姚安县| 大同市| 云南省| 仲巴县| 清徐县| 大荔县| 资源县| 夹江县| 樟树市| 大厂| 大冶市| 进贤县| 商都县| 麦盖提县| 西林县| 阿拉善左旗| 泗洪县| 都昌县| 和平区| 十堰市| 柯坪县|