当我将更新推送到Chef服务器时,我的更新首先被推送到我们的开发节点,然后最终将它们合并到生产节点。
我有两个设置为数据库URL的属性,但是,根据我是在开发节点还是在生产节点,这些属性的值是不同的。
如何设置我的属性,以便根据其所处的环境来设置属性的值?
这就是我所拥有的:
default['test_cookbook']['Development']['URL]='jdbc:mysql://exampleDB1.com'
default['test_cookbook']['Production']['URL]='jdbc:mysql://exampleDB2.com'
理想情况下,我希望使用一些厨师资源(例如“ node.chef_environment”)来检查环境,并根据环境决定要使用的URL。
答案 0 :(得分:0)
理想情况下,Chef属性不应以使用它们的环境命名。最好使用以下与环境无关的属性:
default['test_cookbook']['jdbc_url']
TL; DR
您可以通过属性文件实现所需的功能。这个想法是基于node.chef_environment
设置属性。
示例test_cookbook/attributes/default.rb
:
default['test_cookbook']['jdbc_url'] = case node.chef_environment
when 'development'
'jdbc:mysql://exampleDB1.com'
when 'production'
'jdbc:mysql://exampleDB2.com'
end
但是...
可以通过使用knife environment edit
编辑环境或通过编辑该环境的.json
文件来在每个Chef Environment中设置此变量的值。
应该首选此方法,因为可以在每个环境中更改JDBC URL,而无需修改食谱。 菜谱修改类似于代码更改-涉及分支,版本修改以及相关的README.md
和CHANGELOG.md
更新,代码审查和合并。然后,在部署期间,这将逐步下降为使用“新”食谱版本。
所有这些都可以更改值!不推荐。
示例development
厨师环境:
"default_attributes": {
"test_cookbook": {
"jdbc_url": "jdbc:mysql://exampleDB1.com"
}
},
示例production
厨师环境:
"default_attributes": {
"test_cookbook": {
"jdbc_url": "jdbc:mysql://exampleDB2.com"
}
},
答案 1 :(得分:0)
您可以在属性文件中执行此操作:
default['test_cookbook']['Development']['URL']='jdbc:mysql://exampleDB1.com'
default['test_cookbook']['Production']['URL']='jdbc:mysql://exampleDB2.com'
然后在食谱顶部,将食谱的节点属性放入ruby变量中,并使用该变量:
attributes = node['test_cookbook'][node.chef_environment]
remote_file attributes['download_location'] do
source attributes['URL']
end
template attributes['config_file'] do
source "myapp.conf.erb"
variables({ attributes: attributes })
end
还有一种通过“提升”通过PolicyFiles实现此目的的好方法,其中policy_group
代替chef_environment
,这是往前推荐的方法:
https://docs.chef.io/release_notes_client/#policyfile-hoisting