Operations 7 min read

Master Ansible: Automate 300+ Servers with Simple Playbooks

This guide introduces Ansible’s core concepts, installation steps, common commands, and a complete Nginx deployment playbook, showing how to efficiently automate configuration, scaling, and updates across hundreds of servers.

Efficient Ops
Efficient Ops
Efficient Ops
Master Ansible: Automate 300+ Servers with Simple Playbooks

When it comes to boosting operational efficiency, Ansible is a must‑have tool; managing over 300 machines becomes straightforward with this 63.6k‑star automation platform, which can automate virtually any task.

Eliminate duplication and simplify workflows

Manage and maintain system configurations

Continuously deploy complex software

Perform zero‑downtime rolling updates

Ansible uses simple Playbook scripts (YAML) to declare the desired state of local or remote systems. Its design follows four principles:

Agentless architecture: No extra software is installed on target nodes, reducing maintenance overhead.

Simplicity: YAML syntax reads like documentation; Ansible connects via SSH using existing OS credentials.

Scalability and flexibility: Modular design supports many operating systems, cloud platforms, and network devices.

Idempotence and predictability: Re‑running a playbook leaves the system unchanged if the desired state is already met.

Basic Usage

Installation

pipx install --include-deps ansible

For a minimal installation, use the ansible-core package:

pipx install ansible-core

Core Concepts

Inventory : Defines target hosts, groups, and variables for play selection.

Playbook : YAML file containing plays; the basic execution unit of Ansible.

Roles : Reusable collections of tasks, handlers, variables, and plugins.

Task : Individual action; can be run ad‑hoc with ansible or ansible-console.

Modules : Code transferred to managed nodes to perform the operations defined in a task.

Common Commands

Test host reachability: ansible all -m ping Copy a local file to a remote host:

- copy:
    src: /local/file
    dest: /remote/file
    owner: root
    group: root
    mode: '0644'

Manage packages with yum:

- yum:
    name: nginx
    state: present

Use Case: Deploy Nginx Web Server

Create deploy_nginx.yaml with the following content:

---
- name: Deploy Nginx Web Server
  hosts: webservers  # defined in inventory
  become: yes          # use sudo
  tasks:
    - name: Install Nginx
      package:
        name: nginx
        state: present
    - name: Create web directory
      file:
        path: /var/www/html
        state: directory
        mode: '0755'
    - name: Deploy index.html
      copy:
        content: |-
          <!DOCTYPE html>
          <html>
          <head><title>Welcome to Ansible!</title></head>
          <body><h1>Hello from Ansible!</h1><p>This server is managed by Ansible.</p></body>
          </html>
        dest: /var/www/html/index.html
        mode: '0644'
    - name: Start and enable Nginx
      service:
        name: nginx
        state: started
        enabled: yes
    - name: Allow HTTP traffic (firewalld)
      firewalld:
        service: http
        permanent: yes
        state: enabled
      when: ansible_os_family == "RedHat"
    - name: Allow HTTP traffic (ufw)
      ufw:
        rule: allow
        port: "80"
        proto: tcp
      when: ansible_os_family == "Debian"

Define the inventory:

[webservers]
server1 ansible_host=192.168.1.10
server2 ansible_host=192.168.1.11

[webservers:vars]
ansible_user=root
ansible_ssh_private_key_file=~/.ssh/id_rsa

Execute the playbook:

ansible-playbook -i hosts.ini deploy_nginx.yml

Conclusion

Ansible’s strength lies in its simplicity and efficiency. Whether for bulk deployment, configuration management, or system updates, it enables low‑cost automation that empowers operations teams to replace manual server management with reliable, repeatable processes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Configuration ManagementOpsInfrastructure as CodeAnsiblePlaybook
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.