2

What is the best approach to run a standalone .jar as a service via Ansible?

I need run metabase.jar via ansible on an Ubuntu EC2 host.

Should I daemonize it? Is there any recommended method or library?

Ideally I would like to manage the state via the Ansible handlers if possible.

Thanks

RedRobot
  • 53
  • 1
  • 8

1 Answers1

5

Depending upon the init system of your underlying box (init.d or systemctl) there are two approaches.


init.d

With ansible you can create a symlink of your application jar under

/etc/init.d/<your_service_name>

You can then use ansible's service module to manage the state of the service

 - service:
    name: <your_service_name>
    state: restarted

More info: Ansible service module documentation


systemctl

If your init method is systemd you will have to create a very simple Unit file

[Unit]
Description=<Description>

[Service]
ExecStart=/path/to/<your_app>.jar

[Install]
WantedBy=multi-user.target

Copy the unit file to

/etc/systemd/service/<your_unit_file>.service

Then you can use ansible's systemd module to manage the state of the service

- name: Start Service
  systemd:
    name: <your service name>
    state: started

More info: Ansible systemd module documentation

Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37