tinytelly
tinytelly

Reputation: 279

Do something when a Chef Data Bag Changes

Is there a way to do something when the values change in a Chef Data Bag.

Lets say a data bag looks like this.

{
    "data": {
        "version": "1.0.12-SNAPSHOT"
    }
}

and it changes to this

{
    "data": {
        "version": "1.0.13"
    }
}

is there a way for a Chef resource to 'Subscribe' to this change or this change to 'Notify' a Resource of the change?

Upvotes: 1

Views: 474

Answers (1)

Holger Just
Holger Just

Reputation: 55888

The common solution for this is not to subscribe to the change in the data bag but to sensibly handle the result of the change.

It seems that you describe a version of a software to be installed there. Thus, a sensible approach would be to perform these steps during each chef run:

  • check if the currently installed version of the software is the same as the one currently defined in the data bag
  • if it matches, do nothing
  • if it differs, update the software

That way, you don't need any actual publish/subscribe architecture but just handle everything during your regular chef runs.

In fact, this general approach is how about all providers in Chef work: check the current state and adapt it if it differs from the intended state as defined by the recipe.

Upvotes: 2

Related Questions