Puppet - 环境配置

在 Puppet 中,所有环境都有 environment.conf 文件。每当主服务器为分配给该特定环境的任何节点或所有节点提供服务时,此文件可以覆盖多个默认设置。

位置

在 Puppet 中,对于所有已定义的环境,environment.conf 文件位于其主环境的顶层,紧挨着清单和模块目录。举个例子,如果您的环境位于默认目录(Vipin/testing/environment),那么测试环境的配置文件位于Vipin/testing/environments/test/environment.conf

示例

# /etc/testingdir/code/environments/test/environment.conf
# Puppet Enterprise 需要 $basemodulepath;请参阅下面 modulepath 下的注释"。
modulepath = site:dist:modules:$basemodulepath
# 使用我们的自定义脚本获取代码当前状态的 git 提交:
config_version = get_environment_commit.sh

格式

Puppet 中的所有配置文件都以相同的方式使用相同的 INI 格式。environment.conf 文件遵循与其他文件(如 puppet.conf 文件)相同的 INI 格式。environment.conf 和 puppet.conf 之间的唯一区别是 environment.conf 文件不能包含 [main] 部分。environment.conf 文件中的所有设置都必须位于任何配置部分之外。

值中的相对路径

大多数允许的设置都接受文件路径或路径列表作为值。如果任何路径是相关路径,则它们以不带前导斜杠或驱动器号开头 - 它们将大部分相对于该环境的主目录进行解析。

值中的插值

Environment.conf 设置文件能够使用其他设置的值作为变量。有多个有用的变量可以插入到 environment.conf 文件中。以下是一些重要变量的列表 −

  • $basemodulepath − 用于在模块路径设置中包含目录。Puppet 企业用户通常应包含此 modulepath 值,因为 Puppet 引擎在 basemodulepath 中使用模块。

  • $environment − 可用作 config_version 脚本的命令行参数。您只能在 config_version 设置中插入此变量。

  • $codedir −对于定位文件很有用。

允许的设置

默认情况下,Puppet environment.conf 文件仅允许覆盖配置中列出的四个设置。

  • Modulepath
  • Manifest
  • Config_version
  • Environment_timeout

Modulepath

这是 environment.conf 文件中的关键设置之一。默认情况下,Puppet 会加载 modulepath 中定义的所有目录。这是 Puppet 加载其模块的路径位置。需要明确设置。如果未设置上述设置,则 Puppet 中任何环境的默认模块路径将为 −

<MODULES DIRECTORY FROM ENVIRONMENT>:$basemodulepath

Manifest

这用于定义主清单文件,Puppet Master 将在启动时使用该文件并从定义的清单中编译目录,该清单将用于配置环境。在这里,我们可以定义单个文件、文件列表,甚至由多个清单文件组成的目录,这些清单文件需要按照定义的字母顺序进行评估和编译。

需要在 environment.conf 文件中明确定义此设置。如果没有,则 Puppet 将使用环境默认清单目录作为其主清单。

Config_version

Config_version 可以定义为用于识别目录和事件的明确版本。当 Puppet 默认编译任何清单文件时,它会将配置版本添加到生成的目录以及 Puppet 主机在 Puppet 节点上应用任何定义的目录时生成的报告中。Puppet 运行脚本来执行上述所有步骤,并将所有生成的输出用作 Config_version。

Environment Timeout

它用于获取有关 Puppet 应使用多长时间加载给定环境的数据的详细信息。如果该值在 puppet.conf 文件中定义,则这些值将覆盖默认超时值。

示例 environment.conf 文件

[master] 
   manifest =  $confdir/environments/$environment/manifests/site.pp 
   modulepath =  $confdir/environments/$environment/modules

在上面的代码中,$confdir 是环境配置文件所在目录的路径。$environment 是正在进行配置的环境的名称。

生产就绪环境配置文件

# The environment configuration file  
# The main manifest directory or file where Puppet starts to evaluate code  
# This is the default value. Works with just a site.pp file or any other  
manifest = manifests/  
# The directories added to the module path, looked in first match first used order:  
# modules - Directory for external modules, populated by r10k based on Puppetfile  
# $basemodulepath - As from: puppet config print basemodulepath  
modulepath = site:modules:$basemodulepath  
# Set the cache timeout for this environment.  
# This overrides what is set directly in puppet.conf for the whole Puppet server  
# environment_timeout = unlimited  
# With caching you need to flush the cache whenever new Puppet code is deployed  
# This can also be done manually running: bin/puppet_flush_environment_cache.sh  
# To disable catalog caching:  
environment_timeout = 0  
# Here we pass to one in the control repo the Puppet environment (and git branch)  
# to get title and essential info of the last git commit
config_version = 'bin/config_script.sh $environment'