17

I use puppet to install a current JDK and tomcat.

package {
    [ "openjdk-6-jdk", "openjdk-6-doc", "openjdk-6-jre",
      "tomcat6", "tomcat6-admin", "tomcat6-common", "tomcat6-docs", 
      "tomcat6-user" ]:
    ensure => present,
}

Now I'd like to add

JAVA_HOME="/usr/lib/java"
export JAVA_HOME

to /etc/profile, just to get this out of the way. I haven't found a straightforward answer in the docs, yet. Is there a recommended way to do this?

In general, how do I tell puppet to place this file there or modify that file? I'm using puppet for a single node (in standalone mode) just to try it out and to keep a log of the server setup.

miku
  • 445
  • 1
  • 3
  • 12
  • 1
    If you're getting into Puppet, check out Example42 for prebuilt modules. They don't always work as you'd want, but they're coherent and stable. https://github.com/example42/puppet-modules – kashani Feb 22 '11 at 16:38

2 Answers2

42

mark's solution is the best for adding stuff to everyone's profile, but if you ever need to ensure some lines are in a file, Puppet Labs has a great module called stdlib which includes file_line which will do what you need. Previously I've used echo and grep in the exec type to do this, but file_line is so much easier and cleaner.

Here's the help for it:

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.
Mark McKinstry
  • 935
  • 7
  • 5
26

Add a file to /etc/profile.d/ with the suffix .sh. It will be sourced as part of /etc/profile in Red Hat and Debian and derivatives, can't say on other distros. Generally speaking, if at all possible, it's better to add snippets rather than replace distributed files as it tends to be more future safe.

So in puppet, the following would do:

file { "/etc/profile.d/set_java_home.sh":
    ensure => present,
    source => ...[whatever's appropriate for your setup]...,
    ...
}

This what you're looking for or do you need more detail?

mark
  • 2,325
  • 14
  • 10
  • 1
    This also works on OpenSUSE and Gentoo. – Kamil Kisiel Feb 22 '11 at 16:02
  • 1
    Thanks, that's great. Got it almost working, just need to find an appropriate way to reference a file relative to the `.pp` as source. – miku Feb 22 '11 at 16:17
  • 1
    Assuming Puppet 0.25 or better source => puppet:///$modulename/$file with the real system path being $modulename/files/$file – kashani Feb 22 '11 at 16:35
  • 2
    IIRC 2.6.5 and later emits a deprecation warning if you use the puppet:///$modulename/files/$filename notation. it should look like puppet:///modules/$modulename/files/$filename – ztron Mar 18 '11 at 20:58