Chef 食谱中 nexus_api 的默认属性覆盖无法更新值

default attributes override for nexus_api in chef cookbook fail to update values

我正在为 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 的 属性 值在食谱 运行 期间无法生效,正如我在日志中看到的那样

==> 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 chef 提供这本食谱,我的 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

here's the source 用于我正在构建包装器的食谱

您提到您正在覆盖属性,但您的代码表明您正在将这些属性设置为 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 的引用:

An override attribute is automatically reset at the start of every chef-client run and has a higher attribute precedence than default, force_default, and normal attributes. An override attribute is most often specified in a recipe, but can be specified in an attribute file, for a role, and/or for an environment. A cookbook should be authored so that it uses override attributes only when required.

如果您只是将这些设置为 default 在包装器说明书的 attributes/default.rb 文件中,那么源说明书和包装器都试图在同一级别设置相同的属性。这可能会导致意外行为或根本不起作用。