Last Updated: February 25, 2016
·
434
· trinitronx

Workaround CHEF-3918

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

The cause was CHEF-3918. 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, 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