1
votes

I've got a server on which Supervisord is managing my processes. I normally start supervisord with the following command:

sudo /var/www/imd/venv/bin/supervisord -c /var/www/imd/deploy/supervisord.conf

I'm now trying to set things up with Ansible, but I'm unsure of how I should start Ansible. I can of course do it using something like:

- name: run supervisord
  command: "/var/www/imd/venv/bin/supervisord -c /var/www/imd/deploy/supervisord.conf"

This works, but only the first time that you run it. Second time you run the same script supervisord is of course already running, which causes the following error:

TASK [run supervisord] ******************************************************* fatal: [ansible-test1]: FAILED! => {"changed": true, "cmd": ["/var/www/imd/venv/bin/supervisord", "-c", "/var/www/imd/deploy/supervisord.conf"], "delta": "0:00:00.111700", "end": "2016-06-03 11:57:38.605804", "failed": true, "rc": 2, "start": "2016-06-03 11:57:38.494104", "stderr": "Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.\nFor help, use /var/www/imd/venv/bin/supervisord -h", "stdout": "", "stdout_lines": [], "warnings": []}

Does anybody know how I can correctly run supervisord with Ansible? All tips are welcome!

[EDIT]

Because the solution in the answer by mbarthelemy doesn't work for socket files I now managed to get it working with the following:

- name: run supervisord
  shell: if [ ! -S /var/run/supervisor.sock ]; then sudo /var/www/imd/venv/bin/supervisord -c /var/www/imd/deploy/supervisord.conf; fi

This of course is not very "ansibleish". If anybody has a real Ansible-based solution that would still be really welcome.

2

2 Answers

1
votes

Your situation is specific since you don't seem to use a regular Supervisor installed as a normal system package ; in that case you would start/stop/restart it like any other regular system service, using Ansible's service module.

By default, upon starting Supervisor creates a socket to listen for administrations commands from supervisorctl. When it stops it it supposed to remove it.

Try to find where this socket is created in your specific setup (default would be /var/run/supervisor.sock). Then, let the Ansible command module know that if the Suopervisord process is already running, the socket exists, using the creates option (documentation). This way it won't try to run the command if it's already running:

- name: run supervisord
  command: "./venv/bin/supervisord -c ./deploy/supervisord.conf"
  args: 
    chdir=/var/www/imd
    creates=/var/run/supervisor.sock

Edit : while this would be the right answer if /var/run/supervisor.sock were a file, it won't work because it's a socket, and Ansible's create parameter won't work.

The most Ansible-ish solution I can think of is using an external Ansible module like one of these, to check if you process already exists (test_process) or is already listening (test_tcp)

1
votes

You can use supervisor module

- supervisorctl:
    name: my_app
    state: restarted
    config: /var/opt/my_project/supervisord.conf

or

 - name: Restart my_app
   supervisorctl:
     name: my_app
     state: restarted
     config: /var/opt/my_project/supervisord.conf

full documentation on https://docs.ansible.com/ansible/2.7/modules/supervisorctl_module.html#examples