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

Passing parameters from Hiera

Like the parameter defaults we introduced in the previous chapter, Hiera may be used to provide default values to classes. This feature requires Puppet Version 3 and higher.

Getting ready

Install and configure hiera as we did in Chapter 2, Puppet Infrastructure. Create a global or common yaml file; this will serve as the default for all values.

How to do it...

  1. Create a class with parameters and no default values:
    t@mylaptop ~/puppet $ mkdir -p modules/mysql/manifests t@mylaptop ~/puppet $ vim modules/mysql/manifests/init.pp
    class mysql ( $port, $socket, $package ) {
     notify {"Port: $port Socket: $socket Package: $package": }
    }
    
  2. Update your common .yaml file in Hiera with the default values for the mysql class:
    ---
    mysql::port: 3306
    mysql::package: 'mysql-server'
    mysql::socket: '/var/lib/mysql/mysql.sock'
    

    Apply the class to a node, you can add the mysql class to your default node for now.

    node default {
     class {'mysql': }
    }
    
  3. Run puppet agent and verify the output:
    [root@hiera-test ~]# puppet agent -t
    Info: Caching catalog for hiera-test.example.com
    Info: Applying configuration version '1411182251'
    Notice: Port: 3306 Socket: /var/lib/mysql/mysql.sock Package: mysql-server
    Notice: /Stage[main]/Mysql/Notify[Port: 3306 Socket: /var/lib/mysql/mysql.sock Package: mysql-server]/message: defined 'message' as 'Port: 3306 Socket: /var/lib/mysql/mysql.sock Package: mysql-server'
    Notice: Finished catalog run in 1.75 seconds
    

How it works...

When we instantiate the mysql class in our manifest, we provided no values for any of the attributes. Puppet knows to look for a value in Hiera that matches class_name::parameter_name: or ::class_name::parameter_name:.

When Puppet finds a value, it uses it as the parameter for the class. If Puppet fails to find a value in Hiera and no default is defined, a catalog failure will result in the following command line:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Must pass package to Class[Mysql] at /etc/puppet/environments/production/manifests/site.pp:6 on node hiera-test.example.com

This error indicates that Puppet would like a value for the parameter package.

There's more...

You can define a Hiera hierarchy and supply different values for parameters based on facts. You could, for instance, have %{::osfamily} in your hierarchy and have different yaml files based on the osfamily parameter (RedHat, Suse, and Debian).

主站蜘蛛池模板: 宁南县| 资中县| 公安县| 含山县| 潞城市| 新巴尔虎右旗| 新绛县| 卢湾区| 仁化县| 同仁县| 西乌| 德令哈市| 辽阳县| 南召县| 电白县| 炎陵县| 雅安市| 泰来县| 石阡县| 油尖旺区| 衡东县| 秦皇岛市| 荔波县| 永泰县| 肃北| 荆门市| 图片| 武陟县| 崇左市| 镇雄县| 临夏市| 明溪县| 开原市| 昌平区| 嘉荫县| 隆安县| 寿光市| 正定县| 金塔县| 子长县| 泸溪县|