Last Updated: February 25, 2016
·
4.961K
· hannesg

Chef: Avoid execute, ruby_block, ...

... at least in recipes. The point is: if possible you should describe the desired result and let chef figure out the way to get there. With an execute or ruby_block you only describe the way, but don't say a word about the result. That's why execute and friends are usually not portable. Furthermore, execute resources are always marked as updated.

So, please, use them only in providers. If you must use them in recipes, then give them an if or not_if which checks whether they are actually necessary.

6 Responses
Add your response

If your execute resource results in the creation of a file/directory, you can use the creates attribute to indicate whether or not it should be run. http://wiki.opscode.com/display/chef/Resources#Resources-Execute

over 1 year ago ·

It would also be nice to know if you have any suggestions how to do that... I agree that executing commands is not really idempotent, but it is also true that sometimes Chef is kind of cryptic when it comes to extend its Providers and Resources.

over 1 year ago ·

Hi autolykos!

I turn the commands into LWRPs. This takes some hours but the my colleagues appreciate having a descriptive LWRP instead of a command.

I usually look first whether there is something like a check-command which I can if this command should be excuted. For example domainjoin-cli has the subcommand query. The provider then looks somewhat like this: https://github.com/opscode-cookbooks/samba/blob/master/providers/user.rb

over 1 year ago ·

I catch your drift. Even though the provider action is just a tiny layer on top of the command, making the actions explicit already helps readability a lot and, I suppose, also correctness.

Thx

over 1 year ago ·

You hit the mark! A thing I've forgot to mention: some commands exit with a certain exit code if they did nothing. You can then check this exitcode when setting updated_by_last_action.

You're welcome!

over 1 year ago ·

Or...only execute them with a notification!

over 1 year ago ·