Can chef parse the attribute list and handle it while creating templates

Hi All,

I have an attribute which I must pass as list and update it in the template
file But it seems the attributes are taken as literal string during chef-run.
Is there a way to pass a list and handle it in templates ?

default[‘zabbix’][‘agent’][‘servers_active’] = “hostname”

default[‘zabbix’][‘agent’][‘servers_active’] = “[ “hostname1”, “hostname2” ]”

Template where this list need to
ServerActive=<%= node.zabbix.agent.server_active %>
Desired Output
ServerActive=hostname1,hostname2

thanks for your response in advance!
Tarun

definitely you can do that. can you share the recipe where you are passing
the variables in the template / defining the templates.

On Tue, May 14, 2013 at 9:19 AM, tagrawal@adobe.com wrote:

Hi All,

I have an attribute which I must pass as list and update it in the template
file But it seems the attributes are taken as literal string during
chef-run.
Is there a way to pass a list and handle it in templates ?

default['zabbix']['agent']['servers_active'] = "hostname"

default['zabbix']['agent']['servers_active'] = "[ "hostname1", "hostname2"
]"

Template where this list need to
ServerActive=<%= node.zabbix.agent.server_active %>
Desired Output
ServerActive=hostname1,hostname2

thanks for your response in advance!
Tarun

We currently do this with a set of attributes that are contained in a
Role definition.

By leveraging the rsyslog cookbook, we template the filenames to be watched.

{ "file" : "/opt/zenoss/log/#{node.name}/zeneventd.log" }

filepath=logfile['file'].sub('#{node.name}', node.name)

Might not be the cleanest, but it scales, and we re-used the ruby/dsl
template format to make it recognizable on-site in the role files.

  • alex

On 05/14/2013 11:19 AM, tagrawal@adobe.com wrote:

Hi All,

I have an attribute which I must pass as list and update it in the template
file But it seems the attributes are taken as literal string during chef-run.
Is there a way to pass a list and handle it in templates ?

default['zabbix']['agent']['servers_active'] = "hostname"

default['zabbix']['agent']['servers_active'] = "[ "hostname1", "hostname2" ]"

Template where this list need to
ServerActive=<%= node.zabbix.agent.server_active %>
Desired Output
ServerActive=hostname1,hostname2

thanks for your response in advance!
Tarun

--
Alex Corley | Software as a Service Engineer
Zenoss, Inc. | Transforming IT Operations
acorley@zenoss.com | Skype: acorley_zenoss | Github: anthroprose

If your attribute value is actually a list:

default['zabbix']['agent']['servers_active'] = [ "hostname1", "hostname2" ]

Then in your template, you could just render it's elements separated by a comma:

ServerActive=<%= node.zabbix.agent.servers_active.join(",") %>

If you want to have it both ways - perhaps you have some legacy roles that
still insist on single values and not lists:

just a single value, not passed as a list

default['zabbix']['agent']['servers_active'] = "hostname"

several values passed as a list

default['zabbix']['agent']['servers_active'] = [ "hostname1", "hostname2" ]

Then in your template, to treat whatever is passed as a list:

ServerActive=<%= [node.zabbix.agent.servers_active].join(",") %>

HTH.

On 05/15/2013 01:19 AM, tagrawal@adobe.com wrote:

Hi All,

I have an attribute which I must pass as list and update it in the template
file But it seems the attributes are taken as literal string during chef-run.
Is there a way to pass a list and handle it in templates ?

default['zabbix']['agent']['servers_active'] = "hostname"

default['zabbix']['agent']['servers_active'] = "[ "hostname1", "hostname2" ]"

Template where this list need to
ServerActive=<%= node.zabbix.agent.server_active %>
Desired Output
ServerActive=hostname1,hostname2

thanks for your response in advance!
Tarun

Thanks guyz…I will try the solutions provided & update on the thread!!

Tarun

From: Ranjib Dey [mailto:dey.ranjib@gmail.com]
Sent: 14 May 2013 21:54
To: chef@lists.opscode.com
Subject: [chef] Re: Can chef parse the attribute list and handle it while creating templates

definitely you can do that. can you share the recipe where you are passing the variables in the template / defining the templates.

On Tue, May 14, 2013 at 9:19 AM, <tagrawal@adobe.commailto:tagrawal@adobe.com> wrote:
Hi All,

I have an attribute which I must pass as list and update it in the template
file But it seems the attributes are taken as literal string during chef-run.
Is there a way to pass a list and handle it in templates ?

default[‘zabbix’][‘agent’][‘servers_active’] = “hostname”

default[‘zabbix’][‘agent’][‘servers_active’] = “[ “hostname1”, “hostname2” ]”

Template where this list need to
ServerActive=<%= node.zabbix.agent.server_active %>
Desired Output
ServerActive=hostname1,hostname2

thanks for your response in advance!
Tarun