1

Im learning Puppet and currently trying to install tomcat. While trying to replace the Catalina home on the startup.sh using sed in the exec block, Im facing the below error.

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at '|export CATALINA_HOME=' at /etc/puppetlabs/code/environments/production/modules/tomcat/manifests/init.pp:26:58 on node agent

Current value of startup.sh

export CATALINA_HOME="/home/john"
export JAVA_HOME="/usr"
......
.....

Expected output

export CATALINA_HOME="/home/john/apache-tomcat-6.0.44"
export JAVA_HOME="/usr/java/default"

My code snippet

 .......
  exec { 'modify_file':
    command => "sed -i 's|export CATALINA_HOME="/home/john"|export CATALINA_HOME="/home/john/apache-tomcat-6.0.44"|g' /home/john/apache-tomcat-6.0.44/bin/startup.sh"
    path => '/bin',
}

Any help is really appreciated, thanks in advance.

Also, I had went thru the puppet documents regarding path atrribute of exec block but I'm not sure why it is being used and what should be my path value in my manifest file.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Goku
  • 482
  • 1
  • 7
  • 22

2 Answers2

2

Your sed expression is likely broken due to quote mismatch.

You could simplify the sed command by using:

sed -i '/CATALINA_HOME=/s,/home/john,&/apache-tomcat-6.0.44,;/JAVA_HOME=/s,/usr,&/java/default,' /home/john/apache-tomcat-6.0.44/bin/startup.sh

The expression contains 2 commands for both CATALINA_HOME and JAVA_HOME. Both commands uses the same syntax to append the required string to the variable declaration.

/<regex>/s will perform the subsitution on line with the <regex>.

The , is the command separator. I usually use / unless the pattern to search is a directory path.

& is printing the pattern space, i.e. the matched pattern.

oliv
  • 12,690
  • 25
  • 45
  • Thanks for your input.. but still the target file doesn't get changed below is the block `command => "sed '/CATALINA_HOME=/s,/home/john,&/apache-tomcat-6.0.44,;/JAVA_HOME=/s,/usr,&/java/default,' /home/john/apache-tomcat-6.0.44/bin/startup.sh", path => '/bin',` Am I missing something silly? – Goku Nov 08 '17 at 09:17
  • forgot the `-i` option to the `sed` command to replace within the file, please retry and provide any error message if any. – oliv Nov 08 '17 at 09:30
  • Really thanks a lot for your time here.. if possible can you kindly explain /s, and the ,& part.. Like what exactly it does..Thanks again.. – Goku Nov 08 '17 at 09:54
  • some more explanation in the response – oliv Nov 08 '17 at 10:06
  • Thanks a lot again !! – Goku Nov 08 '17 at 10:29
2

Had you already tried using the file_line resource type of puppetlabs-stdlib module instead of doing an exec call?

You can see how it works here.

Match parameter receive the old value and will be replaced by the value of line parameter. For example:

file_line { 'catalina':
  ensure => present,
  path   => '/etc/catalina/startup.sh',
  line   => 'export CATALINA_HOME=\"/home/john/apache-tomcat-6.0.44\"',
  match  => 'export CATALINA_HOME=\"/home/john\"',
}
Mdmansur
  • 63
  • 7