user2099747
user2099747

Reputation: 11

How to run a binary executable using ansible and keep it running? Can I use a command/ shell module to do it?

I am having a executable file say : carbonserver . I need to run it and keep it running so that api can interact with it.

Upvotes: 1

Views: 4230

Answers (1)

SztupY
SztupY

Reputation: 10546

If this a real service, then instead of ansible I would delegate the task to the system's service handler. On most modern linux distributions this is usually systemd, so I would create a template that generates a new systemd unit file, then use ansible's systemd module to start it up:

templates/carbonserver.service.j2:

[Unit]
Description=Carbon Server

[Service]
ExecStart=/usr/bin/carbonserver

[Install]
WantedBy=multi-user.target

Playbook:

- template:
    src: templates/carbonserver.service.j2
    dest: /etc/systemd/system/carbonserver.service
    owner: bin
    group: wheel
    mode: 0644

- systemd:
    name: carbonserver
    state: started
    enabled: True

This is the preferred way if your server is something that you want the system itself to manage. For example if it stops or dies, you can configure systemd to start it up again. You can also configure it to start up automatically on system startup, etc.

If you have a Linux distribution that does not support systemd, or you want to run it on other type of machine (OSX or Windows) then you might need to use the specific operating system's own service mechanism to do that.

If however you only want to run it once using ansible, and just want to keep it running on the background (and don't really care if it dies, etc.) then you can try some others like:

- shell: "( ( nohup /usr/bin/carbonserver 1>/dev/null 2>&1 ) & )"
  async: 5
  poll: 0

This runs a generic shell command in the background, makes sure it won't get killed when ansible finishes, and does it all in the background, so the next task can run immediately. Note that this is only a viable approach if you really don't care about your service running, which is most likely not what you want

Upvotes: 6

Related Questions