Skip to content

Instantly share code, notes, and snippets.

@bbl
Last active November 14, 2017 15:16
Show Gist options
  • Save bbl/6aa0a221e31a3da5a13924f12f7d8dd9 to your computer and use it in GitHub Desktop.
Save bbl/6aa0a221e31a3da5a13924f12f7d8dd9 to your computer and use it in GitHub Desktop.
Chef: add node attributes automatically

Chef: add node attributes automatically

Using knife exec

The best way probably

knife exec -E "nodes.find(:name => 'chef-node') { |node|  node.normal_attrs['some_key'] = 'some_value'; node.save; }"

Using gem

Unofficial gem: https://github.com/pdf/knife-attribute


Using $EDITOR with script

  • Create a script editor.rb:
       #!/usr/bin/env ruby
       require 'json'

       data = JSON.parse(File.read(ARGV[0]))

       # You can read data from other json file on disk and
       # add key=value as attribute
       data['normal']['some_key'] = 'some_data'

       File.write(ARGV[0], JSON.pretty_generate(data))
  • Set the $EDITOR variable : export EDITOR=./path/to/editor.rb

  • Run knife command: knife node edit chef-node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment