Infrastructure as Code

How to Provision Dedicated Servers with Ansible: A Step-by-Step Guide

Managing dedicated servers manually is a time-consuming and error-prone process. Learn how to provision a raw bare-metal server with Ansible using inventories, YAML playbooks, and production-ready best practices.

> agentless > SSH-only > idempotent > YAML playbooks
control-node ~ provisioning
$ ansible-playbook -i inventory.ini provision.yml
PLAY [Provision Dedicated Web Server] ****************
TASK [Install Nginx web server] ......... changed
TASK [Configure UFW Firewall] ........... changed
dedicated-server-01 : ok=8 changed=6 failed=0 ✓
$

Whether you are deploying a single bare-metal server for high-performance computing or scaling a cluster of database nodes, Infrastructure as Code (IaC) is the industry standard for reliable deployments.

Ansible is the ideal configuration management tool for dedicated server provisioning. Because it is agentless and operates entirely over SSH, it requires no prior software installation on your target machines other than a standard Python environment. This tutorial covers the end-to-end process of provisioning a raw dedicated server using Ansible, incorporating production-ready best practices.

Prerequisites

Before writing any automation code, ensure you have the following components in place:

Control Node Where Ansible runs

A local machine or management server running Linux, macOS, or WSL (Windows Subsystem for Linux) where Ansible will be installed.

Target Server Your dedicated server

A freshly deployed bare-metal server running a Linux distribution (Ubuntu 24.04 or Debian 12 is used in this guide).

SSH Access Key-based login

Root or a sudo-privileged user access to the target server via SSH keys. Password-based SSH should ideally be disabled for security.

Install Ansible on Your Control Node

Ansible only needs to be installed on your control node. It will execute commands on your remote dedicated servers over standard SSH connections.

For Ubuntu/Debian control nodes, update your package manager and install Ansible:

Bash
sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y

For RHEL/CentOS/Rocky Linux:

Bash
sudo dnf install epel-release -y
sudo dnf install ansible -y

Verify the installation was successful by checking the version:

Bash
ansible --version

Define Your Infrastructure Inventory

Ansible needs to know which servers it is managing and how to connect to them. This is done using an inventory file. While you can use the global /etc/ansible/hosts file, creating a project-specific inventory file is a better practice for version control.

Create a file named inventory.ini in your project directory:

INI
[webservers]
dedicated-server-01 ansible_host=203.0.113.50 ansible_user=root

[databases]
dedicated-server-02 ansible_host=203.0.113.51 ansible_user=root

[all:vars]
ansible_ssh_private_key_file=~/.ssh/id_ed25519
  • Groups ([webservers]): Allows you to target specific types of servers.
  • Variables (ansible_host): Maps a friendly name to the server's public IPv4 or IPv6 address.
  • Global Variables ([all:vars]): Applies SSH keys or specific parameters to all groups.

Test the connection to your dedicated servers using the built-in ping module:

Bash
ansible all -i inventory.ini -m ping
Verification You should receive a "pong" response, confirming that your control node can successfully authenticate and communicate with the target servers.

Write the Provisioning Playbook

A playbook is a YAML file that declares the desired state of your servers. Ansible will read this playbook and execute the necessary steps to transition the server from its current state to your desired state.

Create a file named provision.yml. This playbook will secure the server, create a dedicated admin user, configure the firewall, and install a basic Nginx web server.

YAML
---
- name: Provision Dedicated Web Server
  hosts: webservers
  become: yes # Run commands with sudo/root privileges

  tasks:
    - name: Ensure system packages are fully updated
      apt:
        upgrade: dist
        update_cache: yes
      when: ansible_os_family == "Debian"

    - name: Create a non-root admin user
      user:
        name: sysadmin
        state: present
        shell: /bin/bash
        groups: sudo
        append: yes

    - name: Deploy SSH key for the new admin user
      authorized_key:
        user: sysadmin
        state: present
        key: "{{ lookup('file', '~/.ssh/id_ed25519.pub') }}"

    - name: Install Nginx web server
      apt:
        name: nginx
        state: present

    - name: Ensure Nginx is running and enabled on boot
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Configure UFW Firewall to allow SSH and HTTP
      ufw:
        rule: allow
        port: "{{ item }}"
        proto: tcp
      loop:
        - "22"
        - "80"
        - "443"

    - name: Enable UFW Firewall
      ufw:
        state: enabled
        policy: deny
Avoid Locking Yourself Out The final task enables UFW with a default deny policy. Always confirm that SSH (port 22) is allowed before this task runs, and test new firewall rules on a non-production server first.

