# Ansible 部署 Supervisor

1\. 引言

Supervisor 是一个用于管理和监控进程的工具，常用于在 Linux 系统中管理后台任务、守护进程和长期运行的应用程序。通过使用 Ansible，您可以实现自动化部署和配置 Supervisor，提高系统管理的效率和可靠性。

2\. Supervisor 的使用场景

Supervisor 在以下场景中特别有用：

2.1 后台任务管理

Supervisor 可以用于管理后台任务，例如定时脚本、数据抓取任务等。它可以确保这些任务在系统启动时自动运行，并在意外退出时重新启动。

2.2 守护进程管理

Supervisor 可以管理守护进程，如 Web 服务器、消息队列等。通过监控和自动重启异常退出的进程，Supervisor 提供了一种可靠的方式来保持这些核心服务的稳定运行。

2.3 长期运行应用程序管理

Supervisor 也适用于管理长期运行的应用程序，如 WebSocket 服务器、实时数据处理等。它可以确保这些应用程序在系统启动时启动，并在崩溃时自动重启。

3\. 部署 Supervisor

下面是使用 Ansible 部署 Supervisor 的最佳实践步骤：

3.1 配置Host文件

创建一个名 deploy-supervisor-host.ini 的 Ansible Inventory 文件，并使用文本编辑器打开。在 Inventory 中，定义以下主机信息：

```
[deploy]
aliyun_ecs ansible_ssh_host=47.96.25.224 ansible_ssh_port=22 ansible_user=ecs-user ansible_ssh_pass=Ecs-user123 ansible_sudo_pass=Ecs-user123
```

3.2 准备配置文件

您可以根据需要进行 Supervisor 的配置。创建一个名为 supervisor.conf 的配置文件，并使用以下示例内容：

```ini
[inet_http_server]
port = :9001
username = admin
password = 123456

[include]
files =/etc/supervisord.d/conf.d/*.ini

[supervisord]
logfile = /var/log/supervisor/supervisord.log
logfile_maxbytes = 50MB
logfile_backups=10
loglevel = info
pidfile = /var/run/supervisord.pid
nodaemon = false
minfds = 65535
#minprocs = 200
umask = 022
user = root
identifier = supervisor
nocleanup = true
childlogdir = /tmp
strip_ansi = false

[supervisorctl]
serverurl = http://127.0.0.1:9001
username = admin
password = 123456
prompt = supervisor

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
```

3.3 准备开机启动脚本文件

```systemd
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.d/supervisor.conf
ExecStop=/usr/bin/supervisorctl -c /etc/supervisord.d/supervisor.conf shutdown
ExecReload=/usr/bin/supervisorctl -c /etc/supervisord.d/supervisor.conf reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
```

3.4 创建 Playbook

创建一个名为 deploy-supervisor.yml 的 Ansible Playbook 文件，并使用文本编辑器打开。在 Playbook 中，定义以下任务：

```yaml
---
- name: 安装 Supervisor
  hosts: deploy
  become: true
  tasks:
    - name: 安装 Supervisor 包
      apt:
        name: supervisor
        state: present
      when: ansible_os_family == 'Debian'

    - name: 安装 Supervisor 包
      yum:
        name: supervisor
        state: present
      when: ansible_os_family == 'RedHat'

    - name: 配置 Supervisor 配置文件
      copy: 
        src: supervisor.conf
        dest: /etc/supervisord.d/supervisor.conf
        owner: root

    - name: 配置 supervisord systemctl 服务
      copy: 
        src: supervisord.service
        dest: /usr/lib/systemd/system/supervisord.service 
        owner: root

    - name: 启用并启动 Supervisor 服务
      service:
        name: supervisord
        state: restarted
        enabled: true
        daemon_reload: true
    
    - name: 检查端口是否运行
      wait_for: port=9001 state=started delay=1 timeout=30
```

上述 Playbook 定义了以下任务：

* 装 Supervisor 包，根据操作系统类型使用对应的包管理器。
* 复制 Supervisor 配置文件。
* 服务 Supervisor 开机启动脚本文件。
* 启用并启动 Supervisor 服务。
* 测试 Supervisor 端口是否正常

<br>

&#x20;3.5 执行 Playbook

在终端中，导航到包含 Playbook 和配置文件的目录，并运行以下命令来执行 Ansible Playbook：

```bash

$ ansible-playbook deploy-supervisor.yml -i deploy-supervisor-host.ini


PLAY [安装 Supervisor] **************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [aliyun_ecs]

TASK [安装 Supervisor 包] ***********************************************************************************
skipping: [aliyun_ecs]

TASK [安装 Supervisor 包] ***********************************************************************************
ok: [aliyun_ecs]

TASK [配置 Supervisor 配置文件] *****************************************************************************
ok: [aliyun_ecs]

TASK [配置 supervisord systemctl 服务] **********************************************************************
ok: [aliyun_ecs]

TASK [启用并启动 Supervisor 服务] ***************************************************************************
changed: [aliyun_ecs]

TASK [检查端口是否运行] *************************************************************************************
ok: [aliyun_ecs]

PLAY RECAP **************************************************************************************************
aliyun_ecs                 : ok=5    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
```

Ansible 将在目标主机上安装 Supervisor 并启动服务。您可以检查目标主机上的 Supervisor 配置文件和服务状态，确保一切正常。<br>

4\. 结论

本文介绍了使用 Ansible 部署 Supervisor 的实践。通过自动化配置和部署 Supervisor，您可以轻松管理后台任务、守护进程和长期运行的应用程序，提高系统的稳定性和可靠性。使用 Ansible Playbook，您可以定义 Supervisor 的安装和配置任务，并在目标主机上执行。这种自动化方式能够节省时间和减少手动操作的出错风险。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/ansible/ansible-bu-shu-supervisor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
