# Ansible Playbook and Supervisorctl Restart Twice Problem

# 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:

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

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

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

## Problem

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](https://docs.ansible.com/ansible/2.9/modules/supervisorctl_module.html)

And the behavior of  `supervisorctl update` is：

> Reload config and add/remove as necessary, and will restart affected programs -- [Supervisord Doc](http://supervisord.org/running.html)

## Temp Solution?

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

```yaml
- 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
```

## Reference

* [Ansible: supervisorctl – Manage the state of a program or group of programs running via supervisord](https://docs.ansible.com/ansible/2.9/modules/supervisorctl_module.html)
* [Supervisor: Running Supervisor](http://supervisord.org/running.html)

