Ansible 安装
使用 Ansible 自动化部署 OpenClaw
概述
使用 Ansible 可以自动化 OpenClaw 的安装和配置过程,特别适合:
- 多服务器批量部署
- 基础设施即代码(IaC)
- 可重复的环境配置
- CI/CD 流水线集成
批量部署
一次配置,多台服务器
可重复
幂等性操作,安全可靠
版本控制
配置即代码,易于管理
前置要求
- Ansible 2.9 或更高版本
- 目标服务器的 SSH 访问权限
- Python 3.8+ (目标服务器)
# 检查 Ansible 版本
ansible --version
# 安装 Ansible(如果需要)
pip install ansiblePlaybook 示例
创建 OpenClaw 安装 Playbook:
# openclaw.yml
---
- name: Install and configure OpenClaw
hosts: openclaw_servers
become: yes
vars:
openclaw_version: "latest"
openclaw_user: "openclaw"
ai_provider: "anthropic"
tasks:
- name: Create OpenClaw user
user:
name: "{{ openclaw_user }}"
shell: /bin/bash
create_home: yes
- name: Install OpenClaw
shell: |
curl -fsSL https://clawdbotai.co/install.sh | bash
args:
creates: /usr/local/bin/openclaw
- name: Create config directory
file:
path: "/home/{{ openclaw_user }}/.openclaw"
state: directory
owner: "{{ openclaw_user }}"
mode: '0755'
- name: Deploy configuration
template:
src: templates/config.yaml.j2
dest: "/home/{{ openclaw_user }}/.openclaw/config.yaml"
owner: "{{ openclaw_user }}"
mode: '0600'
- name: Create systemd service
template:
src: templates/openclaw.service.j2
dest: /etc/systemd/system/openclaw.service
notify: Restart OpenClaw
- name: Enable and start OpenClaw
systemd:
name: openclaw
enabled: yes
state: started
handlers:
- name: Restart OpenClaw
systemd:
name: openclaw
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/openclaw.service.j2
[Unit]
Description=OpenClaw Gateway
After=network.target
[Service]
Type=simple
User={{ openclaw_user }}
ExecStart=/usr/local/bin/openclaw gateway
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target变量配置
创建变量文件来管理敏感信息:
# group_vars/openclaw_servers.yml
---
openclaw_version: "1.3.0"
openclaw_user: "openclaw"
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/openclaw_servers.yml
# 运行时解密
ansible-playbook openclaw.yml --ask-vault-pass运行部署
Inventory 文件
# inventory.ini
[openclaw_servers]
server1.example.com
server2.example.com
192.168.1.100执行 Playbook
# 检查配置(dry run)
ansible-playbook -i inventory.ini openclaw.yml --check
# 执行部署
ansible-playbook -i inventory.ini openclaw.yml
# 仅部署到特定服务器
ansible-playbook -i inventory.ini openclaw.yml --limit server1.example.com
# 带 Vault 密码
ansible-playbook -i inventory.ini openclaw.yml --ask-vault-pass