Adding lines to /etc/profile with puppet?

40,171

Solution 1

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?

Solution 2

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.
Share:
40,171

Related videos on Youtube

miku
Author by

miku

Updated on September 17, 2022

Comments

  • miku
    miku over 1 year

    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.

    • kashani
      kashani about 13 years
      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. github.com/example42/puppet-modules
  • Rahim
    Rahim about 13 years
    This also works on OpenSUSE and Gentoo.
  • miku
    miku about 13 years
    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.
  • kashani
    kashani about 13 years
    Assuming Puppet 0.25 or better source => puppet:///$modulename/$file with the real system path being $modulename/files/$file
  • ztron
    ztron about 13 years
    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
  • gak
    gak over 11 years
    Make sure you have pluginsync enabled on the client, otherwise this seems to silently fail.
  • Francesco Casula
    Francesco Casula about 9 years
    here's the doc with direct anchor forge.puppetlabs.com/puppetlabs/stdlib#resources