Last Updated: February 25, 2016
·
546
· trinitronx

Workaround CHEF-3918

Recently when migrating all my chef roles to .json format for use with the wonderful [knife-essentials][1] gem, I ran into a problem with roles when provisioning with chef-solo.

The cause was [CHEF-3918][2]. The sed command to fix it was a pain to get working on OSX due to the slight differences between how the GNU and BSD versions of sed [handle newlines][3], so I am sharing this so others can benefit.

Chef Roles

On OSX or BSD-based sed:

for f in $(ls -1 roles/*.json); do  sed -i '' -e 's/^{/{\'$'\n  "json_class": "Chef::Role",/' $f; done

On Linux or GNU-based sed:

for f in $(ls -1 roles/*.json); do  sed -i -e 's/^{/{\n  "json_class": "Chef::Role",/' $f; done

Chef Environments

For environments, just replace Chef::Role with Chef::Environment.

On OSX or BSD-based sed:

for f in $(ls -1 environments/*.json); do  sed -i '' -e 's/^{/{\'$'\n  "json_class": "Chef::Environment",/' $f; done

On Linux or GNU-based sed:

for f in $(ls -1 environments/*.json); do  sed -i -e 's/^{/{\n  "json_class": "Chef::Environment",/' $f; done

[1]: http://rubygems.org/gems/knife-essentials
[2]: https://tickets.opscode.com/browse/CHEF-3918
[3]: http://stackoverflow.com/a/11163357/645491