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

  • DevOps:Puppet,Docker,and Kubernetes
  • Thomas Uphill John Arundel Neependra Khare Hideto Saito Hui Chuan Chloe Lee Ke Jou Carol Hsu
  • 358字
  • 2021-07-09 18:32:57

Using EPP templates

EPP templates are a new feature in Puppet 3.5 and newer versions. EPP templates use a syntax similar to ERB templates but are not compiled through Ruby. Two new functions are defined to call EPP templates, epp, and inline_epp. These functions are the EPP equivalents of the ERB functions template and inline_template, respectively. The main difference with EPP templates is that variables are referenced using the Puppet notation, $variable instead of @variable.

How to do it...

  1. Create an EPP template in ~/puppet/epp-test.epp with the following content:
    This is <%= $message %>.
  2. Create an epp.pp manifest, which uses the epp and inline_epp functions:
    $message = "the message"
    file {'/tmp/epp-test':
      content => epp('/home/thomas/puppet/epp-test.epp')
    }
    notify {inline_epp('Also prints <%= $message %>'):}
  3. Apply the manifest making sure to use the future parser (the future parser is required for the epp and inline_epp functions to be defined):
    t@mylaptop ~/puppet $ puppet apply epp.pp --parser=future
    Notice: Compiled catalog for mylaptop in environment production in 1.03 seconds
    Notice: /Stage[main]/Main/File[/tmp/epp-test]/ensure: defined content as '{md5}999ccc2507d79d50fae0775d69b63b8c'
    Notice: Also prints the message
    
  4. Verify that the template worked as intended:
    t@mylaptop ~/puppet $ cat /tmp/epp-test 
    This is the message.
    

How it works...

Using the future parser, the epp and inline_epp functions are defined. The main difference between EPP templates and ERB templates is that variables are referenced in the same way they are within Puppet manifests.

There's more...

Both epp and inline_epp allow for variables to be overridden within the function call. A second parameter to the function call can be used to specify values for variables used within the scope of the function call. For example, we can override the value of $message with the following code:

file {'/tmp/epp-test':
  content => epp('/home/tuphill/puppet/epp-test.epp',
    { 'message' => "override $message"} )
}
notify {inline_epp('Also prints <%= $message %>',
  { 'message' => "inline override $message"}):}

Now when we run Puppet and verify the output we see that the value of $message has been overridden:

t@mylaptop ~/puppet $ puppet apply epp.pp --parser=future
Notice: Compiled catalog for mylaptop.pan.costco.com in environment production in 0.85 seconds
Notice: Also prints inline override the message
Notice: Finished catalog run in 0.05 seconds
t@mylaptop ~/puppet $ cat /tmp/epp-test 
This is override the message.
主站蜘蛛池模板: 南江县| 温泉县| 赞皇县| 水富县| 玛沁县| 达州市| 颍上县| 晋州市| 那曲县| 宾阳县| 长丰县| 吴桥县| 仁化县| 武邑县| 仁化县| 营口市| 丰都县| 文成县| 遵义市| 湖口县| 和田县| 什邡市| 商南县| 广德县| 白水县| 阜城县| 西盟| 东光县| 文水县| 辽中县| 贺州市| 永福县| 昭通市| 仁布县| 上蔡县| 东兴市| 青海省| 尼木县| 德昌县| 田东县| 安阳县|