Overriding Cookbook Attribute in Vagrantfile Custom JSON Data

余生颓废 提交于 2019-12-07 13:51:20

问题


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"?


回答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"


来源:https://stackoverflow.com/questions/23201242/overriding-cookbook-attribute-in-vagrantfile-custom-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!