使用 puppet 在 /etc/profile 中添加行?

mik*_*iku 16 linux bash java puppet

我使用 puppet 来安装当前的 JDK 和 tomcat。

package {
    [ "openjdk-6-jdk", "openjdk-6-doc", "openjdk-6-jre",
      "tomcat6", "tomcat6-admin", "tomcat6-common", "tomcat6-docs", 
      "tomcat6-user" ]:
    ensure => present,
}
Run Code Online (Sandbox Code Playgroud)

现在我想补充

JAVA_HOME="/usr/lib/java"
export JAVA_HOME
Run Code Online (Sandbox Code Playgroud)

/etc/profile,只是为了解决这个问题。我还没有在文档中找到直接的答案。有没有推荐的方法来做到这一点?

一般来说,我如何告诉 puppet 将这个文件放在那里或修改那个文件?我将 puppet 用于单个节点(在独立模式下)只是为了尝试并保留服务器设置的日志

小智 42

mark 的解决方案最适合向每个人的个人资料中添加内容,但是如果您需要确保某些行在文件中,Puppet Labs 有一个名为stdlib的很棒的模块,其中包含 file_line ,它可以满足您的需求。以前我在 exec 类型中使用了 echo 和 grep 来做到这一点,但 file_line 更容易和更清晰。

这是它的帮助:

Ensures that a given line is contained within a file. The implementation
matches the full line, including whitespace at the beginning and end. If
the line is not contained in the given file, Puppet will add the line to
ensure the desired state. Multiple resources may be declared to manage
multiple lines in the same file.

Example:

file_line { 'sudo_rule':
   path => '/etc/sudoers',
   line => '%sudo ALL=(ALL) ALL',
}
file_line { 'sudo_rule_nopw':
   path => '/etc/sudoers',
   line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
}

In this example, Puppet will ensure both of the specified lines are
contained in the file /etc/sudoers.
Run Code Online (Sandbox Code Playgroud)

  • 确保您在客户端上启用了 pluginsync,否则这似乎会默默地失败。 (2认同)

mar*_*ark 26

添加/etc/profile.d/后缀为 的文件.sh。它将作为 Red Hat 和 Debian 及其衍生产品中 /etc/profile 的一部分提供,不能在其他发行版上说。一般来说,如果可能的话,最好添加片段而不是替换分布式文件,因为它在未来更安全。

因此,在 puppet 中,将执行以下操作:

file { "/etc/profile.d/set_java_home.sh":
    ensure => present,
    source => ...[whatever's appropriate for your setup]...,
    ...
}
Run Code Online (Sandbox Code Playgroud)

这是您正在寻找的内容还是您需要更多详细信息?

  • 如果您使用 puppet:///$modulename/files/$filename 表示法,IIRC 2.6.5 及更高版本会发出弃用警告。它应该看起来像 puppet:///modules/$modulename/files/$filename (2认同)