4

I have a tomcat.sh with its intended purpose to restart (stop and start) the tomcat after it has been deployed onto the remote host.

I noticed that the Shell and Command modules is not executing the .sh file. However, I am able to execute the .sh file manually on the remote host as the the remote user.

The Playbook tasks are listed below:

Shell

- name: ensures tomcat is restarted
  shell:
   "nohup {{tomcat_dir}}/apache-tomcat-{{tomcat_version}}/tomcat.sh &"

Command

- name: ensures tomcat is restarted test-test
  command: ./tomcat.sh
  args:
    chdir: "{{tomcat_dir}}/apache-tomcat-{{tomcat_version}}"
2
  • Are you executing the script with the same remote user w/ and w/o Ansible? Do you get any error message from Ansible? If the script only restarts tomcat, why are you sending it to the background? Commented Feb 5, 2016 at 2:21
  • There are no errors while executing the script. No particular reason for putting nohup. I just got used to sending processes in the background, but it should not affect the underlying shell command. On the contrary, the I left it out for the command task. Commented Feb 5, 2016 at 15:22

2 Answers 2

2

I was having the same problem. I have my simple script that is supposed to start a java program. shell and command module work very erratically. That is sometimes the java program is started successfully, and sometimes nothing happens. Even though ansible rc status shows as 0 (which is successful exit code ). I even put a echo "Hello" >> output.log as the first line in my script, to check if the script is actually picked for running. But, even that does not get printed.But, no errors whatsoever is printed and ansible module exit status (rc) is 0.Also, be sure to look at the stderr too. Sometimes, even though rc could be 0, but there might be some info in stderr

After lots of hair tearing, I could manage to fix my issue. I was running the java program as "sudo".I removed the sudo out of my script, and put it within playbook as the "become" directive - http://docs.ansible.com/ansible/become.html . This directive is available from ansible 2.0 onwards only, so I had to upgrade my ansible from 1.5 to 2.0. My playbook finally looked like this

- name: Execute run.sh
    become: true 
    script: /home/vagrant/deploy/target/scripts/run.sh

The script looks like this:

nohup java -Djava.net.preferIPv4Stack=true -Xms1048m -Xmx1048m -cp /x/y/z/1.jar com.github.binitabharati.x.Main >> output.log 2>&1 &

Also, notice that I have not used the command or shell module, instead I used script module.

Sign up to request clarification or add additional context in comments.

Comments

0

Does the value of {{tomcat_dir}} start with a "/"? If not then it will try to execute the command using the specified path relative to the home dir of whichever user ansible using to ssh in to the remote host.

incidentally, If you installed via package manager, this might be easier?

- name: restart tomcat
  become: yes
  become_user: sudo
  service: name=tomcat state=restarted

More detail on the "service" module here

Of course, the service name may be different, like tomcat6 or tomcat7 or whatever, but you get the gist, I hope.

Lastly, if you installed tomcat via ansible using a role from galaxy or github, your role may have handlers that you could copy to get this done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.