DEV Community

Cover image for How to Install and Set Up Ansible on Ubuntu (First Playbook)
FlatlinerAtheist | CLI-NERDS
FlatlinerAtheist | CLI-NERDS

Posted on Originally published at clinerds.com

How to Install and Set Up Ansible on Ubuntu (First Playbook)

Ansible is the tool that turns "log into 20 servers and run the same commands" into "run one command once." It's agentless — nothing to install on the machines you manage, just SSH and Python, which they already have.

This guide gets you from a fresh Ubuntu install to your first working playbook.

Install Ansible on the control node

On Ubuntu, the distro package is fine for getting started:

sudo apt update
sudo apt install -y ansible
ansible --version
Enter fullscreen mode Exit fullscreen mode

For the newest release, use pip instead:

sudo apt install -y python3-pip
pip install ansible-core
Enter fullscreen mode Exit fullscreen mode

The machine running Ansible is the control node. Your servers only need SSH access and Python — nothing else.

Set up SSH access to your servers

Ansible talks to servers over SSH, so key-based login makes everything smoother:

ssh-copy-id user@192.168.1.50
Enter fullscreen mode Exit fullscreen mode

Repeat for each server. If you can ssh user@server without a password, Ansible can too. (Need help with keys? See our SSH private key guide.)

Create an inventory

The inventory lists the machines Ansible manages. Create a project folder and an inventory.ini:

[webservers]
web1 ansible_host=192.168.1.50 ansible_user=ubuntu
web2 ansible_host=192.168.1.51 ansible_user=ubuntu

[dbservers]
db1 ansible_host=192.168.1.60 ansible_user=ubuntu

[all:vars]
ansible_python_interpreter=/usr/bin/python3
Enter fullscreen mode Exit fullscreen mode

Groups let you target web servers and database servers separately later.

Test connectivity

ansible all -i inventory.ini -m ping
Enter fullscreen mode Exit fullscreen mode

Every server should answer "pong". If you get UNREACHABLE, it's SSH, DNS, or firewall — not Ansible. Fix the SSH connection first and Ansible will work.

Your first ad-hoc commands

ansible webservers -i inventory.ini -m command -a "uptime"
ansible all -i inventory.ini -m apt -a "name=htop state=present" --become
ansible all -i inventory.ini -m service -a "name=nginx state=started" --become
Enter fullscreen mode Exit fullscreen mode

--become is Ansible's sudo. Use it for anything that installs packages or changes system state.

Your first playbook

Playbooks are YAML files describing the state you want. setup.yml:

- hosts: webservers
  become: true
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
        update_cache: true

    - name: Make sure Nginx is running and enabled
      service:
        name: nginx
        state: started
        enabled: true

    - name: Allow HTTP through the firewall
      ufw:
        rule: allow
        port: "80"
        proto: tcp
Enter fullscreen mode Exit fullscreen mode

Run it:

ansible-playbook -i inventory.ini setup.yml
Enter fullscreen mode Exit fullscreen mode

The magic word is idempotent: run it ten times and it changes nothing after the first run. Nginx is already installed? Skipped. Already running? Skipped. You describe the end state, not the steps.

Where to go next

  • Roles — package tasks, templates, and handlers into reusable units once playbooks grow
  • Templates — push config files with per-server variables (server_name {{ inventory_hostname }})
  • Ansible Galaxy — thousands of community roles for Docker, PostgreSQL, and more: ansible-galaxy install geerlingguy.docker

Start by automating the boring task you did three times this week. That's usually all the motivation Ansible needs.

Top comments (0)