How to Set Up Basic Ansible for Server Management

Managing many servers can feel like trying to herd cats. Each one has its own setup, software, and tiny problems. That’s where Ansible helps.

Ansible is a tool that lets you control many servers from one computer. You write simple instructions, and Ansible makes sure each server follows them. I like using Ansible because it saves time, avoids mistakes, and works well even when your internet is slow or the moon is full.

If you’ve ever had to run the same command on five servers, one by one, Ansible will feel like a magic wand. Except it’s not magic—it’s just good automation.


What Is Ansible?

Ansible is a tool for automation. That means it helps your computer do tasks for you. You write a file called a playbook, which is a list of instructions. Ansible reads it and runs those tasks on your servers.

It’s a lot like cooking. If you have a recipe and the right ingredients, you can cook the same dish every time. Ansible makes your servers follow the recipe—without burning the toast.

Some new words to know:

  • Node: A server you want to control
  • Playbook: A file that contains tasks you want to run
  • Inventory: A list of servers Ansible will manage

Why Use Ansible?

I started using Ansible when I had to update software on ten servers. Doing it by hand was slow and boring. With Ansible, I wrote the steps once, then ran them on every server in seconds.

Here’s what I like about Ansible:

  • You don’t need to install anything on the remote servers
  • You use plain text files (YAML format)
  • It works over SSH, so it’s secure and simple

Compared to other tools like Puppet or Chef, Ansible is lighter and easier to learn. And yes, I know—“Puppet” sounds fun, but in this case, Ansible pulls the strings.


What You’ll Need

Let’s get started. To follow along, here’s what you’ll need:

  • A computer with Linux or macOS (or WSL if you use Windows)
  • At least one remote server you can connect to via SSH
  • sudo access on your main computer
  • Basic command line knowledge

I’ll be doing this on Ubuntu, but the steps are mostly the same on other Linux systems.


Step 1: Install Ansible

On Ubuntu or Debian, run:

sudo apt update
sudo apt install ansible -y

On CentOS, RHEL, or Fedora:

sudo dnf install ansible -y

To check if it installed:

ansible --version

If you see a version number, you’re good to go.


Step 2: Set Up SSH Access

Ansible uses SSH to connect to servers. That means you must be able to log in to the server from your computer without typing a password each time.

First, create an SSH key (if you don’t have one yet):

ssh-keygen

Press Enter to accept the default file path.

Now copy your key to the server:

ssh-copy-id user@your-server-ip

Replace user and your-server-ip with your real info.

Try logging in:

ssh user@your-server-ip

If you can log in without a password, Ansible will be happy.


Step 3: Create an Inventory File

Ansible needs to know which servers to manage. You give it a list, called an inventory.

Make a new folder:

mkdir ~/ansible-basic
cd ~/ansible-basic

Create a file named inventory.ini:

[webservers]
192.168.1.10
192.168.1.11

[dbservers]
192.168.1.12

This tells Ansible you have two groups: webservers and dbservers.


Step 4: Test Connection

Let’s see if Ansible can talk to the servers.

Run:

ansible all -i inventory.ini -m ping -u your_user

Change your_user to the username you use on the servers.

If you see “pong” replies, it means Ansible can connect. If not, check your SSH keys or usernames.


Step 5: Create Your First Playbook

Now the fun begins.

Make a file called setup.yaml:

---
- name: Basic server setup
  hosts: all
  become: yes

  tasks:
    - name: Update APT package cache
      apt:
        update_cache: yes

This playbook says:

  • Run tasks on all servers
  • Use sudo (that’s what become: yes means)
  • Run apt update to refresh the software list

To run the playbook:

ansible-playbook -i inventory.ini setup.yaml -u your_user

Ansible will connect to each server and update the packages. All at once. That’s the magic.


Some Common Tasks I Use

Here are three things I often do with Ansible:

  • Install packages
    - name: Install htop
      apt:
        name: htop
        state: present
    
  • Create a new user
    - name: Add deploy user
      user:
        name: deploy
        state: present
    
  • Start a service
    - name: Start nginx
      service:
        name: nginx
        state: started
    

Once you learn the pattern, it’s easy to build more playbooks.


A Few Tips From Me

Here are some things I learned along the way:

  • Keep your playbooks in a folder with clear names
  • Use comments (#) to explain what each task does
  • Test on one server before rolling out to all

And if something breaks, don’t panic. Just run the playbook again. Ansible is idempotent, which means it won’t mess things up by repeating the same steps.

Also, don’t forget to laugh when things go wrong. Last week I accidentally installed a web server on my database server. Oops. It served absolutely nothing — like a pizza place without cheese.


What Makes Ansible So Handy?

Let’s compare manual work to using Ansible:

Task Without Ansible With Ansible
Update packages on 5 servers SSH into each one by hand One playbook, one run
Add users Repeat same command One task, applied to all
Install NGINX Type it 5 times Just once in YAML

Using Ansible saves me time, cuts down mistakes, and keeps things consistent.


Quick Checklist

Before we wrap up, here’s a short checklist to keep you on track:

  • Install Ansible on your main computer
  • Set up SSH keys and test login
  • Create an inventory file
  • Write a simple playbook
  • Run it and enjoy the results

Final Thoughts

Learning Ansible changed how I manage servers. It made boring tasks simple, and scary tasks less scary.

You don’t have to be a sysadmin guru. If you can write short, clear instructions and run a few commands, you can use Ansible.

Think of it like teaching your servers how to cook dinner. You write the recipe once. They follow it every time. And nobody burns the rice.

So, are you ready to make your servers behave? Let Ansible do the heavy lifting while you sip your coffee.

Got questions or want me to show more playbook examples? I’m always happy to share. After all, sharing is con-fi-guring.

 

Leave a Reply