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

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

Importing dynamic information

Even though some system administrators like to wall themselves off from the rest of the office using piles of old printers, we all need to exchange information with other departments from time to time. For example, you may want to insert data into your Puppet manifests that is derived from some outside source. The generate function is ideal for this. Functions are executed on the machine compiling the catalog (the master for centralized deployments); an example like that shown here will only work in a masterless configuration.

Getting ready

Follow these steps to prepare to run the example:

  1. Create the script /usr/local/bin/message.rb with the following contents:
    #!/usr/bin/env ruby
    puts "This runs on the master if you are centralized"
    
  2. Make the script executable:
    $ sudo chmod a+x /usr/local/bin/message.rb
    

How to do it…

This example calls the external script we created previously and gets its output:

  1. Create a message.pp manifest containing the following:
    $message = generate('/usr/local/bin/message.rb')
    notify { $message: }
    
  2. Run Puppet:
    $ puppet apply message.pp 
    ...
    Notice: /Stage[main]/Main/Notify[This runs on the master if you are centralized
    ]/message: defined 'message' as 'This runs on the master if you are centralized
    

How it works…

The generate function runs the specified script or program and returns the result, in this case, a cheerful message from Ruby.

This isn't terribly useful as it stands but you get the idea. Anything a script can do, print, fetch, or calculate, for example, the results of a database query, can be brought into your manifest using generate. You can also, of course, run standard UNIX utilities such as cat and grep.

There's more…

If you need to pass arguments to the executable called by generate, add them as extra arguments to the function call:

$message = generate('/bin/cat', '/etc/motd')

Puppet will try to protect you from malicious shell calls by restricting the characters you can use in a call to generate, so shell pipes and redirection aren't allowed, for example. The simplest and safest thing to do is to put all your logic into a script and then call that script.

See also

  • The Creating custom facts recipe in Chapter 9, External Tools and the Puppet Ecosystem
  • The Configuring Hiera recipe in Chapter 2, Puppet Infrastructure
主站蜘蛛池模板: 郑州市| 苍南县| 凤凰县| 瓮安县| 会理县| 美姑县| 宜君县| 苍山县| 微山县| 平谷区| 博客| 右玉县| 临武县| 长沙市| 黄石市| 宣恩县| 嘉黎县| 石屏县| 元谋县| 永顺县| 枣阳市| 旌德县| 中西区| 神池县| 玛多县| 海丰县| 洞口县| 普陀区| 南华县| 黄浦区| 抚松县| 中卫市| 溆浦县| 伊宁市| 鄱阳县| 新源县| 桐乡市| 辰溪县| 柯坪县| 祁门县| 西充县|