Skip to content

Instantly share code, notes, and snippets.

@jmaziarz
Last active June 24, 2016 20:15
Show Gist options
  • Save jmaziarz/354971f7b1f8ea9b949da4c8d09d7585 to your computer and use it in GitHub Desktop.
Save jmaziarz/354971f7b1f8ea9b949da4c8d09d7585 to your computer and use it in GitHub Desktop.
Chef's "deep merging" of array attributes doesn't work as expected...
# test-attributes-1/attributes/default.rb
default['test-attributes']['exclude'] = [ 'one', 'two', 'three' ]
# test-attributes-2/attributes/default.rb
default['test-attributes']['exclude'] = [ 'three', 'four', 'five' ]
# test-attributes-1/recipes/default.rb
include_recipe 'test-attributes-2'
Chef::Log.info(node['test-attributes']['exclude'].join(' '))
# Expected output
...
one two three four five
...
# Actual output
$ sudo chef-client -o "recipe[test-attributes-1]" --force-logger
[2016-06-24T15:16:06-04:00] INFO: Forking chef instance to converge...
[2016-06-24T15:16:06-04:00] INFO: *** Chef 12.6.0 ***
[2016-06-24T15:16:06-04:00] INFO: Chef-client pid: 23803
[2016-06-24T15:16:08-04:00] WARN: Run List override has been provided.
[2016-06-24T15:16:08-04:00] WARN: Original Run List: [role[rhel-base]]
[2016-06-24T15:16:08-04:00] WARN: Overridden Run List: [recipe[test-attributes-1]]
[2016-06-24T15:16:08-04:00] INFO: Run List is [recipe[test-attributes-1]]
[2016-06-24T15:16:08-04:00] INFO: Run List expands to [test-attributes-1]
[2016-06-24T15:16:08-04:00] INFO: Starting Chef Run for test-host.company.com
[2016-06-24T15:16:08-04:00] INFO: Running start handlers
[2016-06-24T15:16:08-04:00] INFO: Start handlers complete.
[2016-06-24T15:16:08-04:00] INFO: Loading cookbooks [test-attributes-2@0.1.0, test-attributes-1@0.1.0]
[2016-06-24T15:16:08-04:00] INFO: Skipping removal of obsoleted cookbooks from the cache
[2016-06-24T15:16:09-04:00] INFO: Storing updated cookbooks/test-attributes-2/attributes/default.rb in the cache.
[2016-06-24T15:16:09-04:00] INFO: Storing updated cookbooks/test-attributes-1/attributes/default.rb in the cache.
[2016-06-24T15:16:09-04:00] INFO: one two three
[2016-06-24T15:16:09-04:00] WARN: Skipping final node save because override_runlist was given
[2016-06-24T15:16:09-04:00] INFO: Chef Run complete in 0.707446046 seconds
[2016-06-24T15:16:09-04:00] INFO: Skipping removal of unused files from the cache
[2016-06-24T15:16:09-04:00] INFO: Running report handlers
[2016-06-24T15:16:09-04:00] INFO: Report handlers complete
[2016-06-24T15:16:09-04:00] INFO: Sending resource update report (run-id: 2ade7324-5022-4344-8eaa-103168c95d62)
# test-attributes-1/attributes/default.rb
default['test-attributes']['exclude'] = { 'one' => true, 'two' => true, 'three' => true }
# test-attributes-2/attributes/default.rb
default['test-attributes']['exclude'] = { 'three' => true, 'four' => true, 'five' => true }
# test-attributes-1/recipes/default.rb
include_recipe 'test-attributes-2'
Chef::Log.info(node['test-attributes']['exclude'].inject([]) {|memo, (key, value)| memo << key if value; memo})
# Expected output
...
["one", "two", "three", "four", "five"]
...
# Actual output
$ sudo chef-client -o "recipe[test-attributes-1]" --force-logger
[2016-06-24T16:10:18-04:00] INFO: Forking chef instance to converge...
[2016-06-24T16:10:18-04:00] INFO: *** Chef 12.6.0 ***
[2016-06-24T16:10:18-04:00] INFO: Chef-client pid: 10473
[2016-06-24T16:10:20-04:00] WARN: Run List override has been provided.
[2016-06-24T16:10:20-04:00] WARN: Original Run List: [role[rhel-base]]
[2016-06-24T16:10:20-04:00] WARN: Overridden Run List: [recipe[test-attributes-1]]
[2016-06-24T16:10:20-04:00] INFO: Run List is [recipe[test-attributes-1]]
[2016-06-24T16:10:20-04:00] INFO: Run List expands to [test-attributes-1]
[2016-06-24T16:10:20-04:00] INFO: Starting Chef Run for test-host.company.com
[2016-06-24T16:10:20-04:00] INFO: Running start handlers
[2016-06-24T16:10:20-04:00] INFO: Start handlers complete.
[2016-06-24T16:10:20-04:00] INFO: Loading cookbooks [test-attributes-2@0.1.0, test-attributes-1@0.1.0]
[2016-06-24T16:10:20-04:00] INFO: Skipping removal of obsoleted cookbooks from the cache
[2016-06-24T16:10:21-04:00] INFO: Storing updated cookbooks/test-attributes-1/recipes/default.rb in the cache.
[2016-06-24T16:10:21-04:00] INFO: test-attributes ...
[2016-06-24T16:10:21-04:00] INFO: ["one", "two", "three"]
[2016-06-24T16:10:21-04:00] WARN: Skipping final node save because override_runlist was given
[2016-06-24T16:10:21-04:00] INFO: Chef Run complete in 0.757465409 seconds
[2016-06-24T16:10:21-04:00] INFO: Skipping removal of unused files from the cache
[2016-06-24T16:10:21-04:00] INFO: Running report handlers
[2016-06-24T16:10:21-04:00] INFO: Report handlers complete
[2016-06-24T16:10:21-04:00] INFO: Sending resource update report (run-id: cfa0397e-cc82-45a2-b011-075bc248f08b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment