我正在为nexus3编写一个包装器食谱,其中我在我的食谱的attributes/default.rb
文件中覆盖默认属性
# Nexus Options
node.default['nexus3']['properties_variables'] = { port: '8383', host: '0.0.0.0', args: '${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml', context_path: '/nexus/' }
node.default['nexus3']['api']['host'] = 'http://localhost:8383'
node.default['nexus3']['api']['username'] = 'admin'
node.default['nexus3']['api']['password'] = 'Ch5f@A4min'
虽然Chef确实使用覆盖属性安装了nexus3,但是nexus3_api的属性值在cookbook运行期间无法生效,正如我在日志中看到的那样
==> provisioner: * execute[wait for http://localhost:8081/service/siesta/rest/v1/script to respond] action run
==> provisioner: [2018-06-11T05:58:17+00:00] INFO: Processing execute[wait for http://localhost:8081/service/siesta/rest/v1/script to respond] action run (/opt/chef/embedded/lib/ruby/gems/2.5.0/gems/chef-14.2.0/lib/chef/resource.rb line 1285)
==> provisioner: [2018-06-11T05:58:17+00:00] INFO: Processing execute[wait for http://localhost:8081/service/siesta/rest/v1/script to respond] action run (/opt/chef/embedded/lib/ruby/gems/2.5.0/gems/chef-14.2.0/lib/chef/resource.rb line 1285)
我正在通过流浪汉厨师提供这本食谱,我的Vagrant文件如下
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
config.vm.define "provisioner" do |provisioner|
provisioner.vm.box = "ubuntu/xenial64"
provisioner.vm.box_version = "20180509.0.0"
provisioner.vm.box_check_update = false
provisioner.omnibus.chef_version = :latest
provisioner.vm.network "forwarded_port", guest: 8080, host: 8282
provisioner.vm.network "forwarded_port", guest: 8383, host: 8383
provisioner.vm.provider :virtualbox do |vbox|
vbox.name = "pipeline-jumpstart-chef"
vbox.memory = 2048
vbox.cpus = 2
end
provisioner.vm.provision "chef_solo" do |chef|
chef.node_name = "chef-provisioned"
chef.cookbooks_path = "../../cookbooks"
chef.verbose_logging = true
chef.add_recipe "pipeline-jumpstart-chef"
end
end
end
对于我正在构建包装器的食谱
答案 0 :(得分:1)
您提到您正在覆盖属性,但您的代码表明您正在将这些属性设置为default
级别。您应该查看Chef中的Attribute Precedence以了解default
到底意味着什么。此外,在属性文件中,您不需要使用node
作为前缀,只需使用default
::
default['nexus3']['properties_variables'] = { port: '8383', host: '0.0.0.0', args: '${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml', context_path: '/nexus/' }
default['nexus3']['api']['host'] = 'http://localhost:8383'
default['nexus3']['api']['username'] = 'admin'
default['nexus3']['api']['password'] = 'Ch5f@A4min'
node.default
语法用于内联,在配方内设置属性。如果查看优先级图表,您会注意到内联和默认属性高一级。
如果您想使用override
,可以为每个属性执行此操作:
override['nexus3']['properties_variables'] = { port: '8383', host: '0.0.0.0', args: '${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml', context_path: '/nexus/' }
override['nexus3']['api']['host'] = 'http://localhost:8383'
override['nexus3']['api']['username'] = 'admin'
override['nexus3']['api']['password'] = 'Ch5f@A4min'
但是,除非绝对有必要在包装器手册中设置这些属性,否则最好将其设置为具有更高优先级的default
属性,例如角色。请参阅以下same document' Attribute Types section有关覆盖属性的引用:
覆盖属性会在每个属性的开头自动重置 chef-client运行并且具有比默认值更高的属性优先级, force_default和普通属性。覆盖属性最多 通常在配方中指定,但可以在属性中指定 文件,角色和/或环境。 食谱应该是 创作,以便仅在需要时使用覆盖属性。
如果您只是在包装菜谱的default
文件中将它们设置为attributes/default.rb
,则源菜谱和包装器都会尝试在同一级别设置相同的属性。这可能会导致意外行为或根本不起作用。