How to import data from my databag into cookbook attributes


#1

Hi,

hi, I am just using chef and trying to import data from my databag into the
mysql cookbook attribute server.rb.
I downloaded the mysql cookbook from
https://github.com/opscode-cookbooks/mysql and uploaded to my open source
chef server.

I added several lines at the top of the attributes file
attributes/server.rb like this:

the databag ‘website’ was defined, there is a default databag item

‘env_default’, if users have defined their own databag item,

here is node[mycluster], then the following code will use the users’

specific databag item.
if defined? node[‘mycluster’]
item = data_bag_item(‘website’,node[‘mycluster’])
else
item = data_bag_item(‘website’,‘env_default’)
end

when I run the mysql cookbook at a chef client, it prompt
*NoMethodError

Undefined node attribute or method data_bag_item' onnode’

Cookbook Trace:

/var/chef/cache/cookbooks/mysql/attributes/server.rb:21:in `from_file’*

When move these code into the file recipes/server.rb, then the error is
gone.
I am wondering can I import data from databag at attributes files?
If someone can give a hint or example, it will be much appreciated.

Thanks,
Sam


#2

data_bag_item isn’t available within the context of attribute files. You can load the data bag in your recipe (rescuing a missing one) and then fall back to default attributes using the OR operator. Otherwise, you can load the data bag and set node attributes based on the contents of it early on in the recipe and then use the attributes normally later in the run.

Either way, you may be able to accomplish your goals in a cleaner way. Can you explain further why you’re using data bags over just setting node attributes explicitly as part of the bootstrap or within environment files?

Sent from my iPhone

On Sep 7, 2013, at 5:42 PM, Sam Su sam.debian@gmail.com wrote:

Hi,

hi, I am just using chef and trying to import data from my databag into the mysql cookbook attribute server.rb.
I downloaded the mysql cookbook from https://github.com/opscode-cookbooks/mysql and uploaded to my open source chef server.

I added several lines at the top of the attributes file attributes/server.rb like this:

the databag ‘website’ was defined, there is a default databag item ‘env_default’, if users have defined their own databag item,

here is node[mycluster], then the following code will use the users’ specific databag item.

if defined? node[‘mycluster’]
item = data_bag_item(‘website’,node[‘mycluster’])
else
item = data_bag_item(‘website’,‘env_default’)
end

when I run the mysql cookbook at a chef client, it prompt
NoMethodError

Undefined node attribute or method data_bag_item' onnode’

Cookbook Trace:

/var/chef/cache/cookbooks/mysql/attributes/server.rb:21:in `from_file’

When move these code into the file recipes/server.rb, then the error is gone.
I am wondering can I import data from databag at attributes files?
If someone can give a hint or example, it will be much appreciated.

Thanks,
Sam


#3

Thank you so much for your timely help.

As you might know, most of my cookbooks are downloaded from internet, the
initial data of each cookbook are defined at the attribute files.
Now what I am trying to do is replace the initial data with mine and don’t
change the cookbook recipes. That is why I import data form my databag at
the file attributes/server.rb, then replace the default variables with the
data from my databag. Unfortunately, this way doesn’t work.

On Sun, Sep 8, 2013 at 8:51 AM, Daniel Condomitti daniel@condomitti.comwrote:

data_bag_item isn’t available within the context of attribute files. You
can load the data bag in your recipe (rescuing a missing one) and then fall
back to default attributes using the OR operator. Otherwise, you can load
the data bag and set node attributes based on the contents of it early on
in the recipe and then use the attributes normally later in the run.

Either way, you may be able to accomplish your goals in a cleaner way. Can
you explain further why you’re using data bags over just setting node
attributes explicitly as part of the bootstrap or within environment files?

Sent from my iPhone

On Sep 7, 2013, at 5:42 PM, Sam Su sam.debian@gmail.com wrote:

Hi,

hi, I am just using chef and trying to import data from my databag into
the mysql cookbook attribute server.rb.
I downloaded the mysql cookbook from
https://github.com/opscode-cookbooks/mysql and uploaded to my open source
chef server.

I added several lines at the top of the attributes file
attributes/server.rb like this:

the databag ‘website’ was defined, there is a default databag item

‘env_default’, if users have defined their own databag item,

here is node[mycluster], then the following code will use the users’

specific databag item.
if defined? node[‘mycluster’]
item = data_bag_item(‘website’,node[‘mycluster’])
else
item = data_bag_item(‘website’,‘env_default’)
end

when I run the mysql cookbook at a chef client, it prompt
*NoMethodError

Undefined node attribute or method data_bag_item' onnode’

Cookbook Trace:

/var/chef/cache/cookbooks/mysql/attributes/server.rb:21:in `from_file’*

When move these code into the file recipes/server.rb, then the error is
gone.
I am wondering can I import data from databag at attributes files?
If someone can give a hint or example, it will be much appreciated.

Thanks,
Sam


#4

Sam,

Consider using the application/wrapper cookbook pattern described
herehttp://devopsanywhere.blogspot.com/2012/11/how-to-write-reusable-chef-cookbooks.html.
Applying it to your situation, you would create an application cookbook
with a recipe that reads your data bag and overrides the mysql cookbook’s
attributes with the data bag values, then calls the mysql recipe(s) with
include_recipe. Then replace the mysql recipes in your run_list with the
application cookbook recipe. That way, no changes to the mysql cookbook are
required.

Thanks,
Kevin C.

On Sat, Sep 7, 2013 at 9:03 PM, Sam Su sam.debian@gmail.com wrote:

Thank you so much for your timely help.

As you might know, most of my cookbooks are downloaded from internet, the
initial data of each cookbook are defined at the attribute files.
Now what I am trying to do is replace the initial data with mine and don’t
change the cookbook recipes. That is why I import data form my databag at
the file attributes/server.rb, then replace the default variables with the
data from my databag. Unfortunately, this way doesn’t work.

On Sun, Sep 8, 2013 at 8:51 AM, Daniel Condomitti daniel@condomitti.comwrote:

data_bag_item isn’t available within the context of attribute files. You
can load the data bag in your recipe (rescuing a missing one) and then fall
back to default attributes using the OR operator. Otherwise, you can load
the data bag and set node attributes based on the contents of it early on
in the recipe and then use the attributes normally later in the run.

Either way, you may be able to accomplish your goals in a cleaner way.
Can you explain further why you’re using data bags over just setting node
attributes explicitly as part of the bootstrap or within environment files?

Sent from my iPhone

On Sep 7, 2013, at 5:42 PM, Sam Su sam.debian@gmail.com wrote:

Hi,

hi, I am just using chef and trying to import data from my databag into
the mysql cookbook attribute server.rb.
I downloaded the mysql cookbook from
https://github.com/opscode-cookbooks/mysql and uploaded to my open
source chef server.

I added several lines at the top of the attributes file
attributes/server.rb like this:

the databag ‘website’ was defined, there is a default databag item

‘env_default’, if users have defined their own databag item,

here is node[mycluster], then the following code will use the users’

specific databag item.
if defined? node[‘mycluster’]
item = data_bag_item(‘website’,node[‘mycluster’])
else
item = data_bag_item(‘website’,‘env_default’)
end

when I run the mysql cookbook at a chef client, it prompt
*NoMethodError

Undefined node attribute or method data_bag_item' onnode’

Cookbook Trace:

/var/chef/cache/cookbooks/mysql/attributes/server.rb:21:in `from_file’*

When move these code into the file recipes/server.rb, then the error is
gone.
I am wondering can I import data from databag at attributes files?
If someone can give a hint or example, it will be much appreciated.

Thanks,
Sam