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.
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
<code>pipx install --include-deps ansible</code>For a minimal installation, use the
ansible-corepackage:
<code>pipx install ansible-core</code>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
ansibleor
ansible-console.
Modules : Code transferred to managed nodes to perform the operations defined in a task.
Common Commands
Test host reachability:
<code>ansible all -m ping</code>Copy a local file to a remote host:
<code>- copy:
src: /local/file
dest: /remote/file
owner: root
group: root
mode: '0644'</code>Manage packages with yum:
<code>- yum:
name: nginx
state: present</code>Use Case: Deploy Nginx Web Server
Create
deploy_nginx.yamlwith the following content:
<code>---
- 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"
</code>Define the inventory:
<code>[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</code>Execute the playbook:
<code>ansible-playbook -i hosts.ini deploy_nginx.yml</code>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.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.