Conditional in recipe to evaluate node name not working (as expected)


#1

Hi,
I’m trying to modify the community memcached recipe such that it will
source one template file if the node name starts with qa, and another if
the node name is anything else. But no matter what I try, the recipe seems
to ignore the conditional and just goes with the “else” block. So the
result is that my nodes that are named qa* are getting my production
template sourced. I even tried just setting the node name conditional to
the exact name of a specific node, and still that node ends up with the
default template, not the QA one specified in the conditional. Here’s the
conditional:

case node[“name”]
when "qa*"
template “/opt/zookeeper/conf/zk.conf” do
source "zk.qa.conf.erb"
owner "root"
group "root"
mode "0644"
end
else
template “/opt/zookeeper/conf/zk.conf” do
source "zk.prod.conf.erb"
owner "root"
group "root"
mode "0644"
end
end

I also have these nodes in a separate chef environment called “qa”, but I’m
not having any luck keying off that either. Any ideas? Here’s the output of
a ‘knife node show’ for the node in question:

knife node show -Fj qa-memcached01-1-i-91e11be3
{
“name”: “qa-memcached01-1-i-91e11be3”,
“chef_environment”: “qa”,
“run_list”: [
“role[memcached]”
],
“normal”: {
“memcached”: {
“memory”: “384”
},
“tags”: [

]

}
}

Thanks, Jeremy


Jeremy Koerber
Director - Tech Ops
branchout.com/jeremy.koerber


#2

case /^qa/
?

On Thu, Mar 14, 2013 at 11:14 AM, Jeremy Koerber jkoerber@branchout.comwrote:

Hi,
I’m trying to modify the community memcached recipe such that it will
source one template file if the node name starts with qa, and another if
the node name is anything else. But no matter what I try, the recipe seems
to ignore the conditional and just goes with the “else” block. So the
result is that my nodes that are named qa* are getting my production
template sourced. I even tried just setting the node name conditional to
the exact name of a specific node, and still that node ends up with the
default template, not the QA one specified in the conditional. Here’s the
conditional:

case node[“name”]
when "qa*"
template “/opt/zookeeper/conf/zk.conf” do
source "zk.qa.conf.erb"
owner "root"
group "root"
mode "0644"
end
else
template “/opt/zookeeper/conf/zk.conf” do
source "zk.prod.conf.erb"
owner "root"
group "root"
mode "0644"
end
end

I also have these nodes in a separate chef environment called “qa”, but
I’m not having any luck keying off that either. Any ideas? Here’s the
output of a ‘knife node show’ for the node in question:

knife node show -Fj qa-memcached01-1-i-91e11be3
{
“name”: “qa-memcached01-1-i-91e11be3”,
“chef_environment”: “qa”,
“run_list”: [
“role[memcached]”
],
“normal”: {
“memcached”: {
“memory”: “384”
},
“tags”: [

]

}
}

Thanks, Jeremy


Jeremy Koerber
Director - Tech Ops
branchout.com/jeremy.koerber


#3

A quick check in shef (version 10.24) tells me that node[“name”] returns
nil, but node.name gives the expected result:

Ohai2u adam@mynode
chef > node[‘name’]
=> nil
chef > node.name
=> “mynode”


Adam Mielke
System Engineer
University of Minnesota

On Thu, Mar 14, 2013 at 1:14 PM, Jeremy Koerber jkoerber@branchout.comwrote:

Hi,
I’m trying to modify the community memcached recipe such that it will
source one template file if the node name starts with qa, and another if
the node name is anything else. But no matter what I try, the recipe seems
to ignore the conditional and just goes with the “else” block. So the
result is that my nodes that are named qa* are getting my production
template sourced. I even tried just setting the node name conditional to
the exact name of a specific node, and still that node ends up with the
default template, not the QA one specified in the conditional. Here’s the
conditional:

case node[“name”]
when "qa*"
template “/opt/zookeeper/conf/zk.conf” do
source "zk.qa.conf.erb"
owner "root"
group "root"
mode "0644"
end
else
template “/opt/zookeeper/conf/zk.conf” do
source "zk.prod.conf.erb"
owner "root"
group "root"
mode "0644"
end
end

I also have these nodes in a separate chef environment called “qa”, but
I’m not having any luck keying off that either. Any ideas? Here’s the
output of a ‘knife node show’ for the node in question:

knife node show -Fj qa-memcached01-1-i-91e11be3
{
“name”: “qa-memcached01-1-i-91e11be3”,
“chef_environment”: “qa”,
“run_list”: [
“role[memcached]”
],
“normal”: {
“memcached”: {
“memory”: “384”
},
“tags”: [

]

}
}

Thanks, Jeremy


Jeremy Koerber
Director - Tech Ops
branchout.com/jeremy.koerber


#4

That did the trick. Thanks!

Working block is:

case node.name
when /^qa/
template “/opt/zookeeper/conf/zk.conf” do
source "zk.qa.conf.erb"
owner "root"
group "root"
mode "0644"
end
else
template “/opt/zookeeper/conf/zk.conf” do
source "zk.prod.conf.erb"
owner "root"
group "root"
mode "0644"
end
end

