Skip to main content

Command Palette

Search for a command to run...

Ansible Playbook and Supervisorctl Restart Twice Problem

Published
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
  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

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

Reference

More from this blog

簡介 C++ 的 Type Erase (用多型和模板做 Duck Type)

起點 讓我們先從 template 出發:foo 需要一個 callback function。 template<typename Func> void foo(Func callback) { // ... callback(); } 但是這會讓編譯錯誤訊息有點模糊:假如 callback 並不是一個可以呼叫的函數指標,或者並不是一個 callable object ,那編譯器會說錯出在第四行。但是我們都希望,編譯器在呼叫函數時就幫我們指出:這不是 foo 想要的 call...

May 14, 20243 min read

帕秋莉的魔法筆記

45 posts

後端工程師。

不定時張貼一些寫扣時的筆記。