了解Chef环境属性

问题描述 投票:1回答:1

背景

我试图了解如何正确使用配方中的环境属性。注意:我了解我的方法可能不是Chef的最佳做法。但是,我的目标是了解此处的数据流,而不必在生产应用程序中使用我的方法。

一些细节:

  1. 我正在使用Vagrant and 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"

...

预期行为

[当我运行预配器时,我希望看到在厨师运行日志中打印的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. 引用配方中的环境属性以达到预期效果的正确方法是什么?

背景我试图了解如何正确使用配方中的环境属性。注意:我了解我的方法可能不是Chef的最佳做法。但是,我的目标是...

ruby chef
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.