Automated deployment of CrewAI applications using Ansible.
This playbook installs Python, creates a virtual environment, and deploys a CrewAI application.
Create inventory.ini:
[crewai_servers]
crewai-prod-01 ansible_host=192.168.1.100
crewai-prod-02 ansible_host=192.168.1.101
[crewai_servers:vars]
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3
Create crewai.yml:
---
- name: Deploy CrewAI Application
hosts: crewai_servers
become: true
vars:
app_name: crewai
app_dir: /opt/crewai
python_version: "3.11"
openai_api_key: "{{ vault_openai_api_key }}"
serper_api_key: "{{ vault_serper_api_key }}"
tasks:
- name: Install system dependencies
apt:
name:
- python3
- python3-pip
- python3-venv
- python3-dev
- git
- build-essential
state: present
update_cache: yes
- name: Create application directory
file:
path: "{{ app_dir }}"
state: directory
mode: '0755'
- name: Create subdirectories
file:
path: "{{ item }}"
state: directory
mode: '0755'
loop:
- "{{ app_dir }}/src"
- "{{ app_dir }}/config"
- "{{ app_dir }}/output"
- "{{ app_dir }}/logs"
- name: Create Python virtual environment
command: python3 -m venv {{ app_dir }}/venv
args:
creates: {{ app_dir }}/venv/bin/activate
- name: Upgrade pip
pip:
name: pip
state: latest
virtualenv: "{{ app_dir }}/venv"
- name: Install CrewAI
pip:
name:
- crewai
- crewai-tools
- python-dotenv
- langchain-openai
virtualenv: "{{ app_dir }}/venv"
- name: Create .env file
copy:
dest: "{{ app_dir }}/.env"
content: |
OPENAI_API_KEY={{ openai_api_key }}
SERPER_API_KEY={{ serper_api_key }}
CREWAI_TELEMETRY_DISABLED=true
mode: '0600'
- name: Create agents configuration
copy:
dest: "{{ app_dir }}/config/agents.yaml"
content: |
researcher:
role: >
{topic} Senior Data Researcher
goal: >
Uncover cutting-edge developments in {topic}
backstory: >
You're a seasoned researcher with a knack for uncovering the latest
developments in {topic}.
verbose: true
allow_delegation: false
writer:
role: >
{topic} Content Writer
goal: >
Create engaging content about {topic}
backstory: >
You're a skilled writer who makes complex topics accessible.
verbose: true
allow_delegation: true
mode: '0644'
- name: Create tasks configuration
copy:
dest: "{{ app_dir }}/config/tasks.yaml"
content: |
research_task:
description: >
Conduct thorough research about {topic}
expected_output: >
A list with 10 bullet points
agent: researcher
writing_task:
description: >
Create a blog post based on research
expected_output: >
A 500-word blog post
agent: writer
output_file: output/blog_post.md
mode: '0644'
- name: Create main application script
copy:
dest: "{{ app_dir }}/src/main.py"
content: |
#!/usr/bin/env python3
import os
import sys
from dotenv import load_dotenv
from crewai import Crew, Process, Agent, Task
load_dotenv()
def main():
# Load agents
researcher = Agent(
role="{{ topic }} Senior Data Researcher",
goal="Uncover cutting-edge developments in {{ topic }}",
backstory="You're a seasoned researcher.",
verbose=True
)
writer = Agent(
role="{{ topic }} Content Writer",
goal="Create engaging content",
backstory="You're a skilled writer.",
verbose=True
)
# Load tasks
research_task = Task(
description="Research {{ topic }}",
expected_output="10 bullet points",
agent=researcher
)
writing_task = Task(
description="Write blog post",
expected_output="500-word post",
agent=writer
)
# Create and run crew
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential,
verbose=True
)
result = crew.kickoff(inputs={'topic': '{{ topic }}'})
print(result)
if __name__ == "__main__":
main()
mode: '0755'
- name: Create systemd service
copy:
dest: /etc/systemd/system/crewai.service
content: |
[Unit]
Description=CrewAI Application
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory={{ app_dir }}
Environment="PATH={{ app_dir }}/venv/bin"
EnvironmentFile={{ app_dir }}/.env
ExecStart={{ app_dir }}/venv/bin/python {{ app_dir }}/src/main.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
notify:
- Restart CrewAI
- name: Enable and start service
systemd:
name: crewai
enabled: yes
state: started
daemon_reload: yes
handlers:
- name: Restart CrewAI
systemd:
name: crewai
state: restarted
daemon_reload: yes
# Basic run
ansible-playbook -i inventory.ini crewai.yml
# With vault secrets
ansible-playbook -i inventory.ini crewai.yml --ask-vault-pass
# Limit to specific host
ansible-playbook -i inventory.ini crewai.yml --limit crewai-prod-01
# Dry run (check mode)
ansible-playbook -i inventory.ini crewai.yml --check
Create secrets file:
ansible-vault create group_vars/all/vault.yml
Add your secrets:
vault_openai_api_key: "sk-your-openai-api-key"
vault_serper_api_key: "your-serper-api-key"
# Check service status
ansible -i inventory.ini crewai_servers -m systemd -a "name=crewai state=started"
# View logs
ansible -i inventory.ini crewai_servers -m shell -a "journalctl -u crewai -n 20"
# Test execution
ansible -i inventory.ini crewai_servers -m shell -a "{{ app_dir }}/venv/bin/python {{ app_dir }}/src/main.py"
- name: Rolling update CrewAI
hosts: crewai_servers
become: true
serial: 1 # Update one server at a time
tasks:
- name: Stop service
systemd:
name: crewai
state: stopped
- name: Update pip packages
pip:
name:
- crewai
- crewai-tools
state: latest
virtualenv: "{{ app_dir }}/venv"
- name: Start service
systemd:
name: crewai
state: started
- name: Verify service
systemd:
name: crewai
state: started
register: result
retries: 3
delay: 10
until: result.status.ActiveState == "active"
- name: Backup CrewAI configuration
hosts: crewai_servers
become: true
tasks:
- name: Create backup directory
file:
path: "{{ app_dir }}/backups"
state: directory
- name: Backup config files
archive:
path:
- "{{ app_dir }}/config"
- "{{ app_dir }}/.env"
dest: "{{ app_dir }}/backups/config-{{ ansible_date_time.date }}.tar.gz"
ansible -i inventory.ini crewai_servers \
-m systemd -a "name=crewai"
ansible -i inventory.ini crewai_servers \
-m shell -a "journalctl -u crewai --since '1 hour ago'"
ansible -i inventory.ini crewai_servers \
-m systemd -a "name=crewai state=restarted"
ansible -i inventory.ini crewai_servers \
-m shell -a "{{ app_dir }}/venv/bin/pip list | grep crewai"
See the main CrewAI Setup guide for more details.