RE: Re: Passing variables between recipes?

Yes, I know about encrypted data bags, but it’s code smell. I want the application cookbook to be generic, and not depend on a particular data bag or data bag structure.

Kevin Keane

The NetTech

http://www.4nettech.com

Our values: Privacy, Liberty, Justice

See https://www.4nettech.com/corp/the-nettech-values.html

-----Original message-----
From: klum_tz@ukr.net klum_tz@ukr.net
Sent: Friday 19th December 2014 0:53
To: chef@lists.opscode.com
Cc: chef@lists.opscode.com; chef@lists.opscode.com
Subject: [chef] Re: Passing variables between recipes?

Data bag actually has facility to code password etc.
So it will not be reviled.

And it can be easily accessed from any node. And data is being saved only on Chef server side.

— Оригінальне повідомлення —
Від кого: “Kevin Keane Subscription” subscription@kkeane.com
Дата: 19 грудня 2014, 10:32:46

I’m sure that this must be an FAQ but I can’t really find the answer.

I am working with an application-cookbook/wrapper-cookbook pattern. Usually, in this pattern the wrapper cookbook sets up some attributes, and then calls the application cookbook to do the work.

In my case, I do not want to use attributes to pass the data from the wrapper to the application cookbook. Specifically, I dont’ want the data to be saved in the node (the data in question is the password to a MySQL database).

I also would like to avoid using a data bag simply because it seems like code smell. Only the wrapper cookbook should “know” where the data is coming from.

What are my options?

Kevin Keane

The NetTech

http://www.4nettech.com http://www.4nettech.com

Our values: Privacy, Liberty, Justice

See https://www.4nettech.com/corp/the-nettech-values.html

In that case, you can use an attribute to determine the data bag’s name. As for structure, that’s true, but it’s not unreasonable to encourage a structure resembling:

environment: {
iam_user_name: {
access_id: ‘blah’,
secret_key: ‘xxx’
}
}


Jeff Byrnes
Operations Engineer
EverTrue
@berkleebassist

On December 19, 2014 at 4:21:41 AM, Kevin Keane Subscription (subscription@kkeane.com) wrote:

Yes, I know about encrypted data bags, but it’s code smell. I want the application cookbook to be generic, and not depend on a particular data bag or data bag structure.

Kevin Keane

The NetTech

http://www.4nettech.com

Our values: Privacy, Liberty, Justice

See https://www.4nettech.com/corp/the-nettech-values.html

-----Original message-----
From: klum_tz@ukr.net klum_tz@ukr.net
Sent: Friday 19th December 2014 0:53
To: chef@lists.opscode.com
Cc: chef@lists.opscode.com; chef@lists.opscode.com
Subject: [chef] Re: Passing variables between recipes?

Data bag actually has facility to code password etc.
So it will not be reviled.

And it can be easily accessed from any node. And data is being saved only on Chef server side.

— Оригінальне повідомлення —
Від кого: “Kevin Keane Subscription” subscription@kkeane.com
Дата: 19 грудня 2014, 10:32:46

I’m sure that this must be an FAQ but I can’t really find the answer.

I am working with an application-cookbook/wrapper-cookbook pattern. Usually, in this pattern the wrapper cookbook sets up some attributes, and then calls the application cookbook to do the work.

In my case, I do not want to use attributes to pass the data from the wrapper to the application cookbook. Specifically, I dont’ want the data to be saved in the node (the data in question is the password to a MySQL database).

I also would like to avoid using a data bag simply because it seems like code smell. Only the wrapper cookbook should “know” where the data is coming from.

What are my options?

Kevin Keane

The NetTech

http://www.4nettech.com
Our values: Privacy, Liberty, Justice

See https://www.4nettech.com/corp/the-nettech-values.html

