Ansible Essentials Installation & Configuration

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:

BASH
1python3 -m ensurepip --upgrade
2python3 -m pip install --upgrade pip
Click to expand and view more

Install Ansible:

BASH
1pip install ansible
Click to expand and view more

Verify the installation:

BASH
1ansible --version
Click to expand and view more

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

BASH
1ssh-keygen -t ed25519
Click to expand and view more

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

Copy your public key to a remote host

BASH
1ssh-copy-id user@your-server-ip
Click to expand and view more

Then test your connection:

BASH
1ssh user@your-server-ip
Click to expand and view more

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:

BASH
1homelab-ansible/
2├── ansible.cfg
3├── inventory/
4│   └── hosts.yml
5├── playbooks/
6│   └── install-htop.yml
7└── README.md
Click to expand and view more

Create the ansibile configuration file

The ansible.cfg file is typically located either in your home directory or in the /etc/ansible directory.

INI
1[defaults]
2inventory = ./inventory/hosts.yml
3host_key_checking = False
4retry_files_enabled = False
5timeout = 10
Click to expand and view more

This configuration:

Define Your Inventory

Create a static inventory file to list your homelab machines. This file is typically placed in your current working directory, alongside your playbooks and roles in the inventory folder

YAML
1all:
2  children:
3    homelab:
4      hosts:
5        server01:
6        server02:
Click to expand and view more

Replace server01 and server02 with actual IPs or hostnames.

Test SSH Connectivity

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

BASH
1ansible -m ping homelab
Click to expand and view more

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

First Playbook: Install htop

Let’s create a simple Ansible playbook to install a package (in this case, htop) on all your homelab servers. It’s a good practice to save your playbooks in a dedicated playbooks/ directory. If you don’t have this folder yet, you can create it.

Save the following content as playbooks/install-htop.yml:

install-htop.yml
1- name: Install htop on homelab servers
2  hosts: homelab
3  become: true
4  tasks:
5    - name: Ensure htop is installed
6      ansible.builtin.apt:
7        name: htop
8        state: present
Click to expand and view more

This playbook:

Run the Playbook

From your project root:

BASH
1ansible-playbook playbooks/install-htop.yml --ask-become-pass
Click to expand and view more

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

Recap

You’ve now:

What’s Next

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

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

Copyright Notice

Author: Sven van Ginkel

Link: https://svenvg.com/posts/ansible-essentials-installation-configuration/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut