- Mastering Puppet 5
- Ryan Russell Yates Jason Southgate
- 227字
- 2021-07-16 17:46:14
Custom functions
This is where custom facts allow us to run arbitrary code on the client side. Custom functions are a server-side technology that assist you in the compilation of a catalog. Functions are executed on the Puppet server. Puppet already includes several functions that are built-in, and additional ones are contained in Puppet Forge modules, particularly the stdlib module (see https://forge.puppet.com/puppetlabs/stdlib).
There are, in fact, three possible ways to create custom functions, although you are unlikely to use the first two, so I will just leave you with some links to the Puppet documentation for those options:
- You could write the function in Puppet DSL (see https://puppet.com/docs/puppet/5.3/lang_write_functions_in_puppet.html), although you'll be unable to take advantage of the more powerful Ruby API.
- You could write the function in the legacy Ruby functions API (see https://puppet.com/docs/puppet/5.3/functions_legacy.html), although this is to be avoided unless you must specifically support Puppet 3.
- You could write the function in the modern Ruby function API. This is what we'll concentrate on for the remainder of this section.
The best way to create and distribute a new custom function is to place it in a module, in the puppet/functions/<modulename> subdirectory of the lib directory, and it will then be distributed via pluginsync, as shown in the following code:
#<modulepath>/lib/puppet/functions/mymodule/myfunction.rb
Puppet::Functions.create_function(:'mymodule::myfunction') do
dispatch :up do
param 'String', :a_string
end
def up(a_string)
a_string.upcase
end
end
- 電子產(chǎn)品印制電路板制作技能演練
- 西門子SINAMICS G120/S120變頻器技術(shù)與應(yīng)用
- EGPRS網(wǎng)絡(luò)演進
- 新手學(xué)修手機
- 信息論基礎(chǔ)(第2版)
- 面向后續(xù)演進的5G無線增強技術(shù)
- 快修巧修新型手機(修訂版)
- 移動互聯(lián)網(wǎng):原理、技術(shù)與應(yīng)用
- 通信電子線路
- 5G移動通信:無線網(wǎng)絡(luò)優(yōu)化技術(shù)與實踐
- 5G來了:5G如何改變生活、社會和產(chǎn)業(yè)
- 信息論與編碼原理
- 碳化硅功率器件:特性、測試和應(yīng)用技術(shù)
- 愛上單片機(第3版)
- 通信專業(yè)綜合能力與實務(wù):交換技術(shù)