Ansible Playbook and Supervisorctl Restart Twice Problem


2 min read

Ansible Playbook and Supervisorctl Restart Twice Problem

A possible scenario might be:

  1. Update supervisor config
  2. Update app config
  3. If the config is changed, then restart app by using ansible-supervisor

The script of ansible playbook might be:

- name: Upload app config
    src: #...
    dest: #...
  register: app_conf

- name: Upload supervisor config
  become: true
    src: #...
    dest: #...
  register: supervisor_conf

- name: Restart app
    name: app
    state: restarted
    when: app_conf.changed or supervisor_conf.changed


It looks great and awesome, but the problem is that if supervisorctl's config is updated, then the app will be restarted TWICE in a short time! Why?

According to ansible's document, behavior of state: restarted will be:

When state = restarted, the module will call supervisorctl update then call supervisorctl restart. -- Ansible Doc

And the behavior of supervisorctl update is:

Reload config and add/remove as necessary, and will restart affected programs -- Supervisord Doc

Temp Solution?

Record whether the config supervisor is changed, then call update xor restart according to tis flag:

- name: Start server by update
  become: true
  shell: supervisorctl update app
  when: app_conf.changed and supervisor_config.changed

- name: Start server by restart
  become: true
  shell: supervisorctl restart app
  when: app_conf.changed and not supervisor_config.changed