cfengine3 有没有一种简单的方法可以根据其运行的操作系统复制不同的文件

发布于 2024-12-10 09:57:36 字数 128 浏览 4 评论 0原文

我有两个不同版本的 linux/unix,每个版本都运行 cfengine3。是否可以在两台机器上放置一个 Promise.cf 文件,该文件将根据客户端上的操作系统复制不同的文件?我已经在互联网上搜索了几个小时,但还没有找到任何有用的东西。

I have two different versions of linux/unix each running cfengine3. Is it possible to have one promises.cf file I can put on both machines that will copy different files based on what os is on the clients? I have been searching around the internet for a few hours now and have not found anything useful yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年少掌心 2024-12-17 09:57:36

有几种方法可以做到这一点。最简单的是,您可以简单地拥有不同的文件: Promise 取决于操作系统,例如:

files:
  ubuntu_10::
    "/etc/hosts"
      copy_from => mycopy("$(repository)/etc.hosts.ubuntu_10");
  suse_9::
    "/etc/hosts"
      copy_from => mycopy("$(repository)/etc.hosts.suse_9");
  redhat_5::
    "/etc/hosts"
      copy_from => mycopy("$(repository)/etc.hosts.redhat_5");
  windows_7::
    "/etc/hosts"
      copy_from => mycopy("$(repository)/etc.hosts.windows_7");

通过认识到内置 CFEngine 变量 $(sys.flavor) 包含操作系统的类型和版本,可以轻松简化此示例系统,因此我们可以按如下方式重写此示例:

"/etc/hosts"
    copy_from => mycopy("$(repository)/etc.$(sys.flavor)");

实现此任务的更灵活方法在 CFEngine 术语中称为“分层复制。”在此模式中,您可以指定想要区分文件的任意变量列表,以及应考虑它们的顺序(从最具体到最一般)。当执行复制承诺时,将复制找到的最具体的文件。

这种模式实现起来非常简单:

# Use single copy for all files
body agent control
{
   files_single_copy => { ".*" };
}

bundle agent test
{
vars:
  "suffixes"   slist => { ".$(sys.fqhost)", ".$(sys.uqhost)", ".$(sys.domain)",
                          ".$(sys.flavor)", ".$(sys.ostype)", "" };
files:
  "/etc/hosts"
    copy_from => local_dcp("$(repository)/etc/hosts$(suffixes)");
}

如您所见,我们定义了一个名为 $(suffixes) 的列表变量,其中包含我们想要区分文件的标准。尽管您可以使用任意 CFEngine 变量,但此列表中包含的所有变量均由 CFEngine 自动定义。然后我们只需将该变量作为标量包含在我们的 copy_from 参数中。因为 CFEngine 会自动扩展列表,所以它将依次尝试每个变量,多次执行复制承诺(列表中的每个值执行一次)并复制存在的第一个文件。例如,对于名为 superman.justiceleague.com 的 Linux SuSE 11 计算机,@(suffixes) 变量将包含以下值:

{ ".superman.justiceleague.com", ".superman", ".justiceleague.com", ".suse_11",
  ".linux", "" }

当执行文件复制承诺时,隐式循环将导致这些字符串按顺序附加到“ $(repository)/etc/hosts",因此将按顺序尝试以下文件名:hosts.superman.justiceleague.com、hosts.justiceleague.com、hosts.suse_11、主机.linux 和主机。第一个存在的将被复制到客户端中的 /etc/hosts 上,其余的将被跳过。

为了使这种技术发挥作用,我们必须对要处理的所有文件启用“单一副本”。这是一个配置参数,告诉 CFEngine 最多复制每个文件一次,忽略对同一目标文件的连续复制操作。代理控制正文中的 files_single_copy 参数指定正则表达式列表,以匹配应应用单一副本的文件名。通过将其设置为 ".*" 我们可以匹配所有文件名。

对于与任何现有文件都不匹配的主机,列表中的最后一项(空字符串)将导致复制通用主机文件。请注意,除了最后一个元素之外,每个文件名的点都包含在 $(suffixes) 中。

我希望这有帮助。

(ps和无耻的插件:这摘自我即将出版的书“学习CFEngine 3”,由O'出版赖利)

There are several ways of doing this. At the simplest, you can simply have different files: promises depending on the operating system, for example:

files:
  ubuntu_10::
    "/etc/hosts"
      copy_from => mycopy("$(repository)/etc.hosts.ubuntu_10");
  suse_9::
    "/etc/hosts"
      copy_from => mycopy("$(repository)/etc.hosts.suse_9");
  redhat_5::
    "/etc/hosts"
      copy_from => mycopy("$(repository)/etc.hosts.redhat_5");
  windows_7::
    "/etc/hosts"
      copy_from => mycopy("$(repository)/etc.hosts.windows_7");

This example can be easily simplified by realizing that the built-in CFEngine variable $(sys.flavor) contains the type and version of the operating system, so we could rewrite this example as follows:

"/etc/hosts"
    copy_from => mycopy("$(repository)/etc.$(sys.flavor)");

A more flexible way to achieve this task is known in CFEngine terminology as "hierarchical copy." In this pattern, you specify an arbitrary list of variables by which you want files to be differentiated, and the order in which they should be considered, from most specific to most general. When the copy promise is executed, the most-specific file found will be copied.

This pattern is very simple to implement:

# Use single copy for all files
body agent control
{
   files_single_copy => { ".*" };
}

bundle agent test
{
vars:
  "suffixes"   slist => { ".$(sys.fqhost)", ".$(sys.uqhost)", ".$(sys.domain)",
                          ".$(sys.flavor)", ".$(sys.ostype)", "" };
files:
  "/etc/hosts"
    copy_from => local_dcp("$(repository)/etc/hosts$(suffixes)");
}

As you can see, we are defining a list variable called $(suffixes) that contains the criteria by which we want to differentiate the files. All the variables contained in this list are automatically defined by CFEngine, although you could use any arbitrary CFEngine variables. Then we simply include that variable, as a scalar, in our copy_from parameter. Because CFEngine does automatic list expansion, it will try each variable in turn, executing the copy promise multiple times (one for each value in the list) and copy the first file that exists. For example, for a Linux SuSE 11 machine called superman.justiceleague.com, the @(suffixes) variable will contain the following values:

{ ".superman.justiceleague.com", ".superman", ".justiceleague.com", ".suse_11",
  ".linux", "" }

When the file-copy promise is executed, implicit looping will cause these strings to be appended in sequence to "$(repository)/etc/hosts", so the following filenames will be attempted in sequence: hosts.superman.justiceleague.com, hosts.justiceleague.com, hosts.suse_11, hosts.linux and hosts. The first one to exist will be copied over /etc/hosts in the client, and the rest will be skipped.

For this technique to work, we have to enable "single copy" on all the files you want to process. This is a configuration parameter that tells CFEngine to copy each file at most once, ignoring successive copy operations for the same destination file. The files_single_copy parameter in the agent control body specifies a list of regular expressions to match filenames to which single-copy should apply. By setting it to ".*" we match all filenames.

For hosts that don't match any of the existing files, the last item on the list (an empty string) will cause the generic hosts file to be copied. Note that the dot for each of the filenames is included in $(suffixes), except for the last element.

I hope this helps.

(p.s. and shameless plug: this is taken from my upcoming book, "Learning CFEngine 3", published by O'Reilly)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文