简体   繁体   English

如何将 ruby dsl chef 环境文件转换为 JSON 格式?

[英]How to convert ruby dsl chef environment file to JSON format?

How can I convert chef environment file which has Ruby DSL format to json?如何将具有 Ruby DSL 格式的厨师环境文件转换为 json? Is that possible?那可能吗?

I would like to use environment file attributes in a ruby script but I cannot parse it.我想在 ruby 脚本中使用环境文件属性,但我无法解析它。 Are there any way to parse these.rb files?有没有办法解析这些.rb 文件?

Thanks,谢谢,

Tomszy汤姆齐

You can do it the same way Chef does:)你可以像厨师一样做:)

https://github.com/chef/chef/blob/master/lib/chef/environment.rb https://github.com/chef/chef/blob/master/lib/chef/environment.rb

require 'chef'
Chef::Config[:environment_path] = '/path/to/directory/with/rb_file'
env = Chef::Environmment.load_from_file('environment_name') # ! => not filename!
env.to_json

I've used this ruby script to convert ruby DSL files to JSON.我已使用此 ruby 脚本将 ruby DSL 文件转换为 JSON。

#!/usr/bin/env ruby
    
envfilename = '/path/to/environment.rb'
rolefilename = '/path/to/role.rb'

role = Chef::Role.new
role.name(File.basename(rolefilename, ".rb"))
role.from_file(rolefilename)
File.open(rolefilename.gsub(".rb", ".json"),"w") {|f| f.puts(role.to_json)}

env = Chef::Environment.new
env.name(File.basename(envfilename, ".rb"))
env.from_file(envfilename)
File.open(envfilename.gsub(".rb", ".json"),"w") {|f| f.puts(env.to_json)}

I got this script by modifying the code I found here: https://knife-zero.github.io/tips/use_ruby_dsl_for_envs_or_roles/我通过修改在这里找到的代码得到了这个脚本: https://knife-zero.github.io/tips/use_ruby_dsl_for_envs_or_roles/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM