Homelab Automation with Ansible and Docker
Automate your homelab deployment with Ansible. Learn to provision servers, deploy Docker containers, and manage your self-hosted infrastructure as code.
Posted by
Related reading
Private Cloud vs Public Cloud: When to Self-Host and When Not To
Understand when self-hosting makes sense and when public cloud is better. A practical guide to choosing between private cloud homelab and AWS/GCP/Azure services.
Homelab Documentation: Wiki.js vs BookStack for Self-Hosted Wikis
Document your homelab setup with self-hosted wikis. Compare Wiki.js and BookStack for creating searchable documentation of your infrastructure and procedures.
Docker Security Scanning: Trivy and Dockle for Safe Containers
Scan your Docker images for vulnerabilities before deploying. Learn to use Trivy and Dockle to secure your self-hosted homelab container infrastructure.

Why Automate Your Homelab?
Manual server configuration is time-consuming and error-prone. With Ansible, you define your infrastructure as code - repeatable, version-controlled, and self-documenting. Rebuild your entire homelab in minutes, not hours.
Ansible Basics
# Install Ansible
pip install ansible
# Create inventory file (hosts.yml)
all:
hosts:
homelab:
ansible_host: 192.168.1.100
ansible_user: admin
ansible_ssh_private_key_file: ~/.ssh/homelab_keyDocker Installation Playbook
# playbooks/setup-docker.yml
---
- name: Setup Docker on Homelab
hosts: homelab
become: yes
tasks:
- name: Install prerequisites
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
state: present
update_cache: yes
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu jammy stable
state: present
- name: Install Docker
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
- name: Add user to docker group
user:
name: "{{ ansible_user }}"
groups: docker
append: yesDeploy Docker Compose Stack
# playbooks/deploy-services.yml
---
- name: Deploy Homelab Services
hosts: homelab
tasks:
- name: Create homelab directory
file:
path: /opt/homelab
state: directory
mode: '0755'
- name: Copy docker-compose file
copy:
src: ../docker-compose.yml
dest: /opt/homelab/docker-compose.yml
- name: Copy environment file
copy:
src: ../secrets/.env
dest: /opt/homelab/.env
mode: '0600'
- name: Start Docker Compose stack
community.docker.docker_compose:
project_src: /opt/homelab
state: present
pull: yesRunning Playbooks
# Test connection ansible homelab -m ping # Run playbook ansible-playbook playbooks/setup-docker.yml # Deploy services ansible-playbook playbooks/deploy-services.yml # Update all containers ansible-playbook playbooks/update-containers.yml
