如何根据Chef中的环境设置属性的值? [英] How can I set the value of an attribute based on the environment I am in in Chef?
问题描述
当我将更新推送到Chef服务器时,我的更新首先被推送到我们的开发节点,然后最终将它们合并到生产节点.
When I push my updates to the chef server, my updates first get pushed to our development node and then I eventually merge them to the production node.
我有两个设置为数据库URL的属性,但是,根据我是在开发节点上还是在生产节点上,这些属性的值是不同的.
I have two attributes that set to a URL for a database, however, the value of the attributes are different based on whether I am on the development node or the production node.
如何设置我的属性,以便根据其所处的环境来设置属性的值?
How can set my attributes so it sets the value of the attribute based on what environment it is in?
这就是我所拥有的:
default['test_cookbook']['Development']['URL]='jdbc:mysql://exampleDB1.com'
default['test_cookbook']['Production']['URL]='jdbc:mysql://exampleDB2.com'
理想情况下是生产",而不是生产".和开发方面,我需要一些厨师资源,例如"node.chef_environment"它将检查环境并根据环境决定要使用的URL.
Ideally instead of "Production" and Development, I would want some chef resource like "node.chef_environment" that will check the environment and decide what URL to use based on the environment.
推荐答案
您可以在属性文件中执行此操作:
You can do this in your attributes file:
default['test_cookbook']['Development']['URL']='jdbc:mysql://exampleDB1.com'
default['test_cookbook']['Production']['URL']='jdbc:mysql://exampleDB2.com'
然后在食谱顶部,将食谱的节点属性放入ruby变量中,并使用该变量:
Then at the top of your recipe pull the node attributes for the cookbook into a ruby variable and use that:
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
,这是今后的推荐方法:
There's also an elegant way to do this via PolicyFiles using 'hoisting' where policy_group
replaces chef_environment
and is the recommended way going forwards:
https://docs.chef.io/release_notes_client/#policyfile-hoisting
这篇关于如何根据Chef中的环境设置属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!