DevOps13 min read•Updated November 2026

How to Automate VPS Server Setup with Ansible

Quick answer

Install Ansible on your computer (not the server), list your VPS in an inventory file, and write a playbook of tasks such as creating a sudo user with your SSH key, hardening SSH, enabling UFW with only the ports you need, turning on automatic security updates and installing Nginx. Run it with ansible-playbook -i inventory.ini site.yml. Tasks are idempotent, so running the playbook again changes only what has drifted. Keep secrets in Ansible Vault and the playbook in Git, and you can rebuild or clone any server in minutes.

Setting up a server by hand works once. The second time you forget a step; the third server ends up slightly different from the first. Ansible fixes this by describing the desired state of a server in plain YAML files that you run over SSH. Nothing needs to be installed on the server except Python, which Ubuntu and Debian already include. This guide builds a practical baseline playbook for a new VPS.

Why Ansible for a VPS?

  • Repeatable: new servers, staging copies and replacements are configured identically.
  • Documented: the playbook is the documentation of how the server is built.
  • Idempotent: running it twice is safe; it only changes what is not already correct.
  • Agentless: it uses SSH, so there is no extra daemon to secure on each server.
  • Recoverable: if a server is lost, rebuild it from the playbook and restore data from backups.

Step 1: Install Ansible on Your Computer

# macOS
brew install ansible
# Linux or WSL
python3 -m pip install --user ansible
ansible --version

Windows users run Ansible inside WSL, because the control node must be Unix-like.

Step 2: Inventory

# inventory.ini
[web]
web1 ansible_host=203.0.113.10

[staging]
stage1 ansible_host=203.0.113.20

[all:vars]
ansible_user=root
ansible_python_interpreter=/usr/bin/python3

