Declaring an empty array and looping through attributes


#1

Hi All,

Is there a way of defining attributes as an open ended array and then passing JSON to these attributes?

So in one instance I could pass:

{
“stuff”: {
“mystuff”: {
“server1value”: “fred.fred.local”,
“server2value”: “bob.fred.local”,
“server3value”: “jim.fred.local”
}
}

And the next i could do:

{
“stuff”: {
“mystuff”: {
“server1value”: “fred.fred.local”,
“server2value”: “bob.fred.local”,
“server3value”: “jim.fred.local”,
“server4value”: “barry.fred.local”
}
}

And contained within the default attributes file, somehow an open declaration of attributes?

Cheers,
Simon.


#2

I think what you are looking for is attribute merging and attribute precedence. https://docs.chef.io/attributes.html
You can define your default values in the cookbooks attributes:

default['stuff']['mystuff']['server1value'] = 'fred.fred.local'
default['stuff']['mystuff']['server2value'] = 'bob.fred.local'
default['stuff']['mystuff']['server3value'] = 'jim.fred.local'

And then have the values from you node merged into that

{
  "name": "barry.fred.local",
  "chef_environment": "prod",
  "run_list": [....]
  "normal": {
    "stuff": {
      "mystuff": {
        "server4value": "barry.fred.local"
      }
    }
  }
}