title: How to set a variable in a chef template?
body: <p>In the official document, there is only one use case of <code>variable</code> for template: the caller has to pass in a hash.</p>
  
  <p>However for me I have a very simple use case. I only want to set a server name in a sensu client configuration template file <code>client.json.erb</code></p>
  
  <p>Here is the template file:</p>
  
  <pre><code>{
    "client": {
      "name": "&lt;%= @server_name %&gt;",
      "address": "&lt;%= node.ipaddress %&gt;",
      "keepalive": {
        "thresholds": {
          "warning": 90,
          "critical": 180
        }
      },
      "subscriptions": [ "default" ]
    }
  }
  </code></pre>
  
  <p>It is my chef code:</p>
  
  <pre><code>server_name = "server1.example.com"
  
  template "/etc/sensu/conf.d/client.json" do
    variables({
      :server_name =&gt; server_name
    })
    source "sensu-template/conf.d/client.json.erb"
  end
  </code></pre>
  
  <p>The config file turns out to become:</p>
  
  <pre><code>{
    "client": {
      "name": "{}",
      "address": "10.0.1.1",
      "keepalive": {
        "thresholds": {
          "warning": 90,
          "critical": 180
        }
      },
      "subscriptions": [ "default" ]
    }
  }
  </code></pre>
  
  <p>How should I pass a variable name properly into the template?</p>