Chef creating a comma separated string from template and knife search

拜拜、爱过 提交于 2019-12-23 22:54:47

问题


I'm trying to create a dynamically created comma separated list of Splunk indexers (with the data port appended to each host name) from a knife query that supplies the returned list of hosts to a template and corresponding erb.

However the string in the conf file never gets created although no errors are thrown when I converge on my test Docker instance.

Here's the knife query, the attribute it's referencing, and the template resource

attribute file entry:
default['forwarder']['indexer_role'] = 'splunk_indexer'


knife query and template resource:
indexers = search(:node, "role:#{node['forwarder']['indexer_role']}")

template '/opt/splunkforwarder/etc/system/local/outputs.conf' do
  source "system_local_outputs.erb"
  owner 'nobody'
  group 'nobody'
  mode 0600
  action :create
  variables(
    :indexers  => indexers
  )
end

And this is the corresponding erb file for that template resource

[tcpout]
defaultGroup = default-autolb-group

[tcpout:default-autolb-group]
disabled = false
<% @indexers.each do |member| %>
  server = <%= member[ :hostname]  + ":5501," %>
<% end %>

I don't know if the query is specified incorrectly, or whether the template resource is wrong or if I've made a mistake in the erb code.

As I mentioned above there are no errors thrown, the code compiles and converges without any error messages. The line is simply not there.


回答1:


That wouldn't create a comma-separated list, it would make multiple lines like:

server = foo:5501,
server = bar:5501,

What you probably want in the template is more like this:

server = <%= @indexers.map {|n| "#{n}:5501" }.join(",") %>

However if you don't see those lines in the first place, it is likely your search query isn't working. Try checking it with knife search and probably use roles: instead of role: as it is more often correct.



来源:https://stackoverflow.com/questions/44855196/chef-creating-a-comma-separated-string-from-template-and-knife-search

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!