Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Install and Set Up Ansible

This guide shows how to install Ansible on a Linux machine and run your first command on another server. Installing software needs root access.

Web Hosting2 min read9 steps

What is Ansible?

Ansible is a free tool that runs the same tasks on many servers at once. Imagine you must update ten servers. Doing it by hand takes ages. With Ansible you write the task once and it does the work on all ten. It connects through SSH. SSH is a safe way to control a faraway server by typing commands.

The machine where you install Ansible is the control node. The servers it manages are called hosts.

Install Ansible on Ubuntu

  1. Log in to your control node with SSH.
  2. Update the package list:
    sudo apt update
  3. Install Ansible:
    sudo apt install ansible
    This command downloads Ansible and what it needs.
  4. Check it:
    ansible --version
    You should see a version number.

On CentOS or AlmaLinux, install the EPEL repository first, then run sudo yum install ansible. A repository is an online store of software.

Tell Ansible about your servers

  1. Open the inventory file. It is a list of your hosts:
    sudo nano /etc/ansible/hosts
  2. Add a group name and your server addresses. Use your own details:
    [webservers]
    server1 ansible_host=203.0.113.10 ansible_user=root
    The address here is only an example. Your real IP address is in your welcome email.
  3. Save with Ctrl+O, Enter, then exit with Ctrl+X.

Set up SSH keys

  1. Create a key pair:
    ssh-keygen
    Press Enter to accept the defaults.
  2. Copy the key to your server:
    ssh-copy-id root@203.0.113.10
    This lets Ansible log in without a password.

Run your first command

ansible webservers -m ping

This command asks every host in the webservers group to answer. A reply of pong means it works.

Next step: a playbook

A playbook is a file of tasks written in YAML, a simple text format. You run it with ansible-playbook file.yml.

Tip: Test playbooks on a spare server first. They can change many machines at once.

Quick recap

  • Ansible runs tasks on many servers.
  • Install it with sudo apt install ansible.
  • List your servers in /etc/ansible/hosts.
  • Use SSH keys for login.
  • Test with ansible webservers -m ping.