3

How do you access node.override using chef.json in a Vagrant file?

For example, using vagrant-berkshelf, I'm trying to install a particular Maven version based on Custom JSON Data in the Vagrantfile:

  chef.json = {
  'maven' => {
    'version' => '3.0.5'    
    }
  }

cookbooks\maven_custom\attributes\default.rb

default['maven']['version'] = "3.2.1" 

cookbooks\maven_custom\recipes\default.rb

Chef::Log.info(node['maven']['version'])

When I run vagrant provision, the following gets printed out:

3.2.1

Additionally, I tried vagrant reload --provision, yet still saw "3.2.1" print out.

I would've expected 3.0.5 since I had (I thought) overridden it in my Vagrantfile.

How can I correctly extract the Vagrantfile's JSON value of "3.0.5"?

Thiago Figueiro
  • 410
  • 5
  • 15
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • possible duplicate of [Installing a Particular Library Version](http://stackoverflow.com/questions/22578830/installing-a-particular-library-version) – Mark O'Connor Apr 21 '14 at 18:07
  • Hi Mark. I'm using a `maven_custom` cookbook as opposed to the `opscode-cookbooks\maven` cookbook. I read your helpful answer in the post that you linked, but it did not answer my present question. – Kevin Meredith Apr 21 '14 at 18:09
  • I was confused by the question.... Let me make an attempt to re-create your issue. (Simply setting the version is not enough) – Mark O'Connor Apr 21 '14 at 21:25
  • An equivalent example for logrotate would be: how to access node.override['logrotate'] instead of node['logrotate']? – Thiago Figueiro Jan 18 '16 at 00:10

1 Answers1

1

Not entirely clear what the question is, but I'll assume you're trying to write a wrapper cookbook that installs a more modern version of Maven.

The trick is to set "normal" attributes in the wrapper cookbook which will override the "default" attributes of maven cookbook. For more details read about chef's attribute precedence

This is a better than providing run-time parameters, for the following reasons:

  1. You are writing a wrapper cookbook, so an attribute file would be the natural place to set values
  2. The "maven" cookbook requires setting 4 attributes to specify a new Maven version.

Hope this helps.

Example

├── attributes
│   └── maven.rb
├── Berksfile
├── Berksfile.lock
├── metadata.rb
├── recipes
│   └── default.rb
└── Vagrantfile

metadata.rb

name             'maven_custom'
maintainer       'YOUR_NAME'
maintainer_email 'YOUR_EMAIL'
license          'All rights reserved'
description      'Installs/Configures maven_custom'
long_description 'Installs/Configures maven_custom'
version          '0.1.0'

depends "apt"
depends "maven"

attributes/maven.rb

normal['maven']['version'] = 3
normal['maven']['3']['version'] = '3.2.1'
normal['maven']['3']['url'] = 'http://www.eu.apache.org/dist/maven/maven-3/3.2.1/binaries/apache-maven-3.2.1-bin.tar.gz'
normal['maven']['3']['checksum'] = 'cdee2fd50b2b4e34e2d67d01ab2018b051542ee759c07354dd7aed6f4f71675c'

recipes/default.rb

#
# Cookbook Name:: maven_custom
# Recipe:: default
#
include_recipe "apt"
include_recipe "maven"
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Hi Mark - thanks, I'll try this in the morning. But, basically my question was - how can I properly override the `node['maven']['version']` variable in the `Vagrantfile` with my custom cookbook? As I understand, the `cookbooks/my_cookbook/attributes/default.rb` reads in default values, but they can be overridden with `override` and `normal` values (Chef docs). However, I was confused why I wasn't able to override them in the `Vagrantfile` custom JSON data section. I'm not using the official `maven` cookbook at all. It's all homegrown without any wrapper. – Kevin Meredith Apr 22 '14 at 02:28
  • @KevinMeredith There is no "reading" of attributes. If your cookbook and the wrapped one both declare a default attribute I don't think you can be certain which would win. As for setting them from the vagrant file... Frankly I don't know, no mention is made of command-line json attributes in the doco (see: http://docs.opscode.com/chef_overview_attributes.html) In conclusion stick with cookbook attributes if you can. Much simpler. Hope this helps – Mark O'Connor Apr 22 '14 at 08:10
  • @KevinMeredith Use wrapper cookbooks in concert with the open source. This is the pattern I use in a corporate environment that does not allow external downloads. Ultimately it means you can leverage open source instead of re-inventing it!! – Mark O'Connor Apr 22 '14 at 08:12