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

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
主站蜘蛛池模板: 襄樊市| 商都县| 河南省| 大化| 中卫市| 禄丰县| 岳阳市| 涿鹿县| 大竹县| 南丰县| 进贤县| 水富县| 沾化县| 大城县| 江油市| 阿拉善盟| 海安县| 邵阳县| 泽库县| 阳山县| 辽宁省| 防城港市| 武清区| 增城市| 娄烦县| 年辖:市辖区| 巴林右旗| 荥阳市| 凯里市| 吉木萨尔县| 郧西县| 遂溪县| 仁化县| 竹溪县| 开平市| 英山县| 余庆县| 黎城县| 山东| 漠河县| 彰武县|