Automate the attributes used in chef


#1

Hi

I want to automate the attributes file (default.rb) inside the cookbook. I have
a .json file (which is generated by another tool), with the details of the
attribute values that should be given to the cookbook. Please suggest me the
best method to achieve this goal.

Thank you.


#2

A simple way would be to write a script that would read the json file
and generate an attributes file that you can put in place. Something
like the following would work:

#!/usr/bin/env ruby
require ‘json’
data = JSON.parse(File.read(ARGV[0]))
data.each { |k, v| puts "default[‘cookbookname’][’#{k}’] = ‘#{v}’" }

Change cookbookname as appropriate. And this assumes the json file is
a simple set of key value pairs. This prints out the file contents so
you would want to run it as ‘myscript.rb >
cookbookname/attributes/default.rb’, or you could change the script to
write out the attributes file directly.

Regards,

Mark

On Tue, Jul 22, 2014 at 2:12 AM, anjumr06@gmail.com wrote:

Hi

I want to automate the attributes file (default.rb) inside the cookbook. I have
a .json file (which is generated by another tool), with the details of the
attribute values that should be given to the cookbook. Please suggest me the
best method to achieve this goal.

Thank you.


#3

How would you deal complex nested attribute trees, conditions, comments,
etc?

I’m not currently very experienced in Chef yet but this idea smells a bit
to me.
Would be nice to hear from experienced folks.

On 22 July 2014 15:25, Mark Harrison mark@mivok.net wrote:

A simple way would be to write a script that would read the json file
and generate an attributes file that you can put in place. Something
like the following would work:

#!/usr/bin/env ruby
require ‘json’
data = JSON.parse(File.read(ARGV[0]))
data.each { |k, v| puts "default[‘cookbookname’][’#{k}’] = ‘#{v}’" }

Change cookbookname as appropriate. And this assumes the json file is
a simple set of key value pairs. This prints out the file contents so
you would want to run it as ‘myscript.rb >
cookbookname/attributes/default.rb’, or you could change the script to
write out the attributes file directly.

Regards,

Mark

On Tue, Jul 22, 2014 at 2:12 AM, anjumr06@gmail.com wrote:

Hi

I want to automate the attributes file (default.rb) inside the cookbook.
I have
a .json file (which is generated by another tool), with the details of
the
attribute values that should be given to the cookbook. Please suggest me
the
best method to achieve this goal.

Thank you.

walter dolce | developer
twitter @walterdolce https://twitter.com/WalterDolce
skype walter.dolce
tel +39 (0) 327 597 9830


#4

On Jul 22, 2014, at 9:24 AM, Walter Dolce walterdolce@gmail.com wrote:

How would you deal complex nested attribute trees, conditions, comments, etc?

JSON doesn’t have conditions or comments, so I’m unsure how those apply.

–Noah


#5

If you have a JSON file with nested attributes, then you’ll need to
modify the script to walk the tree and generate attributes as
appropriate. It all depends on what you have in the json file and what
you want the output to look like. The script can be tweaked as needed
to do that. This idea is also based on already having a json file that
you would rather not manually type out an attributes file for. If you
don’t have that, then just write the attributes file out, complete
with conditionals, comments and so on.

On Tue, Jul 22, 2014 at 12:24 PM, Walter Dolce walterdolce@gmail.com wrote:

How would you deal complex nested attribute trees, conditions, comments,
etc?

I’m not currently very experienced in Chef yet but this idea smells a bit to
me.
Would be nice to hear from experienced folks.

On 22 July 2014 15:25, Mark Harrison mark@mivok.net wrote:

A simple way would be to write a script that would read the json file
and generate an attributes file that you can put in place. Something
like the following would work:

#!/usr/bin/env ruby
require ‘json’
data = JSON.parse(File.read(ARGV[0]))
data.each { |k, v| puts "default[‘cookbookname’][’#{k}’] = ‘#{v}’" }

Change cookbookname as appropriate. And this assumes the json file is
a simple set of key value pairs. This prints out the file contents so
you would want to run it as ‘myscript.rb >
cookbookname/attributes/default.rb’, or you could change the script to
write out the attributes file directly.

Regards,

Mark

On Tue, Jul 22, 2014 at 2:12 AM, anjumr06@gmail.com wrote:

Hi

I want to automate the attributes file (default.rb) inside the cookbook.
I have
a .json file (which is generated by another tool), with the details of
the
attribute values that should be given to the cookbook. Please suggest me
the
best method to achieve this goal.

Thank you.

walter dolce | developer
twitter @walterdolce
skype walter.dolce
tel +39 (0) 327 597 9830


#6

I’ve been heading tentatively down this path (code generation is always a
slippery slope) with part of the function in the knife-topo
https://github.com/christinedraper/knife-topo plugin for managing
topologies from a JSON file. I wanted to be able to define properties like
software versions along with runlists and other node attributes in a single
JSON file so they could be easily put in version control and updated. The
plugin has basic conditional support (value of a single node property) and
nested attributes. Lots of ways I’d like to enhance it, of course…

On Tue, Jul 22, 2014 at 12:09 PM, Mark Harrison mark@mivok.net wrote:

If you have a JSON file with nested attributes, then you’ll need to
modify the script to walk the tree and generate attributes as
appropriate. It all depends on what you have in the json file and what
you want the output to look like. The script can be tweaked as needed
to do that. This idea is also based on already having a json file that
you would rather not manually type out an attributes file for. If you
don’t have that, then just write the attributes file out, complete
with conditionals, comments and so on.

On Tue, Jul 22, 2014 at 12:24 PM, Walter Dolce walterdolce@gmail.com
wrote:

How would you deal complex nested attribute trees, conditions, comments,
etc?

I’m not currently very experienced in Chef yet but this idea smells a
bit to
me.
Would be nice to hear from experienced folks.

On 22 July 2014 15:25, Mark Harrison mark@mivok.net wrote:

A simple way would be to write a script that would read the json file
and generate an attributes file that you can put in place. Something
like the following would work:

#!/usr/bin/env ruby
require ‘json’
data = JSON.parse(File.read(ARGV[0]))
data.each { |k, v| puts "default[‘cookbookname’][’#{k}’] = ‘#{v}’" }

Change cookbookname as appropriate. And this assumes the json file is
a simple set of key value pairs. This prints out the file contents so
you would want to run it as ‘myscript.rb >
cookbookname/attributes/default.rb’, or you could change the script to
write out the attributes file directly.

Regards,

Mark

On Tue, Jul 22, 2014 at 2:12 AM, anjumr06@gmail.com wrote:

Hi

I want to automate the attributes file (default.rb) inside the
cookbook.

I have
a .json file (which is generated by another tool), with the details of
the
attribute values that should be given to the cookbook. Please suggest
me

the
best method to achieve this goal.

Thank you.

walter dolce | developer
twitter @walterdolce
skype walter.dolce
tel +39 (0) 327 597 9830