Vagrantfile自定义JSON数据中的覆盖Cookbook属性 [英] Overriding Cookbook Attribute in Vagrantfile Custom JSON Data

查看:102
本文介绍了Vagrantfile自定义JSON数据中的覆盖Cookbook属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Vagrant文​​件中使用Chef.json访问node.override?

例如,使用vagrant-berkshelf,我试图基于Vagrantfile中的自定义JSON数据"安装特定的Maven版本:

  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'])

当我运行vagrant provision时,将打印出以下内容:

3.2.1

此外,我尝试了vagrant reload --provision,但仍然看到打印出了"3.2.1".

我本来希望3.0.5,因为我已经(在我以为)在Vagrantfile中覆盖了它.

如何正确提取Vagrantfile's JSON值"3.0.5"?

解决方案

尚不完全清楚问题是什么,但我假设您正在尝试编写一本包装食谱,以安装更现代的Maven版本.

诀窍是在包装食谱中设置普通"属性,该属性将覆盖Maven食谱的默认"属性.有关更多详细信息,请参阅厨师的属性优先级

由于以下原因,这比提供运行时参数要好:

  1. 您正在编写包装指南,因此属性文件将是设置值的自然地方
  2. "maven"食谱需要设置 4个属性以指定新的Maven版本.

希望这会有所帮助.

示例

├── 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'

食谱/default.rb

#
# Cookbook Name:: maven_custom
# Recipe:: default
#
include_recipe "apt"
include_recipe "maven"

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

解决方案

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"

这篇关于Vagrantfile自定义JSON数据中的覆盖Cookbook属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