【问题标题】:default attributes override for nexus_api in chef cookbook fail to update values厨师食谱中nexus_api的默认属性覆盖无法更新值
【发布时间】:2018-06-11 21:31:34
【问题描述】:

我正在为 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 provision 运行这本食谱,我的 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 用于我正在构建包装器的食谱

【问题讨论】:

    标签: vagrant chef-infra chef-solo cookbook nexus3


    【解决方案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 和普通属性。覆盖属性是最 通常在配方中指定,但可以在属性中指定 文件、角色和/或环境。 食谱应该是 创作使其仅在需要时才使用覆盖属性

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

    【讨论】:

    • 好的,所以当我们创建某种包装食谱时,可以扩展从厨师超市下载的食谱;我们应该将我们想要的默认值放在我们的包装器中的食谱或包装器的attributes/default.rb 中吗?食谱?
    • 如果属性必须是包装说明书的一部分,您应该在其属性文件中将它们设置为覆盖。如果您不想覆盖它们,可以在roleenvironment 的属性中将它们设置为default。这真的取决于你的喜好。编辑:我也澄清了我的答案。
    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2023-03-22
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多