Maeh
Maeh

Reputation: 1764

Running a server with Ansible

I just started using Ansible and I am having trouble running a server.

I have a server which can be started using java -jar target/server-1.0-SNAPSHOT.jar. However, this will start the server and keep running forever displaying output, so Ansible never finishes.

This is what I tried that never finishes:

- name: Start server
  command: chdir=~/server java -jar target/server-1.0-SNAPSHOT.jar

What is the proper way to do this?

Upvotes: 4

Views: 2281

Answers (3)

Raul Hugo
Raul Hugo

Reputation: 1136

Put that in a PID and send the output to nohup.

Something like this:

nohup java -jar target/server-1.0-SNAPSHOT.jar &

In your playbook:

  • name: Start server command: chdir=~/server nohup java -jar target/server-1.0-SNAPSHOT.jar &

If you want kill the process kill -9 #numerofpid.

Upvotes: 0

As @Petro026 suggested, your choices are asynchronous task or creating a service.

I would strongly suggest against the asynchronous task approach. It's a very fragile solution:

  • What if the host is restarted?
  • What if you run your playbook twice?
  • What if your server app just dies?

Your best bet is to create a service for it, and probably the easiest approach for it would involve using a process control system like supervisord, which is supported by ansible.

From the supervisor docs:

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

Upvotes: 2

Petro026
Petro026

Reputation: 1309

Either create a service, as @udondan suggests, or use an asynchronous task to launch your server. http://docs.ansible.com/ansible/playbooks_async.html

Upvotes: 3

Related Questions