# Dynamically setting attributes

**URL:** <https://discourse.chef.io/t/dynamically-setting-attributes/5000>\
**Category:** Chef Infra (archive)\
**Created:** [February 10, 2014, 3:00pm UTC](https://discourse.chef.io/t/dynamically-setting-attributes/5000 "2014-02-10T15:00:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jennifer\_Fountain](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jennifer_fountain/32/63_2.png) [@Jennifer\_Fountain](https://discourse.chef.io/u/Jennifer_Fountain)\
**Post date:** [February 10, 2014, 3:00pm UTC](https://discourse.chef.io/t/dynamically-setting-attributes/5000/1 "2014-02-10T15:00:18Z")

</div>

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](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? 😃

Thanks!!!

-J

---

<div class="post-metadata">

**Author:** ![Mike](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/mike/32/18_2.png) [@Mike](https://discourse.chef.io/u/Mike)\
**Post date:** [February 10, 2014, 3:26pm UTC](https://discourse.chef.io/t/dynamically-setting-attributes/5000/2 "2014-02-10T15:26:39Z")

</div>

This sounds like a perfect use case for Chef Environments [1](http://docs.opscode.com/essentials_environments.html).  
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](http://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](mailto: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](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? 😃
> 
> Thanks!!!
> 
> -J

---

<div class="post-metadata">

**Author:** ![Jennifer\_Fountain](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jennifer_fountain/32/63_2.png) [@Jennifer\_Fountain](https://discourse.chef.io/u/Jennifer_Fountain)\
**Post date:** [February 10, 2014, 5:21pm UTC](https://discourse.chef.io/t/dynamically-setting-attributes/5000/3 "2014-02-10T17:21:35Z")

</div>

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](mailto:miketheman@gmail.com) wrote:

> This sounds like a perfect use case for Chef Environments [1](http://docs.opscode.com/essentials_environments.html).  
> 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](http://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](mailto: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](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? 😃
> 
> Thanks!!!
> 
> -J