On Thu, Mar 14, 2013 at 11:31 AM, Adam Mielke adam@umn.edu wrote:

A quick check in shef (version 10.24) tells me that node[“name”] returns
nil, but node.name gives the expected result:

Ohai2u adam@mynode
chef > node[‘name’]
=> nil
chef > node.name
=> “mynode”


Adam Mielke
System Engineer
University of Minnesota

On Thu, Mar 14, 2013 at 1:14 PM, Jeremy Koerber jkoerber@branchout.comwrote:

Hi,
I’m trying to modify the community memcached recipe such that it will
source one template file if the node name starts with qa, and another if
the node name is anything else. But no matter what I try, the recipe seems
to ignore the conditional and just goes with the “else” block. So the
result is that my nodes that are named qa* are getting my production
template sourced. I even tried just setting the node name conditional to
the exact name of a specific node, and still that node ends up with the
default template, not the QA one specified in the conditional. Here’s the
conditional:

case node[“name”]
when "qa*"
template “/opt/zookeeper/conf/zk.conf” do
source "zk.qa.conf.erb"
owner "root"
group "root"
mode "0644"
end
else
template “/opt/zookeeper/conf/zk.conf” do
source "zk.prod.conf.erb"
owner "root"
group "root"
mode "0644"
end
end

I also have these nodes in a separate chef environment called “qa”, but
I’m not having any luck keying off that either. Any ideas? Here’s the
output of a ‘knife node show’ for the node in question:

knife node show -Fj qa-memcached01-1-i-91e11be3
{
“name”: “qa-memcached01-1-i-91e11be3”,
“chef_environment”: “qa”,
“run_list”: [
“role[memcached]”
],
“normal”: {
“memcached”: {
“memory”: “384”
},
“tags”: [

]

}
}

Thanks, Jeremy


Jeremy Koerber
Director - Tech Ops
branchout.com/jeremy.koerber


Jeremy Koerber
Director - Tech Ops
branchout.com/jeremy.koerber


#5

Please don’t do this!

Put the environment into an attribute (it may already be in one:
bonus) and simplify the whole section:

node.default['zookeeper']['environment'] = "dev"
env = node['zookeeper']['environment'] # shorten the interpolated lvar

template "/opt/zookeeper/conf/zk.conf" do
  source "zk.#{env}.conf.erb"
  owner "root"
  group "root"
  mode "0644"
end

Cheers,

AJ

On 15 March 2013 07:59, Jeremy Koerber jkoerber@branchout.com wrote:

That did the trick. Thanks!

Working block is:

case node.name
when /^qa/

template “/opt/zookeeper/conf/zk.conf” do
source "zk.qa.conf.erb"
owner "root"
group "root"
mode "0644"
end
else
template “/opt/zookeeper/conf/zk.conf” do
source "zk.prod.conf.erb"
owner "root"
group "root"
mode "0644"
end
end

On Thu, Mar 14, 2013 at 11:31 AM, Adam Mielke adam@umn.edu wrote:

A quick check in shef (version 10.24) tells me that node[“name”] returns
nil, but node.name gives the expected result:

Ohai2u adam@mynode
chef > node[‘name’]
=> nil
chef > node.name
=> “mynode”


Adam Mielke
System Engineer
University of Minnesota

On Thu, Mar 14, 2013 at 1:14 PM, Jeremy Koerber jkoerber@branchout.com
wrote:

Hi,
I’m trying to modify the community memcached recipe such that it will
source one template file if the node name starts with qa, and another if the
node name is anything else. But no matter what I try, the recipe seems to
ignore the conditional and just goes with the “else” block. So the result is
that my nodes that are named qa* are getting my production template sourced.
I even tried just setting the node name conditional to the exact name of a
specific node, and still that node ends up with the default template, not
the QA one specified in the conditional. Here’s the conditional:

case node[“name”]
when "qa*"
template “/opt/zookeeper/conf/zk.conf” do
source "zk.qa.conf.erb"
owner "root"
group "root"
mode "0644"
end
else
template “/opt/zookeeper/conf/zk.conf” do
source "zk.prod.conf.erb"
owner "root"
group "root"
mode "0644"
end
end

I also have these nodes in a separate chef environment called “qa”, but
I’m not having any luck keying off that either. Any ideas? Here’s the output
of a ‘knife node show’ for the node in question:

knife node show -Fj qa-memcached01-1-i-91e11be3
{
“name”: “qa-memcached01-1-i-91e11be3”,
“chef_environment”: “qa”,
“run_list”: [
“role[memcached]”
],
“normal”: {
“memcached”: {
“memory”: “384”
},
“tags”: [

]

}
}

Thanks, Jeremy


Jeremy Koerber
Director - Tech Ops
branchout.com/jeremy.koerber


Jeremy Koerber
Director - Tech Ops
branchout.com/jeremy.koerber