In modern software engineering, automated operations have become a fundamental capability. Whether it is deploying applications, managing servers in bulk, performing daily operational tasks, or building CI/CD pipelines—automation is the core means to improve efficiency and reduce human errors.
Python has two heavyweight tools in the field of operations:
- • Ansible: A mainstream, agentless automation platform for operations
- • Fabric: A lightweight, SSH-based Python automation execution framework
They cover the complete scenarios from “bulk server management” to “task script orchestration,” suitable for enterprise-level DevOps workflows as well as personal projects or small team automation.
1. Why Choose Automation Tools in the Python Ecosystem
The advantages of Python in automated operations include:
- • Concise syntax, very suitable for writing task scripts
- • A wealth of mature libraries: paramiko, pexpect, fabric, ansible
- • An active ecosystem, comprehensive documentation, and a friendly learning curve
- • Naturally compatible with Linux/Unix environments
Compared to Shell, Python offers better maintainability; compared to Go/Java, Python is more flexible and has lower writing costs.
2. Ansible: The Cornerstone of Enterprise-Level Automated Operations
Ansible, maintained by Red Hat, is one of the most popular automation systems for operations, featuring the following characteristics:
1. Agentless
No need to install server-side programs on target servers; management can be done via SSH.
2. YAML-based Declarative Task Definition
Reduces logical code, making operational files clearer and easier to maintain.
3. Playbook: The Core of Automated Tasks
A Playbook is Ansible’s “script” used to define tasks for batch execution.
4. Rich Module Library
For example:
| Module | Function |
<span>yum</span> / <span>apt</span> |
Install packages |
<span>copy</span> |
Upload files |
<span>service</span> |
Start/stop services |
<span>command</span> |
Execute commands |
<span>docker_*</span> |
Docker management |
1. Basic Directory Structure of Ansible
inventory/ # Host file
playbooks/ # Playbooks
roles/ # Role modules
ansible.cfg
2. Example of Inventory Host Management
<span>inventory/hosts</span>:
[web]
192.168.1.10
192.168.1.11
[db]
192.168.1.21
3. A Simple Playbook Example
Used to install nginx and start the service:
---
- name: Install and start Nginx
hosts: web
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx service
service:
name: nginx
state: started
Execute:
ansible-playbook playbooks/nginx.yml
This entire process requires no login to the server, making it ideal for bulk management.
4. Roles: Best Practices for Large Projects
Roles are used for modular task management:
roles/
nginx/
tasks/
templates/
handlers/
This makes large automation projects more maintainable and reusable.
3. Fabric: A Lightweight Python Automation Tool
Fabric is more like a “high-level SSH tool” for executing remote commands, uploading files, and deploying applications, with syntax very close to Python.
Its positioning is:
Highly effective for small teams, small projects, and flexible deployment scenarios.
1. A Classic Fabric Script
<span>fabfile.py</span>:
from fabric import Connection, task
@task
def deploy(c):
conn = Connection("[email protected]")
# Pull code
conn.run("cd /var/www/app && git pull")
# Restart service
conn.sudo("systemctl restart app")
Execute the deployment command:
fab deploy
This is a simple “automation deployment tool”.
2. File Upload Example
conn.put("local.conf", "/etc/app/config.conf")
conn.sudo("systemctl restart app")
3. Parallel Execution (suitable for bulk servers)
from fabric import SerialGroup
for c in SerialGroup("host1", "host2", "host3"):
c.run("uptime")
The flexibility of Fabric and its Python style make it very suitable for:
- • Management of medium to small-scale servers
- • Deployment of monolithic or simple web projects
- • Automation scripts as a replacement for shell
4. Ansible vs Fabric: How to Choose?
| Feature | Ansible | Fabric |
| Agentless | Yes | Yes |
| Configuration Complexity | Somewhat high | Very simple |
| Suitable Scale | Large-scale servers, enterprise-level | Medium to small projects, lightweight deployment |
| Operation Method | Declarative (YAML) | Imperative (Python script) |
| Batch Execution | Strong | Moderate |
| Maintainability | High | Moderate |
| Learning Cost | Medium | Low |
In Summary:
- • Cluster, large-scale operations → Choose Ansible
- • Small teams, frequent deployments, flexible operations → Choose Fabric
5. Best Practices for Automated Operations
1. Use Git to Manage Operational Scripts
Maintain consistency and traceability.
2. Ensure Repeatability
All tasks can be run multiple times without side effects (idempotency), which Ansible supports natively.
3. Execute in Separate Environments for Development and Production
Avoid configuration conflicts.
4. Logging
Use logging or Ansible callback plugins to record execution logs.
5. Encrypt Sensitive Information
Use Ansible Vault or environment variables to store keys.
6. Automatically Trigger with CI/CD (e.g., Jenkins/GitLab CI)
Achieve true DevOps.
6. Conclusion
In the Python ecosystem, Ansible and Fabric together form a complete solution from “lightweight automation” to “enterprise-level operation orchestration”:
- • Ansible: Declarative, modular, suitable for large-scale operation scenarios
- • Fabric: Lightweight, highly flexible, very suitable for deployment scripts and small-scale automation
Mastering both gives you the key skills for modern DevOps workflows, including:
- • Automated deployment
- • Bulk server management
- • Application release process automation
- • Configuration management
- • Continuous delivery (CI/CD) integration
For engineers looking to develop in operations development (SRE), DevOps, or backend directions, these are essential skills.