【问题标题】:Understanding Chef Environment Attributes了解 Chef 环境属性
【发布时间】:2020-05-24 00:35:54
【问题描述】:

背景

我正在尝试了解如何正确使用配方中的环境属性。注意:我理解我的方法可能不是 Chef 的最佳实践;但是,我的目标是了解这里的数据流,而不一定要在生产应用程序中使用我的方法。

一些细节:

  1. 我正在使用 Vagrant 和 Chef -- 具体来说,chef-zero 供应商(因此我必须在我的环境中使用 ruby​​ 文件,而不是 由于厨师流浪者的限制而不是 JSON)。
  2. 这是我的目录结构:
├── environments
    ├── vm.rb
├── roles
    ├── base.json
├── cookbooks
    ├── init
        ├── recipes
            ├── default.rb
├── Vagrantfile

这是我的文件

环境:vm.rb

name "vm"
description "Configuration file for the Kukla Demo VM"
default_attributes(
custom_demo: {
    title: 'My demo title',
    description: 'My demo description'
))

角色:base.json

{
    "name": "base",
    "description": "Base VM configuration",
    "chef_type": "role",
    "json_class": "Chef::Role",
    "default_attributes": {},
    "override_attributes": {},
    "run_list": ["recipe[init::default]"]
}

配方:default.rb

#
# Cookbook:: init
# Recipe:: default
#
# Copyright:: 2020, The Authors, All Rights Reserved.

print 'I am here!'
print node[:custom_demo]

Vagrantfile 中的主厨供应商

...

chef.nodes_path = 'nodes/'
chef.environments_path = 'environments/'
chef.roles_path = 'roles/'
chef.cookbooks_path = 'cookbooks/'  

# Roles
chef.add_role "base"

...

预期行为

当我运行配置程序时,我希望看到在 chef 运行日志中打印出 custom_demo 哈希。比如:

...

==> Machine: Synchronizing Cookbooks:
==> Machine: [2020-02-07T20:37:15+00:00] INFO: Storing updated cookbooks/init/recipes/default.rb in the cache.
==> Machine:
==> Machine: - init (0.1.0)
==> Machine: Installing Cookbook Gems:
==> Machine: Compiling Cookbooks...
==> Machine: I am here! custom_demo => { :title => 'My demo title', :description => 'My demo description' }
==> Machine: Converging 0 resources
==> Machine: [2020-02-07T20:37:15+00:00] INFO: Chef Infra Client Run complete in 0.118042019 seconds
...

实际行为

相反,我得到:

...

==> Machine: Synchronizing Cookbooks:
==> Machine: [2020-02-07T20:37:15+00:00] INFO: Storing updated cookbooks/init/recipes/default.rb in the cache.
==> Machine:
==> Machine: - init (0.1.0)
==> Machine: Installing Cookbook Gems:
==> Machine: Compiling Cookbooks...
==> Machine: I am here!
==> Machine: Converging 0 resources
==> Machine: [2020-02-07T20:37:15+00:00] INFO: Chef Infra Client Run complete in 0.118042019 seconds
...

我的问题:

根据结果,我有以下问题:

  1. 我对环境属性(以及因此)的理解是否正确?
  2. 如果没有,我错过了什么?可以以这种方式使用环境属性吗?
  3. 调试环境是否被厨师使用的正确方法是什么?
  4. 在配方中引用环境属性以实现预期结果的正确方法是什么?

【问题讨论】:

    标签: ruby chef-infra


    【解决方案1】:

    您所做的一切都是正确的,您的期望是正确的。您缺少的唯一一件事是您没有将环境分配给虚拟机。为此,您需要在 Vagrantfile 中添加一行:

    ...
    
    chef.nodes_path = 'nodes/'
    chef.environments_path = 'environments/'
    chef.roles_path = 'roles/'
    chef.cookbooks_path = 'cookbooks/'
    chef.environment = 'vm' # the added line  
    
    # Roles
    chef.add_role "base"
    
    ...
    

    要了解,如果节点设置了正确的环境,您可以在配方中打印出来:

    p node.chef_environment
    # or
    p node.environment
    

    关于您的最后一个问题,您实际上无法从配方环境属性中引用。 Chef从不同的来源(配方、环境、节点等)收集属性并将它们组合成1个属性哈希using precedence,所以最后你只能引用具有最高优先级或某些特定优先级的属性,而不是按属性来源(环境、节点或配方)。

    node[:custom_demo] # highest precedence
    node.default[:custom_demo] # default precedence, ignoring override and normal
    

    【讨论】:

    • 天哪,这真是一个愚蠢的错过。非常感谢这里的解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多