Hi guys,
I am really struggle with getting data bags data into my recipe:
Here is recipe: val1 = data_bag_item(‘db_databag’, ‘tenant_name’) val2 = data_bag_item(‘db_databag’, ‘path’) val3 = data_bag_item(‘db_databag’, ‘db_name’) val4 = data_bag_item(‘db_databag’, ‘any_other_stuff’)
template ‘c:\test_template.txt’ do source ‘test.txt.erb’ variables({ :value1 => “this is FIRST #{val1}”, :value2 => “this is SECOND #{val2}”, :value3 => “this is THIRD #{val3}”, :value4 => “this is FORTH #{val4}” }) end
Here is Data bag: { “id”: “tenant”, “tenant_name”: “db_test1”, “db_name”: “db_name1”, “path”: “path1”, “any_other_stuff”: “some_stuff1” }
Here is Template file: some text will be: <%= @value1 %> some text will be: <%= @value2 %> some text will be: <%= @value3 %> some text will be: <%= @value4 %>
Chef-client version I have on a client side: C:\Users\tklym>chef-client --version
Chef: 11.16.4
Client’s platform is windows 2008 R2.
And here what I get when I run chef-client:

172.26.3.36 [2014-12-19T14:43:47+00:00] INFO: Loading cookbooks [hello@0.1.0] 172.26.3.36 [2014-12-19T14:43:47+00:00] INFO: Storing updated cookbooks/hello/re cipes/templates_with_data_bags.rb in the cache. 172.26.3.36 [2014-12-19T14:43:47+00:00] INFO: HTTP Request Returned 404 Object N ot Found: Cannot load data bag item tenant_name for data bag db_databag 172.26.3.36 [2014-12-19T14:43:47+00:00] ERROR: Failed to load data bag item: “db _databag” “tenant_name” 172.26.3.36 172.26.3.36 ==================================================================== ============ 172.26.3.36 Recipe Compile Error in c:/chef/cache/cookbooks/hello/recipes/templa tes_with_data_bags.rb 172.26.3.36 ==================================================================== ============ 172.26.3.36 172.26.3.36 Net::HTTPServerException 172.26.3.36 ------------------------ 172.26.3.36 404 “Object Not Found” 172.26.3.36 172.26.3.36 Cookbook Trace: 172.26.3.36 --------------- 172.26.3.36 c:/chef/cache/cookbooks/hello/recipes/templates_with_data_bags.rb: 1:in `from_file’ 172.26.3.36 172.26.3.36 Relevant File Content: 172.26.3.36 ---------------------- 172.26.3.36 c:/chef/cache/cookbooks/hello/recipes/templates_with_data_bags.rb: 172.26.3.36 172.26.3.36 1>> val1 = data_bag_item(‘db_databag’, ‘tenant_name’) 172.26.3.36 2: val2 = data_bag_item(‘db_databag’, ‘path’) 172.26.3.36 3: val3 = data_bag_item(‘db_databag’, ‘db_name’) 172.26.3.36 4: val4 = data_bag_item(‘db_databag’, ‘any_other_stuff’) 172.26.3.36 5: 172.26.3.36 6: template ‘c:\test_template.txt’ do 172.26.3.36 7: source ‘test.txt.erb’ 172.26.3.36 8: variables({ 172.26.3.36 9: :value1 => “this is FIRST #{val1}”, 172.26.3.36 10: :value2 => “this is SECOND #{val2}”, 172.26.3.36 172.26.3.36 [2014-12-19T14:43:47+00:00] ERROR: Running exception handlers 172.26.3.36 [2014-12-19T14:43:47+00:00] ERROR: Exception handlers complete 172.26.3.36 [2014-12-19T14:43:47+00:00] FATAL: Stacktrace dumped to c:/chef/cach e/chef-stacktrace.out 172.26.3.36 [2014-12-19T14:43:47+00:00] INFO: Sending resource update report (ru n-id: 7a567034-e647-4681-a2e5-494e04dbff81) 172.26.3.36 [2014-12-19T14:43:47+00:00] FATAL: Net::HTTPServerException: 404 “Ob ject Not Found” ERROR: Failed to execute command on 172.26.3.36 return code 1

Have no idea how to get access to data bag data… :frowning:
Thank you in advance for your help.
Regards, Taras.

The confusion here is that the data bag item is what you refer as the data bag. In your case, the data bag is db_databag, and the data bag item is tenant.

That is, this is the data bag item:

{
“id”: “tenant”,
“tenant_name”: “db_test1”,
“db_name”: “db_name1”,
“path”: “path1”,
“any_other_stuff”: “some_stuff1”
}

So you want to query for it like so:

tenant_data_bag = data_bag_item ‘db_databag’, ‘tenant’
tenant_name = tenant_data_bag[‘tenant_name’]
path = tenant_data_bag[‘path’]

Does that make more sense?


Jeff Byrnes
@berkleebassist
Lead DevOps Engineer
EverTrue
704.516.4628

On December 19, 2014 at 9:49:16 AM, klum_tz@ukr.net (klum_tz@ukr.net) wrote:

Hi guys,

I am really struggle with getting data bags data into my recipe:

Here is recipe:
val1 = data_bag_item(‘db_databag’, ‘tenant_name’)
val2 = data_bag_item(‘db_databag’, ‘path’)
val3 = data_bag_item(‘db_databag’, ‘db_name’)
val4 = data_bag_item(‘db_databag’, ‘any_other_stuff’)

template ‘c:\test_template.txt’ do
source 'test.txt.erb’
variables({
:value1 => “this is FIRST #{val1}”,
:value2 => “this is SECOND #{val2}”,
:value3 => “this is THIRD #{val3}”,
:value4 => “this is FORTH #{val4}”
})
end

Here is Data bag:
{
“id”: “tenant”,
“tenant_name”: “db_test1”,
“db_name”: “db_name1”,
“path”: “path1”,
“any_other_stuff”: “some_stuff1”
}

Here is Template file:
some text will be: <%= @value1 %>
some text will be: <%= @value2 %>
some text will be: <%= @value3 %>
some text will be: <%= @value4 %>

Chef-client version I have on a client side:
C:\Users\tklym>chef-client --version
Chef: 11.16.4

Client’s platform is windows 2008 R2.

And here what I get when I run chef-client:

172.26.3.36 [2014-12-19T14:43:47+00:00] INFO: Loading cookbooks [hello@0.1.0]
172.26.3.36 [2014-12-19T14:43:47+00:00] INFO: Storing updated cookbooks/hello/recipes/templates_with_data_bags.rb in the cache.
172.26.3.36 [2014-12-19T14:43:47+00:00] INFO: HTTP Request Returned 404 Object Not Found: Cannot load data bag item tenant_name for data bag db_databag
172.26.3.36 [2014-12-19T14:43:47+00:00] ERROR: Failed to load data bag item: “db_databag” "tenant_name"
172.26.3.36
172.26.3.36 ==================================================================== ============
172.26.3.36 Recipe Compile Error in c:/chef/cache/cookbooks/hello/recipes/templates_with_data_bags.rb
172.26.3.36 ================================================================================
172.26.3.36
172.26.3.36 Net::HTTPServerException
172.26.3.36 ------------------------
172.26.3.36 404 "Object Not Found"
172.26.3.36
172.26.3.36 Cookbook Trace:
172.26.3.36 ---------------
172.26.3.36 c:/chef/cache/cookbooks/hello/recipes/templates_with_data_bags.rb:1:in `from_file’
172.26.3.36
172.26.3.36 Relevant File Content:
172.26.3.36 ----------------------
172.26.3.36 c:/chef/cache/cookbooks/hello/recipes/templates_with_data_bags.rb:
172.26.3.36
172.26.3.36 1>> val1 = data_bag_item(‘db_databag’, ‘tenant_name’)
172.26.3.36 2: val2 = data_bag_item(‘db_databag’, ‘path’)
172.26.3.36 3: val3 = data_bag_item(‘db_databag’, ‘db_name’)
172.26.3.36 4: val4 = data_bag_item(‘db_databag’, ‘any_other_stuff’)
172.26.3.36 5:
172.26.3.36 6: template ‘c:\test_template.txt’ do
172.26.3.36 7: source 'test.txt.erb’
172.26.3.36 8: variables({
172.26.3.36 9: :value1 => “this is FIRST #{val1}”,
172.26.3.36 10: :value2 => “this is SECOND #{val2}”,
172.26.3.36
172.26.3.36 [2014-12-19T14:43:47+00:00] ERROR: Running exception handlers
172.26.3.36 [2014-12-19T14:43:47+00:00] ERROR: Exception handlers complete
172.26.3.36 [2014-12-19T14:43:47+00:00] FATAL: Stacktrace dumped to c:/chef/cache/chef-stacktrace.out
172.26.3.36 [2014-12-19T14:43:47+00:00] INFO: Sending resource update report (run-id: 7a567034-e647-4681-a2e5-494e04dbff81)
172.26.3.36 [2014-12-19T14:43:47+00:00] FATAL: Net::HTTPServerException: 404 "Object Not Found"ERROR: Failed to execute command on 172.26.3.36 return code 1

Have no idea how to get access to data bag data… :frowning:

Thank you in advance for your help.

Regards,
Taras.