How to append to the same variable with augeas run via puppet

augeaspuppet

I have the following resource declaration to set the setenv.sh file via augeas in puppet.

augeas {'test':
    lens    => 'Properties.lns',
    incl    => '/tmp/setenv.sh',
    changes => "set CATALINA_OPTS \" $CATALINA_OPTS -XX:PermSize=192m -XX:MaxPermSize=192m \"",
}

I have run into a few issues with the above.

  1. The file basically appends configs to the CATALINA_OPTS variable which means when I run augeas it removes all instances of that variable and replaces it with my change. How can I achieve the following?

    CATALINA_OPTS="$CATALINA_OPTS -javaagent:/opt/appdynamics/appagent/javaagent.jar"
    CATALINA_OPTS="$CATALINA_OPTS -XX:PermSize=192m -XX:MaxPermSize=192m"
    
  2. An attempt to run the above fails due to the quotes. The debug output shows:

    Debug: Augeas[test](provider=augeas): sending command 'set' with params ["/files/tmp/setenv.sh/CATALINA_OPTS", "  -XX:PermSize=192m -XX:MaxPermSize=192m "]
    Debug: Augeas[test](provider=augeas): Put failed on one or more files, output from /augeas//error:
    Debug: Augeas[test](provider=augeas): /augeas/files/tmp/setenv.sh/error = put_failed
    Debug: Augeas[test](provider=augeas): /augeas/files/tmp/setenv.sh/error/path = /files/tmp/setenv.sh
    Debug: Augeas[test](provider=augeas): /augeas/files/tmp/setenv.sh/error/lens = /usr/share/augeas/lenses/dist/properties.aug:50.25-.100:
    Debug: Augeas[test](provider=augeas): /augeas/files/tmp/setenv.sh/error/message = Malformed child node 'CATALINA_OPTS'
    

How can I have the change use double quotes in the string?

Best Answer

If you just want to append lines to a file, try using file_line resource instead of augeas, for example:

file_line { 'line1':
  path => '/tmp/setenv.sh',
  line => 'CATALINA_OPTS="$CATALINA_OPTS -javaagent:/opt/appdynamics/appagent/javaagent.jar"',
}

file_line { 'line2':
  path => '/tmp/setenv.sh',
  line => 'CATALINA_OPTS="$CATALINA_OPTS -XX:PermSize=192m -XX:MaxPermSize=192m"',
}

To avoid issues with doublequotes, just singlequote the whole file line. If you want to add line to a specific place in the file, you can use the 'after' property.

file_line is available in 'puppetlabs/stdlib' module.