Puppet:根据主机名包含其他文件

Puppet:根据主机名包含其他文件

我正在尝试在我的 Puppet 清单中实现类似这样的功能:

    if $hostname == 'host1' || $hostname == 'host2' {
        # Modified config.txt for portrait mode. 
        file { '/boot/config.txt' :
            ensure      => present,
            mode        => '0755',
            source      => 'puppet://puppet/files/boot/config.txt.portrait',
        }
    } else {
        # Normal config.txt for landscape mode.
        file { '/boot/config.txt' :
            ensure      => present,
            mode        => '0755',
            source      => 'puppet://puppet/files/boot/config.txt.landscape',
        }
    }

但是,这失败了。根据主机名包含不同文件的最佳方法是什么?

答案1

另一种语法是在源参数中使用选择器。

file { '/boot/config.txt' :
    ensure      => present,
    mode        => '0755',
    source      => $::hostname?{
                      'host1' => 'puppet://puppet/files/boot/config.txt.portrait',
                      'host2' => 'puppet://puppet/files/boot/config.txt.portrait',
                      default => 'puppet://puppet/files/boot/config.txt.landscape',
                    },
}

答案2

根据 devicenull 所说的内容,您可以使用选择性文件源进一步缩短它:

# Normal config.txt for landscape mode.
file { '/boot/config.txt' :
    ensure      => present,
    mode        => '0755',
    source      => [ 
         "puppet://puppet/files/boot/config.txt.$hostname", # down the individual hostname if required
         "puppet://puppet/files/boot/config.txt.$layouttype", # a layout dimension fact (portrait/landscape)
         "puppet://puppet/files/boot/config.txt" # default
    ],
}

我的清单中有很多类似的例子,其中主机类型需要相对静态的文件(通常不会深入到主机名级别),但肯定需要 $domain 和自定义事实 $site_location(外部、数据中心、办公室等)被广泛使用。

答案3

对于你问的问题我会用案例陈述

    case $hostname {
      'host1', 'host2': { # Modified config.txt for portrait mode. 
        file { '/boot/config.txt' :
            ensure      => present,
            mode        => '0755',
            source      => 'puppet://puppet/files/boot/config.txt.portrait',
        }  
      } 
      default:            { 
        file { '/boot/config.txt' :
            ensure      => present,
            mode        => '0755',
            source      => 'puppet://puppet/files/boot/config.txt.landscape',
        }
      } # apply the generic class
    }

答案4

我必须使用“或”而不是||:

    if $hostname == 'host1' or $hostname == 'host2' {

相关内容