Ansible for the Homelab — Part 1: Installation and Your First Inventory
Before you can automate anything, you need Ansible installed and pointed at your machines. That's what this post covers — getting Ansible running and teaching it about your homelab.
Installing Ansible
Ansible runs on Linux or Mac. You only install it on the machine you'll be running commands from — your laptop, a management VM, whatever you use as your control node. Nothing gets installed on the machines you're managing.
On Mac:
brew install ansibleOn Ubuntu/Debian:
sudo apt update
sudo apt install ansibleVerify it worked:
ansible --versionYour First Inventory
An inventory is how Ansible knows about your machines. At its simplest it's just a list of hostnames or IP addresses. Create a file called inventory.yml:
yaml
all:
hosts:
myserver.lan:
anotherserver.lan:That's it. Ansible now knows about two machines.
Organizing with Groups
As your homelab grows you'll want to group machines by role. Here's a more realistic inventory:
yaml
all:
children:
tools_servers:
hosts:
tools.lan:
database_servers:
hosts:
postgres.lan:
monitoring_servers:
hosts:
kite.lan:
```
Groups let you run commands against a specific set of machines — all your database servers at once, for example.
**Testing Connectivity**
With your inventory in place, test that Ansible can reach your machines:
```
ansible all -i inventory.yml -m ping
```
A successful response looks like this:
```
tools.lan | SUCCESS => {
"ping": "pong"
}If it fails, check that SSH key authentication is set up between your control node and the target machines. Ansible uses SSH under the hood and expects to connect without a password prompt.
Setting Default Variables
Rather than passing -i inventory.yml every time, create an ansible.cfg file in your project directory:
ini
[defaults]
inventory = inventory.yml
remote_user = ansible
private_key_file = ~/.ssh/ansible_ed25519
```
Now you can just run:
```
ansible all -m pingWhat's Next
You have Ansible installed, an inventory of your machines, and a working connection. In Part 2 we'll write your first playbook — automating something real on one of your machines.