Post

Ansible Series Part 1 | Initial Setup

Getting started with Ansible — from installation to your first automated task.

Ansible Series Part 1 | Initial Setup

Ansible is a powerful automation tool that lets you manage your infrastructure using simple, repeatable playbooks. In this first part of the series, you’ll install Ansible, configure SSH access, and run your first task — laying the foundation for automating your homelab.

Install Ansible

Install Ansible on your local workstation or control node. In my case I use a my macbook as “control node”.

Ensure pip is available and up to date:

1
2
python3 -m ensurepip --upgrade
python3 -m pip install --upgrade pip

Install Ansible:

1
pip install ansible

Verify the installation:

1
ansible --version

Set Up SSH Key Access

Ansible connects to remote machines via SSH. To avoid typing passwords every time, set up SSH key authentication.

If you already use Tailscale to access your nodes, you can use Tailscale SSH instead of managing your own SSH keys.

Generate a key pair

1
ssh-keygen -t ed25519

Press enter to use the default file location. Leave the passphrase empty for automation.

Copy your public key to a remote host

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

Then test your connection:

1
ssh user@your-server-ip

If you’re logged in without a password prompt, you’re ready to automate.

Project Structure

Here’s a basic layout for organizing your Ansible project:

1
2
3
4
5
6
7
homelab-ansible/
├── ansible.cfg
├── inventory/
│   └── hosts.yml
├── playbooks/
│   └── install-htop.yml
└── README.md

Create ansible.cfg

1
2
3
4
5
[defaults]
inventory = ./inventory/hosts.yml
host_key_checking = False
retry_files_enabled = False
timeout = 10

This configuration:

  • Points to your inventory file
  • Disables SSH host key prompts
  • Improves reliability by disabling retry files and adding a timeout

Define Your Inventory

Create a static inventory file to list your homelab machines:

1
2
3
4
5
6
all:
  children:
    homelab:
      hosts:
        server01:
        server02:

Replace server01 and server02 with actual IPs or hostnames.

Test SSH Connectivity

Use Ansible’s ping module to confirm everything is working:

1
ansible -m ping homelab

Each machine should return pong if SSH and the inventory are set up correctly.

First Playbook: Install htop

Let’s write a simple playbook to install a package on all your homelab servers.

1
2
3
4
5
6
7
8
- name: Install htop on homelab servers
  hosts: homelab
  become: true
  tasks:
    - name: Ensure htop is installed
      ansible.builtin.apt:
        name: htop
        state: present

This playbook:

  • Connects to all hosts in the homelab group
  • Uses sudo (become: true)
  • Installs the htop package if it’s not already present

Run the Playbook

From your project root:

1
ansible-playbook playbooks/install-htop.yml --ask-become-pass

If your user has passwordless sudo, you can skip the --ask-become-pass flag.

Recap

You’ve now:

  • Installed Ansible
  • Set up SSH key access to your servers
  • Created a clean project structure
  • Written and executed a basic playbook

What’s Next

In the next parts of this series, we’ll cover:

  • Organizing your automation with roles
  • Reacting to changes using handlers
  • Managing secrets securely with Ansible Vault

You’re now ready to scale up your homelab automation. Let’s continue.

In the meantime check out my git repro that I use for my homelab

This post is licensed under CC BY 4.0 by the author.