使用Chef来管理具有特定于实例的内容的文件?

时间:2016-08-25 11:07:57

标签: chef chef-recipe

我使用Chef配方来管理在Amazon EC2实例上运行的一些Tomcat服务器。在我需要使用大量相同的Tomcats并且具有相同的.jars / Java选项/等的场景中,它非常适合我。

但是,我遇到了一个我不确定如何处理的用例。基本上,每个Tomcat都有一个context.xml(位于/ etc / tomcat7)文件,我希望将其配置为具有需要指向特定端点的Manager元素(Amazon中与其名称对应的Elasticache,所以TomcatA有ElasticacheA等等)。最初,我在配方中使用关联的context.xml模板获得了以下代码:

elasticache="TomcatA"
elasticache_suffix = ".xxx.cfg.use1.cache.amazonaws.com"

template 'tomcat context.xml configuration' do
 path ::File.join(tomcat_directory, 'context.xml')
 source 'context.xml.erb'
 variables(
{
            :elasticache => elasticache,
            :elasticache_suffix => elasticache_suffix
}
          )
 owner 'root'
 group node["tomcat"]["group"]
 mode 00644
 notifies :restart, 'service[tomcat7]', :delayed
 end

context.xml的模板:

<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version   2.0
(the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or    implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -->
<!-- The contents of this file will be loaded for each web application ->
<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<Manager
className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="<%=@elasticache%><%=@elasticache_suffix%>:11211"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
sessionBackupAsync="true"
sticky="true"
copyCollectionsForSerialization="false"
/>

<!-- Uncomment this to enable Comet connection tacking (provides events
     on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->

</Context>

然而,这显然不适用于我的目的,因为Chef将确保所有现有设置都使用ElasticacheA。

我可以使用AWS CLI获取我需要的Elasticache名称值,但是如何确保Chef每次都不会使用该值覆盖现有设置?理想情况下,每次运行配方都会为此模板生成一个新结果,该结果不会影响已在使用的任何设置。

1 个答案:

答案 0 :(得分:0)

这里你的选择很少。所有都来自节点属性,而不是硬编码的elasticache变量,您应该使用节点属性,例如node["tomcat"]["elasticache"]。您可以通过两种方式将其调整为每个部署。

  1. 为每种类型的Tomcat部署创建environment,为不同的部署类型分配不同的环境并在那里覆盖属性。
  2. 创建&#39;包装&#39; cookbook / recipe,它将设置/覆盖节点属性并包含进行Tomcat设置的基本配方。