【发布时间】:2020-05-24 00:35:54
【问题描述】:
背景
我正在尝试了解如何正确使用配方中的环境属性。注意:我理解我的方法可能不是 Chef 的最佳实践;但是,我的目标是了解这里的数据流,而不一定要在生产应用程序中使用我的方法。
一些细节:
- 我正在使用 Vagrant 和 Chef -- 具体来说,
chef-zero供应商(因此我必须在我的环境中使用 ruby 文件,而不是 由于厨师流浪者的限制而不是 JSON)。 - 这是我的目录结构:
├── 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
...
我的问题:
根据结果,我有以下问题:
- 我对环境属性(以及因此)的理解是否正确?
- 如果没有,我错过了什么?可以以这种方式使用环境属性吗?
- 调试环境是否被厨师使用的正确方法是什么?
- 在配方中引用环境属性以实现预期结果的正确方法是什么?
【问题讨论】:
标签: ruby chef-infra