perigee
perigee

Reputation: 9878

Is it possible to set data bag values from inside a Chef recipe?

Is it possible to set data bag values from inside a chef recipe? I need gather some information from one particular node, and then share it with another node. I want to do it inside a cookbook instead of manually by using knife.

Upvotes: 1

Views: 2010

Answers (1)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

TL;DR

You technically can do it, but don't. Data bags shouldn't be modified dynamically. That would make system state almost impossible to reason about.

Instead, use ohai to gather information about the local node at runtime, or use a ruby_block to query ohai or other data collection activity on a remote node.

Don't Do It

This use case is specifically addressed within the Chef data bag documentation. The documentation says no to do it, and explains why.

Creating and editing the contents of a data bag or a data bag item from a recipe is not recommended. The recommended method of updating a data bag or a data bag item is to use knife and the knife data bag subcommand. If this action must be done from a recipe, please note the following:

  • If two operations concurrently attempt to update the contents of a data bag, the last-written attempt will be the operation to update the contents of the data bag. This situation can lead to data loss, so organizations should take steps to ensure that only one chef-client is making updates to a data bag at a time.

  • Altering data bags from the node when using the open source Chef server requires the node’s API client to be granted admin privileges. In most cases, this is not advisable.

If you decide to do it anyway, the documentation provides some limited guidance. However, if you break it you get to keep both halves. :)

Upvotes: 3

Related Questions