Understanding the Playbook Mechanics

apt / service / user Idempotency

Ansible modules are idempotent. If you run this playbook multiple times, Ansible will only make changes if the server's current state does not match the playbook. If Nginx is already installed and running, Ansible does nothing.

loop Loops

The firewall configuration uses a loop to iterate through an array of ports, keeping the code DRY (Don't Repeat Yourself).

when Conditionals

The OS check ensures apt is only used on Debian-based systems, preventing failures if you mix OS distributions in the future.

Execute the Playbook

With your inventory and playbook ready, you can now provision the dedicated server.

Bash
ansible-playbook -i inventory.ini provision.yml

As the playbook runs, your terminal will display the status of each task. Tasks marked as ok required no changes, while tasks marked as changed indicate that Ansible modified the system.

Advanced Best Practices for Production

To scale your Ansible workflows beyond a single playbook, adopt the following production-grade practices:

  • Modularize with Ansible Roles: As your infrastructure grows, a single YAML file becomes unmaintainable. Break your playbook into Roles (e.g., common, nginx, mysql). Roles organize tasks, variables, and templates into a standardized directory structure, making your code reusable across different projects.
  • Protect Secrets with Ansible Vault: Never hardcode passwords, API keys, or database credentials in plain text. Use Ansible Vault to encrypt sensitive data files. You can encrypt a file by running ansible-vault encrypt secrets.yml and passing the decryption password at runtime using the --ask-vault-pass flag.
  • Use Handlers for Service Restarts: Instead of forcing a service to restart every time a playbook runs, use notify directives and Handlers. This ensures Nginx only reloads if its configuration file was actually modified during the run.
  • Version Control: Store your Ansible code (inventories, playbooks, roles) in a private Git repository. Treat your infrastructure configuration with the same rigor as application source code through Pull Requests and code reviews.

Ansible transforms the unpredictable nature of manual server administration into a documented, repeatable, and scalable workflow. By treating your dedicated infrastructure as code, you can provision bare-metal resources in minutes with total confidence in the resulting configuration.

Conclusion

Provisioning dedicated servers with Ansible makes routine Linux administration more consistent and repeatable. By combining SSH connectivity, inventory management, YAML playbooks, and reusable roles, administrators can automate many of the tasks involved in preparing physical servers for production workloads.

The example in this tutorial provides a starting point for configuring Ubuntu-based dedicated servers, installing essential packages, preparing administrative accounts, and deploying additional software.

As infrastructure requirements become more complex, the same approach can be extended to manage different server groups, application environments, monitoring systems, and deployment workflows.

The most effective automation strategy is one that is documented, version-controlled, tested, and regularly reviewed as infrastructure requirements change.

Frequently Asked Questions

Can Ansible provision physical dedicated servers?

Yes. Ansible can configure and manage physical dedicated servers after they have an accessible operating system, network connectivity, SSH access, and the necessary remote execution dependencies. Physical installation, hardware replacement, and initial operating system deployment generally require separate data-center or provisioning-system capabilities.

Does Ansible need to be installed on every dedicated server?

No. In a standard SSH-based setup, Ansible runs on the control node and connects to managed Linux systems remotely. Most standard Linux modules require Python on the managed node, but the Ansible application itself does not normally need to be installed there.

Can Ansible manage multiple dedicated servers simultaneously?

Yes. Ansible inventories allow administrators to organize multiple servers into groups and target them through a single playbook. Execution concurrency can be controlled using settings such as the forks configuration and playbook-level serial parameter.

Is Ansible suitable for production environments?

Yes. Ansible is used for configuration management and infrastructure automation in production environments. However, production workflows should include access controls, testing, credential protection, change reviews, and appropriate deployment procedures.

Can Ansible automate server security configuration?

Yes. It can manage user accounts, SSH-related configuration, package updates, firewall settings, file permissions, and other operating system security controls. These configurations should be tested carefully to avoid locking out administrators or disrupting production services.

What is the difference between Ansible and manual server configuration?

Manual configuration requires administrators to execute and verify tasks individually. Ansible uses reusable playbooks to apply defined configurations across selected machines, making repeated operations easier to standardize and audit.

Ready to automate?

Put Your Playbooks on Real Bare-Metal Power

Automation is only as good as the hardware underneath it. Deploy dedicated servers from Fit Servers with full root access, dedicated bandwidth, and SSH-ready provisioning, then let Ansible do the rest.

Explore Dedicated Servers →