learning puppet 4--template and function

Templates

Templates are documents that contain a mixture of static and dynamic content. By using a small amount of conditional logic and variable interpolation, they let you maintain one source document that can be rendered into any number of final documents.

Variables in Templates

Templates are powerful because they have access to all of the Puppet variables that are present when the template is rendered.

  • Facts, global variables, and local variables from the current scope are available to a template as Ruby instance variables — instead of Puppet's $ prefix, they have an @ prefix. (e.g. @fqdn, @memoryfree, @operatingsystem, etc.)
  • Variables from other scopes can be accessed with the scope.lookupvar method, which takes a long variable name without the $ prefix. (For example, scope.lookupvar('apache::user').)

The ERB Templating Language

Puppet doesn't have its own templating language; instead, it uses ERB, a common Ruby-based template language. (The Rails framework uses ERB, as do several other projects.)

Functions 

We've seen several functions already, including include, template, fail, and str2bool, so this is as good a time as any to explain what they are.

Puppet has two kinds of functions:

  • Functions that return a value
  • Functions that do something else, without returning a value

The template and str2bool functions both return values; you can use them anywhere that requires a value, as long as the return value is the right kind. The include and fail functions do something else, without returning a value — declare a class, and stop catalog compilation, respectively.

All functions are run during catalog compilation. This means they run on the puppet master, and don't have access to any files or settings on the agent node.

Functions can take any number of arguments, which are separated by commas and can be surrounded by optional parentheses:

function(argument, argument, argument)

Functions are plugins, so many custom plugins are available in modules.

Complete documentation about functions are available at the functions page of the Puppet reference manual and the list of built-in functions.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值