Data-Bag how to use in templates

HI,

Yesterday I tested the Data-Bag! But I’m not able to use the Information from the DataBag in Templates.
My recipe is:
GLOBAL = data_bag_item(‘GLOBAL’, ‘NTP’) and just use a template-function (create a file from file.erb)

In my file.erb:
<%= node[:GLOBAL][:ntp_2_thalesgroup.at] %>

But If I start a chef-client I will get this problem:
Chef::Mixin::Template::TemplateError (undefined method `at’ for :ntp_2_thalesgroup:Symbol) on line #3:

Could anybody help me please?!

Regards
Andreas

A symbol cannot have a period within its name without being quoted;
consequently, the ".at", instead of being considered part of the symbol, is
being parsed as the name of a method to call on the symbol itself. (Also,
it's unusual to make a symbol's name all-uppercase -- they're special
constants by definition, they don't need any kind of capitalization).

Frankly, this looks to me like a case where you want a string, anyhow:

node[:global]["ntp_2_thalesgroup.at"]

would be more usual, and also would work.

On Wed, Mar 23, 2011 at 2:21 AM, WEINHAPL Andreas <
andreas.weinhapl@thalesgroup.com> wrote:

HI,

Yesterday I tested the Data-Bag! But I’m not able to use the Information
from the DataBag in Templates.

My recipe is:

GLOBAL = data_bag_item('GLOBAL', 'NTP') and just use a template-function
(create a file from file.erb)

In my file.erb:

<%= node[:GLOBAL][:ntp_2_thalesgroup.at] %>

But If I start a chef-client I will get this problem:

Chef::Mixin::Template::TemplateError (undefined method `at' for
:ntp_2_thalesgroup:Symbol) on line #3:

Could anybody help me please?!

Regards

Andreas

On 23 March 2011 07:21, WEINHAPL Andreas
andreas.weinhapl@thalesgroup.com wrote:

HI,

Yesterday I tested the Data-Bag! But I’m not able to use the Information
from the DataBag in Templates.

My recipe is:
GLOBAL = data_bag_item('GLOBAL', 'NTP') and just use a template-function
(create a file from file.erb)
In my file.erb:

<%= node[:GLOBAL][:ntp_2_thalesgroup.at] %>

Irrespective of your problem, already addressed by Charles, take a
look at the Resources wiki page Template section. I believe the
canonical way to reference data in a template, especially when it's
global/data-bag driven and not node-specific, is to use the fact that
it shows up as instance variables inside your template when it's been
passed in correctly. Not node[:blah] values.

I may be wrong, but it WFM :slight_smile:
Jonathan

Jonathan Matthews
London, UK
http://www.jpluscplusm.com/contact.html

Hi Jonathan & Charles,

Thanks for the great hint. I really made a mistake in using the data-bag information directly in a template. Here is the code which will work:

DataBag:
intChef:~/chef-repo$ knife data bag show GLOBAL NTP
{
"id": "NTP",
"ntp_1_for_testing": "xxx.xxx.xxx.xxx",
}

Recipe:
GLOBAL = data_bag_item('GLOBAL','NTP')
node[:ntp1] = "#{GLOBAL['ntp_1_for_testing']}"

Template:
NTP1 for DOMAIN <%= node[:ntp1] %>

Maybe it's not the correct way to do that but it works!

Regards
Andreas
-----Original Message-----
From: jonathan@jpluscplusm.com [mailto:jonathan@jpluscplusm.com] On Behalf Of Jonathan Matthews
Sent: Mittwoch, 23. März 2011 09:22
To: chef@lists.opscode.com
Subject: [chef] Re: Data-Bag how to use in templates

On 23 March 2011 07:21, WEINHAPL Andreas
andreas.weinhapl@thalesgroup.com wrote:

HI,

Yesterday I tested the Data-Bag! But I’m not able to use the Information
from the DataBag in Templates.

My recipe is:
GLOBAL = data_bag_item('GLOBAL', 'NTP') and just use a template-function
(create a file from file.erb)
In my file.erb:

<%= node[:GLOBAL][:ntp_2_thalesgroup.at] %>

Irrespective of your problem, already addressed by Charles, take a
look at the Resources wiki page Template section. I believe the
canonical way to reference data in a template, especially when it's
global/data-bag driven and not node-specific, is to use the fact that
it shows up as instance variables inside your template when it's been
passed in correctly. Not node[:blah] values.

I may be wrong, but it WFM :slight_smile:
Jonathan

Jonathan Matthews
London, UK
http://www.jpluscplusm.com/contact.html

I wrote a tutorial that uses NTP as an example, you may want to take a
peek at it: http://goo.gl/STfRn

-s

On Wed, Mar 23, 2011 at 5:20 AM, WEINHAPL Andreas
andreas.weinhapl@thalesgroup.com wrote:

Hi Jonathan & Charles,

Thanks for the great hint. I really made a mistake in using the data-bag
information directly in a template. Here is the code which will work:

DataBag:
intChef:~/chef-repo$ knife data bag show GLOBAL NTP
{
"id": "NTP",
"ntp_1_for_testing": "xxx.xxx.xxx.xxx",
}

Recipe:
GLOBAL = data_bag_item('GLOBAL','NTP')
node[:ntp1] = "#{GLOBAL['ntp_1_for_testing']}"

Template:
NTP1 for DOMAIN <%= node[:ntp1] %>

Maybe it's not the correct way to do that but it works!

Regards
Andreas
-----Original Message-----
From: jonathan@jpluscplusm.com [mailto:jonathan@jpluscplusm.com] On Behalf
Of Jonathan Matthews
Sent: Mittwoch, 23. März 2011 09:22
To: chef@lists.opscode.com
Subject: [chef] Re: Data-Bag how to use in templates

On 23 March 2011 07:21, WEINHAPL Andreas
andreas.weinhapl@thalesgroup.com wrote:

HI,

Yesterday I tested the Data-Bag! But I’m not able to use the Information
from the DataBag in Templates.

My recipe is:
GLOBAL = data_bag_item('GLOBAL', 'NTP') and just use a template-function
(create a file from file.erb)
In my file.erb:

<%= node[:GLOBAL][:ntp_2_thalesgroup.at] %>

Irrespective of your problem, already addressed by Charles, take a
look at the Resources wiki page Template section. I believe the
canonical way to reference data in a template, especially when it's
global/data-bag driven and not node-specific, is to use the fact that
it shows up as instance variables inside your template when it's been
passed in correctly. Not node[:blah] values.

I may be wrong, but it WFM :slight_smile:
Jonathan

Jonathan Matthews
London, UK
http://www.jpluscplusm.com/contact.html

On Wed, Mar 23, 2011 at 2:20 AM, WEINHAPL Andreas
andreas.weinhapl@thalesgroup.com wrote:

Hi Jonathan & Charles,

Thanks for the great hint. I really made a mistake in using the data-bag
information directly in a template. Here is the code which will work:

DataBag:
intChef:~/chef-repo$ knife data bag show GLOBAL NTP
{
"id": "NTP",
"ntp_1_for_testing": "xxx.xxx.xxx.xxx",
}

Recipe:
GLOBAL = data_bag_item('GLOBAL','NTP')
node[:ntp1] = "#{GLOBAL['ntp_1_for_testing']}"

You don't need to have this as a constant, which is what you're doing
here, most likely. Rather, you might want to do something like set the
value for the NTP server in a role default, and have the value appear
directly on each node.

Conversely, you might want to just change "GLOBAL" to 'global', and
make it a local variable.

Best,
Adam

--
Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com