Experimenting with different distros and container projects is the biggest draw of home labs. But over time, you might get burned out by manually installing packages and libraries on your virtual machines whenever you want to work on a new project.

Thankfully, you can leverage the powerful automation facilities of Ansible to get rid of the more tedious aspects of your workstation. That said, Ansible can seem rather complex if you’re not familiar with YAML scripting. So, I’ve put together a guide to help you get an Ansible control center up and running on your home lab.

👁 Backing up a Proxmox machine
5 things you should do to ensure your home lab survives your experiments

If you love working on complex projects, these five tips can make your precious home server more experimentation-proof

What’s Ansible, anyway?

And why should you run it?

As one of the most popular automation tools in the server ecosystem, Ansible is designed to help you configure every aspect of your virtual machines, containers, and even hypervisor nodes from a central system. Rather than fiddling with individual systems, Ansible lets you execute multiple commands for as many systems as you desire. These commands range from auto-deploying VMs and containers on your home server to installing packages for them, editing their config files, and backing them up on your NAS. You also don’t need to install Ansible on the other systems, as the control node can execute all the commands remotely over the SSH protocol.

At its core, Ansible relies on Playbooks, .yml files that include all the commands Ansible needs to perform operations on your home lab equipment. Yes, equipment, as you can even use Ansible to modify your network stack in addition to your virtual guests. Likewise, Ansible also needs an inventory file containing the IP addresses or hostnames of your virtual guests to execute the SSH commands.

👁 A person holding an X99 server motherboard with two Intel Xeon CPUs slotted in
5 of the coolest services you can host on your home lab

If media servers and ad-blockers sound too cliche, here are some unique apps you can run on your home server

Setting up Ansible

With the theory part out of the way, it’s time to deploy an Ansible control node on your home lab. There are a couple of ways to do so, including creating a virtual Python environment and using it to set up the Ansible server. But since it’s a beginner’s guide, you can configure Ansible inside a Debian virtual machine with a couple of commands.

  1. Launch the terminal app included with your Linux distro.
  2. Use the apt repository to grab the Ansible package.
    sudo apt install ansible -y
  3. Likewise, you’ll need the sshpass utility set up on your control center to establish an SSH connection and run commands on the virtual guests.
    sudo apt install sshpass -y
  4. Technically, Ansible mandates you to use SSH fingerprints to access your machines, but you can disable this extra step with this command:
    export ANSIBLE_HOST_KEY_CHECKING=False
  5. For the virtual hosts, you’ll have to ensure the Ansible control node can connect to them by running the ssh command on the latter system:
    ssh username@IP_Address
  6. In case the client machine is unreachable, you’ll have to install OpenSSH on them with this command:
    sudo apt install openssh-server -y
  7. Be sure to enable the SSH service with the systemctl command:
    sudo systemctl enable ssh --now 

While you can use the nano text editor built into most distros, I recommend installing the all-powerful VS Code to create the config files. If you’re having trouble getting it to run, feel free to check out our beginner’s guide to setting up VS Code on Linux.

Executing commands using Ansible

Once you’ve got all the tools and services set up, it’s time to put on your coding hat and dive into Ansible coding.

  1. Launch the VS Code app on your Ansible control node.
  2. Create a new inventory file called hosts with the following syntax:
    1. [group-name-for-hosts]
    2. IP_address/hostname_of_server
      As you may have guessed, the group name (the string between the square parenthesis) is customizable, and you can set it to any string you like. I’ve included the IPv4 address of my Ubuntu VM in the screenshot, but you can add multiple hostnames or IP addresses under a single group name. Likewise, you can also create multiple groups to better organize your virtual guests and devices.
  3. Next, create a Playbook called tasks.yaml. To keep things simple, I’ve gone over the steps to automate GIMP installation on a remote VM, but there’s a lot more you can pull off with Ansible Playbooks. The overall syntax looks something like this:
    • ---
    • - hosts: all
    •  become: true
    •  tasks:
    •  - name: Gimp setup
    •  apt:
    •  name: gimp
    •  state: latest
    •  update_cache: true
      If you want to automate a Fedora system or an Arch Linux guest, remember to replace the apt keyword under the name header with the yum or arch repository. You can also add more tasks under the same header by using another name tag.

      Remember to use the proper YAML indentation, otherwise, Ansible will throw an error when you try executing the Playbook. You can refer to the screenshot above to get a better estimation of the spacing between the different headers.

  4. You can execute this Playbook by switching to the terminal and running this command:
    ansible-playbook /path_to_yml_file/tasks.yml --user username_of_virtual_guest --ask-pass --ask-become-pass -i /path_to_hosts_file/hosts
    The --ask-pass string will prompt you to enter the SSH password, while the --ask-become-pass needs the password of the root user.

Stay on top of your home lab automation with Ansible

Assuming you followed all the steps correctly, Ansible will run the Playbook and install GIMP on your remote machine. If you try to run the same Playbook again, Ansible won’t perform any actions because of its idempotent nature. In simple terms, Ansible will detect the final state ( in this case, a successful Gimp installation) has already been reached and won't execute the Playbook. If you found Ansible intriguing, I recommend checking out the official documentation, as it has several useful modules you can integrate into your home lab workflow.