The first run connects as root (or the provider's initial user). After the playbook creates your own user and disables root login, change ansible_user to that user and add --become for privileged tasks.

ansible all -i inventory.ini -m ping

Step 3: A Baseline Playbook

# site.yml
- name: Baseline for every VPS
  hosts: all
  become: true
  vars:
    admin_user: deploy
    admin_key: "{{ lookup('file', '~/.ssh/id_ed25519.pub') }}"
    allowed_ports: ["80", "443"]

  tasks:
    - name: Update apt cache and upgrade packages
      ansible.builtin.apt:
        update_cache: true
        upgrade: dist
        cache_valid_time: 3600

    - name: Install base packages
      ansible.builtin.apt:
        name: [ufw, unattended-upgrades, fail2ban, curl, htop]
        state: present

    - name: Create admin user
      ansible.builtin.user:
        name: "{{ admin_user }}"
        groups: sudo
        append: true
        shell: /bin/bash

    - name: Authorise SSH key
      ansible.posix.authorized_key:
        user: "{{ admin_user }}"
        key: "{{ admin_key }}"

    - name: Harden SSH
      ansible.builtin.copy:
        dest: /etc/ssh/sshd_config.d/00-hardening.conf
        content: |
          PasswordAuthentication no
          KbdInteractiveAuthentication no
          PermitRootLogin no
        mode: "0644"
      notify: Restart ssh

    - name: Allow SSH through the firewall
      community.general.ufw:
        rule: limit
        name: OpenSSH

    - name: Allow web ports
      community.general.ufw:
        rule: allow
        port: "{{ item }}"
        proto: tcp
      loop: "{{ allowed_ports }}"

    - name: Enable firewall with deny-by-default
      community.general.ufw:
        state: enabled
        policy: deny
        direction: incoming

    - name: Enable automatic security updates
      ansible.builtin.copy:
        dest: /etc/apt/apt.conf.d/20auto-upgrades
        content: |
          APT::Periodic::Update-Package-Lists "1";
          APT::Periodic::Unattended-Upgrade "1";
        mode: "0644"

  handlers:
    - name: Restart ssh
      ansible.builtin.service:
        name: ssh
        state: restarted

The SSH drop-in file name starts with 00- because OpenSSH uses the first value it reads for most options (see our SSH key guide). The firewall rule for SSH is added before the firewall is enabled, so you cannot lock yourself out (see our UFW guide). The ansible.posix and community.general collections are included in the full Ansible package; with ansible-core only, install them with ansible-galaxy collection install.

Step 4: Run It Safely

# preview what would change
ansible-playbook -i inventory.ini site.yml --check --diff
# apply
ansible-playbook -i inventory.ini site.yml
# limit to one server
ansible-playbook -i inventory.ini site.yml --limit stage1

Keep a second SSH session open the first time you apply SSH or firewall changes, and test logging in as the new user before closing it. After the first run, set ansible_user=deploy in the inventory.

Step 5: Organise with Roles

As playbooks grow, split them into roles, each with its own tasks, handlers, templates and default variables:

roles/
  common/      # users, SSH, firewall, updates
  nginx/       # web server and site templates
  app/         # application user, systemd unit, deploys
site.yml       # - hosts: web
               #   roles: [common, nginx, app]

Templates (Jinja2, .j2 files) let one Nginx site template serve production and staging with different variables such as server_name.

Step 6: Secrets with Ansible Vault

ansible-vault create group_vars/web/vault.yml
# inside:
vault_db_password: "a-long-random-password"

ansible-playbook -i inventory.ini site.yml --ask-vault-pass

Reference {{ vault_db_password }} in tasks and templates. The encrypted file is safe to commit; keep the vault password in a password manager. Mark tasks that handle secrets with no_log: true so values do not appear in output.

Ad-Hoc Commands for Everyday Tasks

Not everything needs a playbook. Ad-hoc commands run one module across many servers at once:

ansible web -i inventory.ini -m ansible.builtin.command -a "uptime"
ansible all -i inventory.ini --become -m ansible.builtin.apt -a "upgrade=dist update_cache=true"
ansible web -i inventory.ini --become -m ansible.builtin.service -a "name=nginx state=reloaded"

They are useful for quick checks and emergency patching; anything you repeat should become a playbook task.

Good Practices

  • Use modules (apt, copy, template, service) instead of shell commands, so tasks stay idempotent.
  • Run with --check --diff before applying changes to production.
  • Keep playbooks in Git and review changes like code.
  • Test new playbooks on a staging VPS first (see our staging guide).
  • Re-run the playbook regularly to correct drift from manual changes.

SkyRainCloud Plans for This Workload

Ansible itself needs no resources on the servers beyond SSH and Python, so it works with every plan. Use it to rebuild any tier identically, or to keep a fleet of entry 2 vCore / 8 GB servers configured the same way.

AMD EPYC 7C13 VPS

Dedicated KVM vCores on AMD EPYC 7C13 (up to 3.70 GHz) with DDR4 ECC memory, NVMe SSD and an unmetered 1 Gbps port.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — AMD28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthWebsites, small APIs, bots, staging and VPN serversOrder
VPS 2 — AMD416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,299/monthWooCommerce, Node.js/Python backends, Docker Compose stacks, CI runnersOrder
VPS 3 — AMD832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,499/monthProduction databases, high-traffic sites, multi-container appsOrder
VPS 4 — AMD1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,899/monthLarge production workloads, SaaS platforms, analytics, in-memory cachesOrder

Intel Xeon Platinum 8269CY VPS

KVM VPS on Intel Xeon Platinum 8269CY (up to 3.80 GHz Turbo) with DDR4 ECC memory, NVMe SSD, full root access and an unmetered 1 Gbps port.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — Intel28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthBusiness websites, small apps, scheduled jobs and botsOrder
VPS 2 — Intel416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,169/monthBusiness software, mid-size databases, several sites on one serverCheck stock
VPS 3 — Intel832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,249/monthERP/CRM stacks, heavier databases, multiple production appsCheck stock
VPS 4 — Intel1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,299/monthHigh-memory workloads and consolidated production serversCheck stock

Prices are monthly in INR as listed on the SkyRainCloud store (verified 2026-11-14). Stock and final pricing are shown on the product page at checkout.

Frequently Asked Questions

Does Ansible need to be installed on the VPS?
No. Ansible runs on your computer and connects over SSH. The server only needs Python, which Ubuntu and Debian include by default.
Can I run Ansible from Windows?
Use Windows Subsystem for Linux (WSL). The Ansible control node must run on a Unix-like system.
What does idempotent mean in Ansible?
Running the same playbook again produces the same result and only changes what has drifted, so repeated runs are safe.
How do I avoid locking myself out with SSH or firewall tasks?
Allow SSH in the firewall before enabling it, test with --check, keep a second SSH session open and confirm the new user can log in before closing it.
How do I store passwords in Ansible?
Encrypt them with Ansible Vault, reference them as variables, and use no_log: true on tasks that handle them.
Ansible or shell scripts?
Shell scripts work for one-off setups. Ansible's modules are idempotent and its roles, templates and inventories scale better to several servers and environments.
Ansible or Terraform?
They complement each other: Terraform creates infrastructure such as servers and DNS records through provider APIs; Ansible configures what runs on the servers.
Can Ansible deploy my application?
Yes. A deploy role can pull code or images, render configuration, run migrations and restart services, though many teams deploy from CI instead.
How do I preview changes?
Run ansible-playbook --check --diff to see what would change without applying it.
Which SkyRainCloud plans work with Ansible?
All of them. Ansible only needs SSH access and Python, so you can configure any AMD EPYC or Intel VPS tier identically.
Instant UPI • ₹649/mo
Fast KVM Cloud Provisioning