As organizations grow, manually configuring and maintaining servers quickly becomes inefficient, error-prone, and unscalable. Ansible, a powerful open-source automation tool, allows sysadmins and DevOps engineers to automate server provisioning, configuration management, and application deployment across multiple nodes—making it ideal for managing a fleet of Linux servers.
In this article, you’ll learn how to deploy a server using Ansible and manage an infrastructure of 10 Linux servers with ease.
What Is Ansible?
Ansible is a suite of software tools that enables infrastructure as code. It is open-source and the suite includes software provisioning, configuration management, and application deployment functionality.Ansible is an agentless IT automation tool. It uses SSH to connect to remote machines and YAML-based playbooks to define automation tasks. With Ansible, you can:
- Install and configure software packages
- Enforce configuration consistency
- Perform updates across nodes
- Deploy applications
- Orchestrate complex workflows
Pre-requisites
To follow this guide, you need:
- A control node (the machine from which you run Ansible)
- 10 Linux servers (can be VMs or physical machines)
- SSH access from the control node to all servers
- Python installed on the target machines (most Linux distros have this by default)
1. Install Ansible on the Control Node
On Ubuntu / Debian Linux
# apt update sudo apt install ansible -y
Or for CentOS/RHEL
# yum install epel-release -y
# yum install ansible -y
Verify installation:
# ansible –version
2. Configure Your Inventory File
Ansible uses an inventory file to define which servers to manage. By default, this is located at
/etc/ansible/hosts
, but you can also create a custom one.
Create an inventory file
hosts.ini
[webservers] server1 ansible_host=192.168.1.101 server2 ansible_host=192.168.1.102 server3 ansible_host=192.168.1.103 [dbservers] server4 ansible_host=192.168.1.104 [all:vars] ansible_user=ansible_user ansible_ssh_private_key_file=~/.ssh/id_rsa
You can also use hostnames if DNS is configured.
3. Test Connectivity
# ansible -i hosts.ini all -m ping
If everything is set up correctly, you should see
pong
responses from all servers.
4. Write Your First Playbook (Provisioning Example)
Create a file called server_setup.yml:
—
– name: Provision and configure Linux servers
hosts: all
become: yes
tasks:– name: Update apt cache (Debian/Ubuntu)
apt:
update_cache: yes
when: ansible_os_family == "Debian"– name: Install common packages
package:
name:
– curl
– git
– htop
state: present– name: Ensure Nginx is installed on webservers
apt:
name: nginx
state: present
when: "'webservers' in group_names"– name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
when: "'webservers' in group_names"
5. Run the Playbook
# ansible-playbook -i hosts.ini server_setup.yml
Ansible will SSH into each server, execute the tasks, and return status messages.
6. Scaling to 10+ Servers
Managing a growing infrastructure is simple with Ansible:
- Add new servers to hosts.ini
- Reuse existing playbooks for setup
- Use roles to modularize configuration (e.g., webserver, database, monitoring)
- Schedule playbooks using cron or CI/CD pipelines (e.g., GitHub Actions, Jenkins)
7. Sample Ansible Project Structure
Organizing your Ansible project effectively is crucial for scalability and maintainability. Here's a sample recommended directory layout:
ansible-infra/
├── hosts.ini
├── server_setup.yml
├── roles/
│ ├── common/
│ │ ├── tasks/
│ │ │ └── main.yml
│ │ └── files/
│ └── webserver/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
└── group_vars/
└── all.yml
Key Components:
- ansible.cfg: Configuration file defining paths and settings.
- inventories/: Directory containing environment-specific inventory files.
- playbooks/: Directory for playbooks that define automation tasks.
- roles/: Directory for reusable roles, each with its own tasks and variables.
- requirements.yml: File listing external roles or collections.
For a detailed explanation of this structure, refer to the Ansible Documentation.
Visual Workflow (text) Diagram
To illustrate the workflow, here's a diagram depicting how Ansible interacts with your infrastructure:
+——————+ +——————+ +——————+
| Control Node | | Ansible | | Managed Nodes |
| (Your Machine) | | (Ansible Core) | | (10 Linux Servers)|
+——————+ +——————+ +——————+
| | |
| SSH | Execute Playbooks | Apply Configurations
| | |
+————————->+————————->+
Workflow Steps:
-
Control Node: You initiate commands from your local machine.
-
Ansible Core: Ansible connects via SSH to each managed node.
-
Managed Nodes: Ansible applies configurations as defined in your playbooks and roles.
This workflow ensures consistent and automated management of your infrastructure.
Best Practices
- Use roles: Organize playbooks into reusable roles
(ansible-galaxy init myrole) - Maintain version control: Keep playbooks and inventory in Git
- Encrypt secrets: Use ansible-vault for secure credentials
- Test in staging: Always validate changes before pushing to production
Conclusion
Ansible is a game-changer for managing Linux infrastructure. With a few simple playbooks, you can provision, configure, and maintain your servers consistently and securely.
For a 10-node infrastructure, it offers the perfect balance of simplicity and power—letting you scale efficiently without the overhead of agent-based solutions.