You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在本地将Chef cookbook的attributes/default.rb属性导出为JSON(Chef 12.21.26)

如何在本地导出Chef Cookbook的合并属性为JSON/YAML(Chef 12.21.26)

Absolutely, there are a couple of straightforward ways to export your merged Chef attributes locally without running recipes on a target node. Let's break down the most reliable methods for your Chef 12.21.26 version:

方法1:使用Chef Shell(交互式方式)

This is great for quick testing and one-off exports:

  1. Navigate to your cookbook's root directory in your terminal.
  2. Start Chef Shell in local mode (which simulates a node environment):
    chef-shell -z
    
  3. In the interactive shell, run these commands to load your attributes and export them:
    # Initialize an empty node object
    node = Chef::Node.new
    
    # Load all attributes from your cookbook's attributes/default.rb
    node.from_file(File.join(Dir.pwd, 'attributes', 'default.rb'))
    
    # Export to pretty-printed JSON
    File.write('/tmp/node-dump.json', Chef::JSONCompat.to_json_pretty(node.to_hash))
    
    # Export to YAML
    File.write('/tmp/node-dump.yml', node.to_yaml)
    
  4. Type exit to leave the shell—your exported files will be in /tmp/.

If you have multiple attribute files (e.g., attributes/production.rb), just repeat the node.from_file() line for each file to load all of them.

方法2:编写独立Ruby脚本

For repeatable exports, create a standalone script that you can run anytime:

Create a file named export_attributes.rb in your cookbook root with this code:

require 'chef'
require 'chef/json_compat'

# Set Chef to look for cookbooks in the current directory
Chef::Config.cookbook_path = [Dir.pwd]

# Initialize a blank node
node = Chef::Node.new

# Load all attribute files in the attributes directory
attributes_dir = File.join(Dir.pwd, 'attributes')
if Dir.exist?(attributes_dir)
  # Load each .rb file in attributes/
  Dir.glob("#{attributes_dir}/*.rb").sort.each do |attr_file|
    # Use Chef's DSL to properly load attribute directives like node.default
    class AttributeLoader
      include Chef::DSL::Attributes
      attr_reader :node
      def initialize(node)
        @node = node
      end
      def load(file)
        instance_eval(File.read(file), file, 1)
      end
    end

    loader = AttributeLoader.new(node)
    loader.load(attr_file)
    puts "Loaded attributes from #{attr_file}"
  end
else
  puts "Error: attributes directory not found in current path!"
  exit 1
end

# Export merged attributes to JSON
json_output = Chef::JSONCompat.to_json_pretty(node.to_hash)
File.write('/tmp/node-dump.json', json_output)
puts "Successfully exported JSON to /tmp/node-dump.json"

# Export merged attributes to YAML
yaml_output = node.to_yaml
File.write('/tmp/node-dump.yml', yaml_output)
puts "Successfully exported YAML to /tmp/node-dump.yml"

Run the script with:

ruby export_attributes.rb

重要注意事项

  • Fix your sample code error: In your provided example, you accidentally wrote the json variable to the YAML file instead of the yaml variable. That would result in JSON content inside a .yml file—make sure to use the correct variable when writing each format.
  • Attribute precedence: Chef 12.21.26 respects the standard attribute priority hierarchy (default < normal < override < force_override), so these methods will correctly merge attributes according to their precedence just like a real Chef run would.

内容的提问来源于stack exchange,提问作者Ondra Žižka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:16:47