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

Using multiple file sources

A neat feature of Puppet's file resource is that you can specify multiple values for the source parameter. Puppet will search them in order. If the first source isn't found, it moves on to the next, and so on. You can use this to specify a default substitute if the particular file isn't present, or even a series of increasingly generic substitutes.

How to do it...

This example demonstrates using multiple file sources:

  1. Create a new greeting module as follows:
    class greeting {
      file { '/tmp/greeting':
        source => [ 'puppet:///modules/greeting/hello.txt',
                    'puppet:///modules/greeting/universal.txt'],
      }
    }
  2. Create the file modules/greeting/files/hello.txt with the following contents:
    Hello, world.
  3. Create the file modules/greeting/files/universal.txt with the following contents:
    Bah-weep-Graaaaagnah wheep ni ni bong
  4. Add the class to a node:
    node cookbook {
      class {'greeting': }
    }
  5. Run Puppet:
    [root@cookbook ~]# puppet agent -t
    Info: Caching catalog for cookbook.example.com
    Info: Applying configuration version '1413784347'
    Notice: /Stage[main]/Greeting/File[/tmp/greeting]/ensure: defined content as '{md5}54098b367d2e87b078671fad4afb9dbb'
    Notice: Finished catalog run in 0.43 seconds
    
  6. Check the contents of the /tmp/greeting file:
    [root@cookbook ~]# cat /tmp/greeting 
    Hello, world.
    
  7. Now remove the hello.txt file from your Puppet repository and rerun the agent:
    [root@cookbook ~]# puppet agent -t
    Info: Caching catalog for cookbook.example.com
    Info: Applying configuration version '1413784939'
    Notice: /Stage[main]/Greeting/File[/tmp/greeting]/content: 
    --- /tmp/greeting 2014-10-20 01:52:28.117999991 -0400
    +++ /tmp/puppet-file20141020-4960-1o9g344-0 2014-10-20 02:02:20.695999979 -0400
    @@ -1 +1 @@
    -Hello, world.
    +Bah-weep-Graaaaagnah wheep ni ni bong
    
    Info: Computing checksum on file /tmp/greeting
    Info: /Stage[main]/Greeting/File[/tmp/greeting]: Filebucketed /tmp/greeting to puppet with sum 54098b367d2e87b078671fad4afb9dbb
    Notice: /Stage[main]/Greeting/File[/tmp/greeting]/content: content changed '{md5}54098b367d2e87b078671fad4afb9dbb' to '{md5}933c7f04d501b45456e830de299b5521'
    Notice: Finished catalog run in 0.77 seconds
    

How it works...

On the first Puppet run, puppet searches for the available file sources in the order given:

source => [
  'puppet:///modules/greeting/hello.txt',
  'puppet:///modules/greeting/universal.txt'
],

The file hello.txt is first in the list, and is present, so Puppet uses that as the source for /tmp/greeting:

Hello, world.

On the second Puppet run, hello.txt is missing, so Puppet goes on to look for the next file, universal.txt. This is present, so it becomes the source for /tmp/greeting:

Bah-weep-Graaaaagnah wheep ni ni bong

There's more...

You can use this trick anywhere you have a file resource. A common example is a service that is deployed on all nodes, such as rsyslog. The rsyslog configuration is the same on every host except for the rsyslog server. Create an rsyslog class with a file resource for the rsyslog configuration file:

class rsyslog {
  file { '/etc/rsyslog.conf':
    source => [
      "puppet:///modules/rsyslog/rsyslog.conf.${::hostname}",
      'puppet:///modules/rsyslog/rsyslog.conf' ],
  }

Then, you put the default configuration in rsyslog.conf. For your rsyslog server, logger, create an rsyslog.conf.logger file. On the machine logger, rsyslog.conf.logger will be used before rsyslog.conf because it is listed first in the array of sources.

See also

  • The Passing parameters to classes recipe in Chapter 3, Writing Better Manifests
主站蜘蛛池模板: 普宁市| 沽源县| 柳江县| 黑河市| 抚远县| 崇文区| 嵊泗县| 湘潭县| 平原县| 桓台县| 利津县| 新津县| 铜陵市| 基隆市| 宁安市| 安康市| 绵阳市| 门头沟区| 军事| 保亭| 石柱| 汨罗市| 惠安县| 崇左市| 云南省| 寻甸| 墨脱县| 昆山市| 临沭县| 北安市| 石首市| 洞口县| 宁化县| 聂拉木县| 勐海县| 西乌珠穆沁旗| 越西县| 六安市| 上栗县| 常熟市| 泸水县|