【问题标题】:How to preserve existing host file when running chef template resource运行厨师模板资源时如何保留现有主机文件
【发布时间】:2018-11-28 02:17:42
【问题描述】:

我正在尝试使用 Chef 模板资源将一组新的 FQDN 和 IP 添加到现有的 Windows 主机文件中。我发现当我运行厨师食谱时,它会删除现有的主机文件,然后添加新的更新。我只想将新资源附加到文件末尾,我在这里使用action :touch 做错了吗?

模板文件hosts.erb:

<% node['hostnames'].each do |name, value| %>
<%= value %>
<% end %>

配方 windows.rb 文件:

template 'C:\\Windows\\system32\\drivers\\etc\\hosts' do
  source 'hosts.erb'
  action :touch
end

kitchen Convergence 输出文件告诉我主机文件被覆盖而不更新?为什么?

   Recipe: hostnames::windows
     * template[C:\Windows\system32\drivers\etc\hosts] action touch
       - update content in file C:\Windows\system32\drivers\etc\hosts from 2d6bdf to 8068b4
       --- C:\Windows\system32\drivers\etc\hosts    2016-07-16 13:21:31.289888800 +0000
       +++ C:\Windows\system32\drivers\etc/chef-hosts20181128-568-1skk2oh   2018-11-28 01:15:55.998460000 +0000
       @@ -1,22 +1,6 @@
       -# Copyright (c) 1993-2009 Microsoft Corp.
       -#
       -# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
       -#
       -# This file contains the mappings of IP addresses to host names. Each
       -# entry should be kept on an individual line. The IP address should
       -# be placed in the first column followed by the corresponding host name.
       -# The IP address and the host name should be separated by at least one
       -# space.
       -#
       -# Additionally, comments (such as these) may be inserted on individual
       -# lines or following the machine name denoted by a '#' symbol.
       -#
       -# For example:
       -#
       -#      102.54.94.97     rhino.acme.com          # source server
       -#       38.25.63.10     x.acme.com              # x client host
       +# Content of the following hostnames are mainten by Chef cookbook

       -# localhost name resolution is handled within DNS itself.
       -#   127.0.0.1       localhost
       -#   ::1             localhost
       +10.219.33.1 a.us.bb-ops.io
       +10.219.34.2 b.us.bb-ops.io
       +10.219.35.3 c.us.bb-ops.io
       - update utime on file C:\Windows\system32\drivers\etc\hosts

【问题讨论】:

    标签: chef-infra chef-recipe test-kitchen chef-template


    【解决方案1】:

    这是大厨应有的行为。您有两种选择:1) 提供整个 hosts 文件作为模板或 2) 使用 FileEdit 类,例如 insert_line_if_no_match-instance_method

    【讨论】:

    • 感谢您澄清“动作:触摸”的行为,这是有道理的。我会将所有内容作为完成的文件添加到模板中。我还使用了 insert_line_if_no_match 函数,它也按预期工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2015-04-02
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    相关资源
    最近更新 更多