Lechucico
Lechucico

Reputation: 2102

Chef node attributes

I would like to check if the following path to the attribute exists or not:

s3_bucket = node['elastic']['s3']['bucket']

This is the json:

"elastic": {
   "version": "5.4.1",
   "cluster": "cluster-dev",
   "node": "node1",
   "host": "localhost",
   "port": 9201,
   "username": "elasticsearch",
   "groupname": "elasticsearch",
   "s3" : {
      "bucket": "hello"
   }
}

The field s3 may not exist depending on the environment, and I would like to check that before taking the attribute bucket. How I can do that?

Upvotes: 2

Views: 1723

Answers (2)

coderanger
coderanger

Reputation: 54211

With newer Chef, you can use the node.read method:

s3_bucket = node.read('elastic', 's3', 'bucket')

It will be nil if any intervening key does not exist.

Upvotes: 4

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

s3_bucket = node['elastic']['s3']['bucket'] if node['elastic']['s3']

Upvotes: 1

Related Questions