# Overriding attributes

**URL:** <https://discourse.chef.io/t/overriding-attributes/4149>\
**Category:** Chef Infra (archive)\
**Created:** [June 28, 2013, 8:55am UTC](https://discourse.chef.io/t/overriding-attributes/4149 "2013-06-28T08:55:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@Jeffrey\_Jones](https://discourse.chef.io/u/Jeffrey_Jones)\
**Post date:** [June 28, 2013, 8:55am UTC](https://discourse.chef.io/t/overriding-attributes/4149/1 "2013-06-28T08:55:01Z")

</div>

Hello all

I have a list of default users I would like for my DB and am attempting  
to combine them with ones set in an attribute.json file used by chef  
solo. The relevant code is below

> <https://gist.github.com/rurounijones/5883385>

If there is nothing set in the json file then it write the 3 default  
values to pg\_hba.conf

However if there are values set in the json then ONLY the values in the  
.json file are used. They are not merged in with the default\_db\_users  
despite my setting of them (I tried everything up to force\_override  
which, as far as I see it, should take precedence),

Am I missing something here?

Cheers

Jeff

---

<div class="post-metadata">

**Author:** ![DV1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/dv1/32/186_2.png) [@DV1](https://discourse.chef.io/u/DV1)\
**Post date:** [March 18, 2014, 6:55pm UTC](https://discourse.chef.io/t/overriding-attributes/4149/2 "2014-03-18T18:55:25Z")

</div>

Ran into this issue today so thought I'd revive this thread.

I'm using postgresql cookbook with chef-solo and cookbook attributes  
aren't merged together with attributes set in .json file passed to  
chef-solo. Instead .json file attributes override those set in  
cookbook.

Here's what's in postgresql/attributes/default.rb:  
...  
default['postgresql']['pg\_hba'] = [  
{:type =\> 'local', :db =\> 'all', :user =\> 'postgres', :addr =\> nil,  
:method =\> 'ident'},  
{:type =\> 'local', :db =\> 'all', :user =\> 'all', :addr =\> nil,  
:method =\> 'ident'},  
{:type =\> 'host', :db =\> 'all', :user =\> 'all', :addr =\>  
'127.0.0.1/32', :method =\> 'md5'},  
{:type =\> 'host', :db =\> 'all', :user =\> 'all', :addr =\> '::1/128',  
:method =\> 'md5'}  
]  
...

Here's what in my .json file:  
...  
{  
...  
"postgresql": {  
...  
"pg\_hba": [  
{ "type": "host", "db": "replication", "user": "postgres",  
"addr": "10.0.0.0/8", "method": "trust" }  
],  
...  
"run\_list": ["recipe[postgresql::server]" ]  
}

Resulting pg\_hba.conf has only one line in it:

host replication postgres 10.0.0.0/8 trust

Since node[:postgresq][:pg\_hba] is an array attribute, it should be  
merged rather than overwritten.

The behavior is same with chef-solo 10.28.0 and 11.6.0.

I'll go ahead and submit that as a defect unless someone thinks it's  
intended behavior.

On Fri, Jun 28, 2013 at 1:55 AM, Jeffrey Jones [jjones@toppan-f.co.jp](mailto:jjones@toppan-f.co.jp) wrote:

> Hello all
> 
> I have a list of default users I would like for my DB and am attempting to  
> combine them with ones set in an attribute.json file used by chef solo. The  
> relevant code is below
> 
> [attributes.json · GitHub](https://gist.github.com/rurounijones/5883385)
> 
> If there is nothing set in the json file then it write the 3 default values  
> to pg\_hba.conf
> 
> However if there are values set in the json then ONLY the values in the  
> .json file are used. They are not merged in with the default\_db\_users  
> despite my setting of them (I tried everything up to force\_override which,  
> as far as I see it, should take precedence),
> 
> Am I missing something here?
> 
> Cheers
> 
> Jeff

--  
Best regards, Dmitriy V.

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [March 19, 2014, 3:17pm UTC](https://discourse.chef.io/t/overriding-attributes/4149/3 "2014-03-19T15:17:33Z")

</div>

On Tuesday, March 18, 2014 at 11:55 AM, DV wrote:

> Ran into this issue today so thought I'd revive this thread.
> 
> I'm using postgresql cookbook with chef-solo and cookbook attributes  
> aren't merged together with attributes set in .json file passed to  
> chef-solo. Instead .json file attributes override those set in  
> cookbook.
> 
> Here's what's in postgresql/attributes/default.rb:  
> ...  
> default['postgresql']['pg\_hba'] = [  
> {:type =\> 'local', :db =\> 'all', :user =\> 'postgres', :addr =\> nil,  
> :method =\> 'ident'},  
> {:type =\> 'local', :db =\> 'all', :user =\> 'all', :addr =\> nil,  
> :method =\> 'ident'},  
> {:type =\> 'host', :db =\> 'all', :user =\> 'all', :addr =\>  
> '127.0.0.1/32', :method =\> 'md5'},  
> {:type =\> 'host', :db =\> 'all', :user =\> 'all', :addr =\> '::1/128',  
> :method =\> 'md5'}  
> ]  
> ...
> 
> Here's what in my .json file:  
> ...  
> {  
> ...  
> "postgresql": {  
> ...  
> "pg\_hba": [  
> { "type": "host", "db": "replication", "user": "postgres",  
> "addr": "10.0.0.0/8", "method": "trust" }  
> ],  
> ...  
> "run\_list": ["recipe[postgresql::server]" ]  
> }
> 
> Resulting pg\_hba.conf has only one line in it:
> 
> host replication postgres 10.0.0.0/8 trust
> 
> Since node[:postgresq][:pg\_hba] is an array attribute, it should be  
> merged rather than overwritten.
> 
> The behavior is same with chef-solo 10.28.0 and 11.6.0.
> 
> I'll go ahead and submit that as a defect unless someone thinks it's  
> intended behavior.
> 
> On Fri, Jun 28, 2013 at 1:55 AM, Jeffrey Jones \<jjones@toppan-f.co.jp ([mailto:jjones@toppan-f.co.jp](mailto:jjones@toppan-f.co.jp))\> wrote:
> 
> > Hello all
> > 
> > I have a list of default users I would like for my DB and am attempting to  
> > combine them with ones set in an attribute.json file used by chef solo. The  
> > relevant code is below
> > 
> > [attributes.json · GitHub](https://gist.github.com/rurounijones/5883385)
> > 
> > If there is nothing set in the json file then it write the 3 default values  
> > to pg\_hba.conf
> > 
> > However if there are values set in the json then ONLY the values in the  
> > .json file are used. They are not merged in with the default\_db\_users  
> > despite my setting of them (I tried everything up to force\_override which,  
> > as far as I see it, should take precedence),
> > 
> > Am I missing something here?
> > 
> > Cheers
> > 
> > Jeff
> 
> --  
> Best regards, Dmitriy V.

Arrays are difficult to get right, because the behavior you want totally depends on what your use case is. For example, you might use an array to specify a list of ports that a server should bind to. So your cookbook might specify, say, port 80 and 443 for an HTTP server. If arrays merged in a purely additive way, then you would not be able to specify that the server should listen only on, say, 8000 and 8443.

Chef attempts to reach a compromise by merging arrays within precedence levels, that is, default attributes regardless of source (environment, role, cookbook) are merged, as are override attributes from those sources. Arrays are replaced between different precedence levels, however, so when you use `-j SOME_JSON`, these become “normal” level attributes, where arrays will replace anything set at the “default” level (which is what cookbooks typically do).

Your best bet is to work with the cookbook maintainer to come up with a non-array-based attributes API, which will behave the way you want.

--  
Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![DV1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/dv1/32/186_2.png) [@DV1](https://discourse.chef.io/u/DV1)\
**Post date:** [March 21, 2014, 11:11pm UTC](https://discourse.chef.io/t/overriding-attributes/4149/4 "2014-03-21T23:11:27Z")

</div>

Thanks Daniel, now it makes a little more sense. I guess it would be  
helpful if precedence of attributes could be defined in the JSON files  
that are passed to chef-solo so that there's a choice of what happens  
to them.

On Wed, Mar 19, 2014 at 8:17 AM, Daniel DeLeo [dan@kallistec.com](mailto:dan@kallistec.com) wrote:

> On Tuesday, March 18, 2014 at 11:55 AM, DV wrote:
> 
> > Ran into this issue today so thought I'd revive this thread.
> > 
> > I'm using postgresql cookbook with chef-solo and cookbook attributes  
> > aren't merged together with attributes set in .json file passed to  
> > chef-solo. Instead .json file attributes override those set in  
> > cookbook.
> > 
> > Here's what's in postgresql/attributes/default.rb:  
> > ...  
> > default['postgresql']['pg\_hba'] = [  
> > {:type =\> 'local', :db =\> 'all', :user =\> 'postgres', :addr =\> nil,  
> > :method =\> 'ident'},  
> > {:type =\> 'local', :db =\> 'all', :user =\> 'all', :addr =\> nil,  
> > :method =\> 'ident'},  
> > {:type =\> 'host', :db =\> 'all', :user =\> 'all', :addr =\>  
> > '127.0.0.1/32', :method =\> 'md5'},  
> > {:type =\> 'host', :db =\> 'all', :user =\> 'all', :addr =\> '::1/128',  
> > :method =\> 'md5'}  
> > ]  
> > ...
> > 
> > Here's what in my .json file:  
> > ...  
> > {  
> > ...  
> > "postgresql": {  
> > ...  
> > "pg\_hba": [  
> > { "type": "host", "db": "replication", "user": "postgres",  
> > "addr": "10.0.0.0/8", "method": "trust" }  
> > ],  
> > ...  
> > "run\_list": ["recipe[postgresql::server]" ]  
> > }
> > 
> > Resulting pg\_hba.conf has only one line in it:
> > 
> > host replication postgres 10.0.0.0/8 trust
> > 
> > Since node[:postgresq][:pg\_hba] is an array attribute, it should be  
> > merged rather than overwritten.
> > 
> > The behavior is same with chef-solo 10.28.0 and 11.6.0.
> > 
> > I'll go ahead and submit that as a defect unless someone thinks it's  
> > intended behavior.
> > 
> > On Fri, Jun 28, 2013 at 1:55 AM, Jeffrey Jones \<jjones@toppan-f.co.jp ([mailto:jjones@toppan-f.co.jp](mailto:jjones@toppan-f.co.jp))\> wrote:
> > 
> > > Hello all
> > > 
> > > I have a list of default users I would like for my DB and am attempting to  
> > > combine them with ones set in an attribute.json file used by chef solo. The  
> > > relevant code is below
> > > 
> > > [attributes.json · GitHub](https://gist.github.com/rurounijones/5883385)
> > > 
> > > If there is nothing set in the json file then it write the 3 default values  
> > > to pg\_hba.conf
> > > 
> > > However if there are values set in the json then ONLY the values in the  
> > > .json file are used. They are not merged in with the default\_db\_users  
> > > despite my setting of them (I tried everything up to force\_override which,  
> > > as far as I see it, should take precedence),
> > > 
> > > Am I missing something here?
> > > 
> > > Cheers
> > > 
> > > Jeff
> > 
> > --  
> > Best regards, Dmitriy V.
> 
> Arrays are difficult to get right, because the behavior you want totally depends on what your use case is. For example, you might use an array to specify a list of ports that a server should bind to. So your cookbook might specify, say, port 80 and 443 for an HTTP server. If arrays merged in a purely additive way, then you would not be able to specify that the server should listen only on, say, 8000 and 8443.
> 
> Chef attempts to reach a compromise by merging arrays within precedence levels, that is, default attributes regardless of source (environment, role, cookbook) are merged, as are override attributes from those sources. Arrays are replaced between different precedence levels, however, so when you use `-j SOME_JSON`, these become “normal” level attributes, where arrays will replace anything set at the “default” level (which is what cookbooks typically do).
> 
> Your best bet is to work with the cookbook maintainer to come up with a non-array-based attributes API, which will behave the way you want.
> 
> --  
> Daniel DeLeo

--  
Best regards, Dmitriy V.
