Automated Operations with Python: Practical Use of Ansible and SaltStack for Batch Management of Server Clusters!

It was three o’clock in the morning.

I was sitting in the office staring at the monitoring screen when over 200 servers suddenly experienced Redis connection issues. Manually fixing each one? Not a chance.

That feeling of despair was truly overwhelming.

01

Traditional operations are a nightmare like this. A configuration file needs to be changed 200 times, a software package needs to be installed 200 times, and if something goes wrong, you have to log into 200 machines to troubleshoot. It’s simply a physical task.

The first time I encountered automation tools was five years ago on a project. At that time, I used shell scripts for batch execution, but the script got stuck on the 50th server, and the remaining 150 did not execute successfully.

Later, I learned that such scenarios require professional tools.

Ansible and SaltStack are two powerful tools in this field. Both are developed based on Python and can easily manage hundreds or thousands of servers. But which one to choose?

This question troubled me for a long time.

02

Let’s start with Ansible. Its biggest feature is that it requires no client. As long as the target server has SSH enabled, it can be managed directly.

# Basic usage of Ansible
# The inventory file defines the list of servers
[webservers]
web1.example.com
web2.example.com
web3.example.com

[databases] 
db1.example.com
db2.example.com

It is indeed simple to use. A single command can execute operations on all servers:

# Batch execute commands
ansible webservers -m command -a "systemctl restart nginx"

# Batch install software
ansible all -m yum -a "name=htop state=present"

# Batch copy files
ansible webservers -m copy -a "src=/tmp/config.conf dest=/etc/nginx/"

My favorite feature of Ansible is the Playbook functionality. You can write complex deployment processes as YAML files:

---
- hosts: webservers
  become: yes
  tasks:
    - name: Install Nginx
      yum:
        name: nginx
        state: present
    - name: Start Nginx service
      systemd:
        name: nginx
        state: started
        enabled: yes
    - name: Deploy configuration file
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: Restart Nginx
  handlers:
    - name: Restart Nginx
      systemd:
        name: nginx
        state: restarted

Scripts written this way are highly readable and easy to maintain.

03

However, Ansible also has its drawbacks.

When the number of servers exceeds 500, the execution speed noticeably slows down. This is because it operates in a push mode, requiring individual connections to each server to execute commands.

Moreover, it lacks real-time capabilities. If I want to know the status change of a particular server immediately, Ansible cannot help.

At this point, SaltStack shows its advantages.

SaltStack adopts a client-server architecture. Each managed server must install the Salt-minion client, which then connects to the Salt-master server.

# Basic configuration of Salt
# Master configuration file /etc/salt/master
interface: 0.0.0.0
publish_port: 4505
ret_port: 4506

# Minion configuration file /etc/salt/minion  
master: salt-master.example.com
id: web-server-01

The command execution speed of SaltStack is indeed much faster:

# Batch execute commands (almost instant completion)
salt '*' cmd.run 'uptime'

# Execute by group
salt 'web*' service.restart nginx

# State management
salt '*' state.apply webserver

One of my most memorable experiences was deploying an application on 800 servers using SaltStack, which took less than 2 minutes. This would have been impossible with Ansible.

04

SaltStack’s state management feature is also very powerful. State files can be written in Python or YAML:

# /srv/salt/webserver.sls
nginx:
  pkg.installed:
    - name: nginx
  service.running:
    - enable: True
    - reload: True
    - require:
      - pkg: nginx

/etc/nginx/nginx.conf:
  file.managed:
    - source: salt://nginx/nginx.conf
    - user: root
    - group: root
    - mode: 644
    - require:
      - pkg: nginx

Even more impressive is that SaltStack supports event-driven architecture. It can monitor server status changes and automatically trigger corresponding actions.

However, SaltStack has a steeper learning curve. Its configuration is also relatively more complex.

05

So which one to choose?

My suggestion is as follows.

If your number of servers is under 100 and your team’s technical level is average, go with Ansible. It’s simple to use, has a low learning cost, and can generally meet your needs.

If you have a large number of servers and high real-time requirements, and your team has some technical background, choose SaltStack. It offers better performance and more powerful features.

Of course, it’s not an either-or situation. In my current project, I use Ansible for application deployment and SaltStack for configuration management and monitoring. Each plays to its strengths.

The most important thing is to actually try them out. Only by using them can you know which one is more suitable for your scenario.

Remember one thing: tools are just means; solving problems is the goal.

Leave a Comment