首先,我正在使用Chef Solo,我希望事情尽可能保持自动化。 所以我有一个问题,我不太清楚如何解决。 我正在设置很多不同的Linux机器的部署,并且他们都将要求自定义安装节点/angular色。 例:
/nodes/app_server.json
{ "run_list": [ "role[app_server]" ] }
/roles/app_server.rb
name 'app_server' description 'App_Server' override_attributes({ "apache" => { "proxypass" => [" / http://localhost:8080/"] } }) run_list 'recipe[app_server]'
我的问题是,我将运行一个脚本来安装厨师在所有这些不同的盒子,但每一个将有一个不同的IP之间http:// [xxxxxx]:8080 /
我需要一种方法来通过命令行来指定这些IP,而不必像创build一百个节点或angular色文件一样。
我看了他们的网站上的一个例子,显示:
Web服务器angular色
description "The base role for systems that serve HTTP traffic" run_list "recipe[apache2]", "recipe[apache2::mod_ssl]", "role[monitor]" env_run_lists "prod" => ["recipe[apache2]"], "staging" => ["recipe[apache2::staging]"],"_default" => [] default_attributes "apache2" => { "listen_ports" => [ "80", "443" ] } override_attributes "apache2" => { "max_children" => "50" }
这似乎是有用的,但我想为每个env_run_listconfiguration设置不同的覆盖集,然后当我运行所有的厨师命令,能够针对每一个我想要的。
我是否以错误的方式去解决这个问题? 我已经搜遍了所有关于节点/angular色/环境的文档,还没有find任何需要我不必制作十几个不同文件的解决scheme。
如果不访问您的app_server配方,我推断您正在使用node['apache']['proxypass']属性来填充从模板生成的apacheconfiguration文件中的一行。
我的猜测是你的模板中的相关行看起来像这样:
ProxyPass <%= node['apache']['proxypass'] %>
我认为如果您使用更多的粒度元素来组合模板中的那一行,您将会获得更大的成功。 如果您希望代理请求的IP地址在每个环境中都不相同,则可以使用Chef的search工具来确定所需的IP地址,并将该IP地址作为variables传递给模板。
你的配方中的代码可能是这样的:
# this search assumes that the role 'myapp_backend' is in the run list of the backend server you need to discover. # the search returns an array of node objects, for simplicity we'll just take the first result. proxypass_backend = search(:node, "roles:myapp_backend AND chef_environment:#{node.chef_environment}").first template ::File.join(node['apache']['dir'],'sites-available','myapp.conf') do variables({ :proxypass_local_path => '/', :proxypass_remote_ip => proxypass_backend['ipaddress'], :proxypass_remote_port => '8080', :proxypass_remote_path => '/' }) source 'myapp.conf.erb' end
而你的模板中的行看起来像这样:
ProxyPass <%= @proxypass_local_path %> http://<%= @proxypass_remote_ip %>:<%= proxypass_remote_port %><%= @proxypass_remote_path %>
为了简单起见,我为远程端口和path指定了静态值,但也可以使用可在angular色或环境级别上覆盖的属性值来设置。 因为search返回整个节点对象,search返回的节点的任何属性(例如服务运行的端口)也可以用来填充模板variables(例如, proxypass_remote_port )。
您可以为每个只包含覆盖的服务器创build一个json文件,并且可以使用-j选项将该文件传递给chef-solo: chef-solo -j node.json
你可以在厨师跑之前创build这些文件。 请注意,inputredirect还没有实现: https : //tickets.opscode.com/browse/CHEF-1918,所以你不能只有一个bash命令。
但我第二@cwjohnston你应该拆分该string,并将IP,端口和协议分为不同的节点属性。 你可以用Ohai(node ['ipaddress'])或者从Rubyfind当前节点的IP,并且为最后2个设置合理的默认值。