Search code examples
vagrantchef-infrachef-solocookbooknexus3

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


I'm writing a wrapper cookbook for nexus3 wherein I override the default attributes like so in the attributes/default.rb file of my cookbook

# 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'

While Chef does install nexus3 with the override properties, property values for the nexus3_api fail to take effect during cookbook run, as I see in the logs

==> 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)

I'm running this cookbook through vagrant chef provision and my Vagrant file is as follows

# -*- 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 for cookbook on which I'm building wrapper


Solution

  • You mention your are overriding the attributes but your code indicates you are setting those attributes to the default level. You should review the Attribute Precedence in Chef to understand what default means exactly. In addition, inside the attributes file you don't need to prefix with node just use 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'
    

    The node.default syntax is used inline, inside a recipe to set attributes. If you review the precedence chart you'll notice inline and default attributes are one level higher.

    If you want to use override you can do this for each attribute:

    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'
    

    However, unless it's absolutely necessary to set these attributes in the wrapper cookbook you are likely better off setting this as a default attribute at a higher precedence, such as a role. See the quote below from the same document' Attribute Types section about override attributes:

    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.

    If you simply set these as default inside your wrapper cookbook's attributes/default.rb file then both the source cookbook and your wrapper are trying to set the same attribute at the same level. This is likely going to lead to unexpected behavior or simply not work.