了解 Chef 环境属性

Understanding Chef Environment Attributes

背景

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

一些细节:

  1. 我正在使用 Vagrant 和 Chef——具体来说,chef-zero 供应商(因此我必须为我的环境使用 ruby 文件而不是 比 JSON 由于 chef-vagrant 限制)。
  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"

...

预期行为

当我 运行 供应商时,我希望看到 custom_demo 散列打印在 chef 运行 日志中。类似于:

...

==> 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. 调试环境是否被 chef 使用的正确方法是什么?
  4. 在配方中引用环境属性以达到预期结果的正确方法是什么?

你做的一切都是对的,你的期望是正确的。您唯一缺少的是您没有将环境分配给虚拟机。为此,您需要在 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从不同的来源(recipe、environment、node等)收集属性并将它们组合成1个属性哈希using precedence,所以最后你只能引用具有最高优先级或具有某些特定优先级的属性,而不能通过属性源(环境、节点或配方)。

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