Chef cookbook - how to fetch hostname dynamically in the attribute file

123 views Asked by At

I need to create 10 tomcat instances on 5 different servers (2 instances on each server). The instances names are common across the 5 servers.

The Tomcat instances would need to be integrated with a monitoring tool AppD. Now, while integrating it needs a parameter to be added in the attribute file which is like mentioned below:

default['mw']['tomcat'][application_identifier]['java_opts'] = '$JAVA_HEAP $JAVA_PROPS_SYSTYPE -Dappd.agent.nodeName=<HostName>-instance_tc_primary

The requirement here is to not hardcode the ; whereas get it placed dynamically based on the host on which it is to be deployed.

E.g.

For host LDN001 it shall appear as:

default['mw']['tomcat'][application_identifier]['java_opts'] = '$JAVA_HEAP $JAVA_PROPS_SYSTYPE -Dappd.agent.nodeName=LDN001-instance_tc_primary

For host LDN002 it shall appear as -

default['mw']['tomcat'][application_identifier]['java_opts'] = '$JAVA_HEAP $JAVA_PROPS_SYSTYPE -Dappd.agent.nodeName=LDN002-instance_tc_primary

So how can I have the attribute file fetching the hostnames dynamically based on the host it will get deployed?

We tried changing the role file as given below thinking that it will override java_opts:

node.override['mw']['tomcat'][application_identifier]['java_opts'] =
  "$JAVA_HEAP $JAVA_PROPS_SYSTYPE -Djava.awt.headless=true $JAVA_SYSTYPE_OPTS -Dappdynamics.agent.nodeName=#{host[hostname]}-instance_tc_primary

But that did not work.

2

There are 2 answers

0
Tejas On

This is done by overwriting the Receipe file as below:

node.override['mw']['tomcat'][application_identifier]['java_opts'] =
  '$JAVA_HEAP $JAVA_PROPS_SYSTYPE -Djava.awt.headless=true $JAVA_SYSTYPE_OPTS -Dappdynamics.agent.nodeName=$(hostname -s)-instance_tc_prim

So using $(hostname -s) resolved the issue.

0
seshadri_c On

Chef Ohai collects comprehensive information about the system on which chef-client runs. We can use this to dynamically get the host information and replace required values.

For example, the hostname of the node is available in node['hostname'] attribute.

So, a simple attribute definition such as below should be enough:

default['mw']['tomcat'][application_identifier]['java_opts'] = "$JAVA_HEAP $JAVA_PROPS_SYSTYPE -Dappd.agent.nodeName=#{node['hostname']}-instance_tc_primary"