如何在本地将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:
- Navigate to your cookbook's root directory in your terminal.
- Start Chef Shell in local mode (which simulates a node environment):
chef-shell -z - 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) - Type
exitto 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
jsonvariable to the YAML file instead of theyamlvariable. That would result in JSON content inside a.ymlfile—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
相关产品推荐
相关产品推荐

