Ansible 安装

使用 Ansible 自动化部署 ClawdBot

概述

使用 Ansible 可以自动化 ClawdBot 的安装和配置过程,特别适合:

  • 多服务器批量部署
  • 基础设施即代码(IaC)
  • 可重复的环境配置
  • CI/CD 流水线集成

批量部署

一次配置,多台服务器

可重复

幂等性操作,安全可靠

版本控制

配置即代码,易于管理

前置要求

  • Ansible 2.9 或更高版本
  • 目标服务器的 SSH 访问权限
  • Python 3.8+ (目标服务器)
# 检查 Ansible 版本
ansible --version

# 安装 Ansible(如果需要)
pip install ansible

Playbook 示例

创建 ClawdBot 安装 Playbook:

# clawdbot.yml
---
- name: Install and configure ClawdBot
  hosts: clawdbot_servers
  become: yes
  vars:
    clawdbot_version: "latest"
    clawdbot_user: "clawdbot"
    ai_provider: "anthropic"

  tasks:
    - name: Create ClawdBot user
      user:
        name: "{{ clawdbot_user }}"
        shell: /bin/bash
        create_home: yes

    - name: Install ClawdBot
      shell: |
        curl -fsSL https://clawdbotai.co/install.sh | bash
      args:
        creates: /usr/local/bin/clawdbot

    - name: Create config directory
      file:
        path: "/home/{{ clawdbot_user }}/.clawdbot"
        state: directory
        owner: "{{ clawdbot_user }}"
        mode: '0755'

    - name: Deploy configuration
      template:
        src: templates/config.yaml.j2
        dest: "/home/{{ clawdbot_user }}/.clawdbot/config.yaml"
        owner: "{{ clawdbot_user }}"
        mode: '0600'

    - name: Create systemd service
      template:
        src: templates/clawdbot.service.j2
        dest: /etc/systemd/system/clawdbot.service
      notify: Restart ClawdBot

    - name: Enable and start ClawdBot
      systemd:
        name: clawdbot
        enabled: yes
        state: started

  handlers:
    - name: Restart ClawdBot
      systemd:
        name: clawdbot
        state: restarted

配置模板

# templates/config.yaml.j2
ai:
  provider: {{ ai_provider }}
  model: {{ ai_model | default('claude-3-5-sonnet-20241022') }}
  api_key: {{ anthropic_api_key }}

channels:
{% if telegram_enabled | default(false) %}
  telegram:
    enabled: true
    bot_token: {{ telegram_bot_token }}
{% endif %}

gateway:
  port: {{ gateway_port | default(8080) }}

Systemd 服务模板

# templates/clawdbot.service.j2
[Unit]
Description=ClawdBot Gateway
After=network.target

[Service]
Type=simple
User={{ clawdbot_user }}
ExecStart=/usr/local/bin/clawdbot gateway
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

变量配置

创建变量文件来管理敏感信息:

# group_vars/clawdbot_servers.yml
---
clawdbot_version: "1.3.0"
clawdbot_user: "clawdbot"
ai_provider: "anthropic"
ai_model: "claude-3-5-sonnet-20241022"
gateway_port: 8080
telegram_enabled: true

# 使用 Ansible Vault 加密敏感信息
# ansible-vault encrypt_string 'your-api-key' --name 'anthropic_api_key'

使用 Ansible Vault

# 加密敏感变量
ansible-vault encrypt group_vars/clawdbot_servers.yml

# 运行时解密
ansible-playbook clawdbot.yml --ask-vault-pass

运行部署

Inventory 文件

# inventory.ini
[clawdbot_servers]
server1.example.com
server2.example.com
192.168.1.100

执行 Playbook

# 检查配置(dry run)
ansible-playbook -i inventory.ini clawdbot.yml --check

# 执行部署
ansible-playbook -i inventory.ini clawdbot.yml

# 仅部署到特定服务器
ansible-playbook -i inventory.ini clawdbot.yml --limit server1.example.com

# 带 Vault 密码
ansible-playbook -i inventory.ini clawdbot.yml --ask-vault-pass