Gary Lam
Gary Lam

Reputation: 41

Ansible Command, Shell modules not executing script

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}}"

Upvotes: 4

Views: 8505

Answers (2)

Binita Bharati
Binita Bharati

Reputation: 5888

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.

Upvotes: 2

Nicholas Abbruzzese
Nicholas Abbruzzese

Reputation: 103

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.

Upvotes: 0

Related Questions