Skip to content

Instantly share code, notes, and snippets.

@bjorand
Last active April 20, 2016 16:02
Show Gist options
  • Save bjorand/cd6f70e569d98f1c0ab69f06fa38bf3c to your computer and use it in GitHub Desktop.
Save bjorand/cd6f70e569d98f1c0ab69f06fa38bf3c to your computer and use it in GitHub Desktop.
Chef solo compile attribute
require 'mixlib/cli'
require "chef"
require "chef/config"
require "chef/config_fetcher"
require 'chef/workstation_config_loader'
class MyCLI
include Mixlib::CLI
option :config_file,
short: "-c CONFIG",
long: "--config CONFIG",
description: "The configuration file to use"
option :json_attribs,
short: "-j JSON_ATTRIBS",
long: "--json-attributes JSON_ATTRIBS",
description: "Load attributes from a JSON file or URL"
def run(argv=ARGV)
parse_options(argv)
end
end
def cli
@cli ||= MyCLI.new
end
cli.run
Chef::Config[:solo] = true
config_fetcher = Chef::ConfigFetcher.new(cli.config[:json_attribs])
@json_attribs = config_fetcher.fetch_json
Chef::WorkstationConfigLoader.new(cli.config[:config_file]).load
Chef::Config.merge!(cli.config)
node = Chef::Node.new
node.consume_attributes(@json_attribs)
expansion = node.run_list.expand(node.environment, data_source="disk")
node.apply_expansion_attributes(expansion)
puts Chef::JSONCompat.to_json_pretty(node.each_attribute.to_h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment