Dynamically setting attributes


#1

Ohai!

I am trying to create a generic cookbook that will deploy different applications. Everything about these app installations are the same save the installation URL. What I want to do may not even be possible or I should do it another way. I am hoping someone here could give me some suggestions.

Each of these applications has a testing, staging and production environment. As a result, three different installation URLs.

Each application name is set using this attribute in the role:
default[:application][:name] = nil

And I want to set the install URL in the environment and reference it like this or however I need to reference it (this is where I am not sure :D):

default#{node[:application][:name]}[:install][:url] = nil

For example:

default[:application][:name] = ‘myapp’ in the “myapp” role

default[:myapp][:install][:url] = http://someurl/myapp.git <<— where :myapp is pulled from the above attribute.

I thought about databags, creating different Roles - "Myapp - Testing role” but I really was hoping to pull it from the name.

Any ideas would be appreciated. Can I even do this? Should I? :smiley:

Thanks!!!

-J


#2

This sounds like a perfect use case for Chef Environments 1.
In this scenario, you might have an environment file named 'testing.rb’
which would contain an attribute defining the environment-specific
modifications.
Thereby, associating a node to a particular environment would switch the
associated attribute.

Here’s a loose example, based on your variables:

cookbook:

default[:application][:name] = nil
default[:application][:url] = nil

role myapp.rb:

default[:application][:name] = ‘myapp’

environment testing.rb:

default_attributes(
“application” => {
“url” => “testingurl.com
}
)

cookbook recipe:

default[:myapp][:install][:url] = “http://#{node[:application
][:url]}/#{node[:application][:name]}.git”

Hope this helps,
-M

On Mon, Feb 10, 2014 at 10:00 AM, Jenn Fountain jfountain@meetme.comwrote:

Ohai!

I am trying to create a generic cookbook that will deploy different
applications. Everything about these app installations are the same save
the installation URL. What I want to do may not even be possible or I
should do it another way. I am hoping someone here could give me some
suggestions.

Each of these applications has a testing, staging and production
environment. As a result, three different installation URLs.

Each application name is set using this attribute in the role:
default[:application][:name] = nil

And I want to set the install URL in the environment and reference it like
this or however I need to reference it (this is where I am not sure :D):

default#{node[:application][:name]}[:install][:url] = nil

For example:

default[:application][:name] = ‘myapp’ in the “myapp” role

default[:myapp][:install][:url] = http://someurl/myapp.git <<-- where
:myapp is pulled from the above attribute.

I thought about databags, creating different Roles - “Myapp - Testing
role” but I really was hoping to pull it from the name.

Any ideas would be appreciated. Can I even do this? Should I? :smiley:

Thanks!!!

-J


#3

Thanks. I did try that but it didn’t work exactly like I wanted.

I tried this piece of code in default.rb:

application = node[:application][:name]
default[application][:install][:url] = nil

It seems to work as expected. Thank you!!

On Feb 10, 2014, at 10:26 AM, Mike miketheman@gmail.com wrote:

This sounds like a perfect use case for Chef Environments 1.
In this scenario, you might have an environment file named ‘testing.rb’ which would contain an attribute defining the environment-specific modifications.
Thereby, associating a node to a particular environment would switch the associated attribute.

Here’s a loose example, based on your variables:

cookbook:

default[:application][:name] = nil
default[:application][:url] = nil

role myapp.rb:

default[:application][:name] = ‘myapp’

environment testing.rb:

default_attributes(
“application” => {
“url” => “testingurl.com
}
)

cookbook recipe:

default[:myapp][:install][:url] = “http://#{node[:application][:url]}/#{node[:application][:name]}.git”

Hope this helps,
-M

On Mon, Feb 10, 2014 at 10:00 AM, Jenn Fountain jfountain@meetme.com wrote:
Ohai!

I am trying to create a generic cookbook that will deploy different applications. Everything about these app installations are the same save the installation URL. What I want to do may not even be possible or I should do it another way. I am hoping someone here could give me some suggestions.

Each of these applications has a testing, staging and production environment. As a result, three different installation URLs.

Each application name is set using this attribute in the role:
default[:application][:name] = nil

And I want to set the install URL in the environment and reference it like this or however I need to reference it (this is where I am not sure :D):

default#{node[:application][:name]}[:install][:url] = nil

For example:

default[:application][:name] = ‘myapp’ in the “myapp” role

default[:myapp][:install][:url] = http://someurl/myapp.git <<— where :myapp is pulled from the above attribute.

I thought about databags, creating different Roles - "Myapp - Testing role” but I really was hoping to pull it from the name.

Any ideas would be appreciated. Can I even do this? Should I? :smiley:

Thanks!!!

-J