Using Home Assistant to Automate Labs Part 2
Part 1
Part 3
Part 4
Note: You will need some familiarity with Linux and Home Assistant with this guide. While I will provide some guidance, it will not be a deep dive into either system.
Home Assistant Proxmox Integration
Let’s start with the Proxmox HACS Integration. It’s the easiest to do. In HA, go to Settings>Devices and Services>Add Integration and search for HACS and install.
(You will need to link HACS to an active GitHub account) All documentation for HACS can be found here.
HACS is a little different when it comes to installing custom integrations. Once installed, open HACS and choose “service” and then “visit.”
Choose integrations, and “explore and download repositories” and install the Proxmox integration. Instructions will be on the following page.
Once installed, go back to Settings>Devices and Services and enter the Proxmox integration and configure. Once done, add your VMs/Containers as needed.
Once added, the VM/Containers will be entities in HA and available for use. Unlike some other devices however the actions are not enabled by default. In the Proxmox integration, go into the “service” menu and your VMs will be listed. Click into the VM you want to change, and you’ll see the options are grayed out for “Controls”. Enable the controls you need. I used Start, Shutdown, and Stop. Shutdown is a orderly shutdown if supported by the OS, and stop is a hard shutdown.
Enabling these will provide an “entity ID” that corresponds to that action that we’ll need later.
E.G. “button.lxc_influxdb_125_start”
Repeat this action for all VMs you want to later Automate. Apart from controls, this also makes available a wealth of stats and information to query and observe. It’s no accident I have an influxdb container here. :) We’ll save that for another blog post.
Home Assistant PNET/EVE-NG Integration
Ok, this is where things get considerably more nerdy. HA and PNET/EVE-NG have absolutely no link or integration to one another. I suppose someone could do some API magic, but until recently I thought that acronym was a drunk trying to tell me what their favorite “IPA” was. So that’s not my route.
As there is no official path for PNET/EVE-NG we need to get old school and dive into the wonderful world or YAML! HA has most of its configuration in YAML and back in the early days of HA we spent a lot and I mean a LOT of time there. Nowadays many more things are in the GUI. But as this is not configurable on the front end, we need to add it via YAML. This is how we will add our integration into HA.
If you want a fun drinking game, take a shot every time I say “YAML”.
The YAML file in question is our old friend configuration.yaml. After reading this sentence the very next thing you should do is go to Settings>Add-Ons and install the Studio Code Server add-on. It makes life WAY easier. Far better to edit the config in-line with some basic error prevention/YAML integration.
Before we get much deeper there however we need to go down a different road first. We can work on YAML all day long but if HA can’t pass those commands to PNET/EVE-NG then we are pretty much wasting our time. First we need to get HA to authenticate to our lab server. The best way to do this is to make a key pair.
Long story short, you need the private key on HA, and the public key on
PNET/EVE-NG. We’ll use HA to generate our keys as it’s super simple.
Note: Make a snapshot or backup before modifying your HA install and delete when comfortable with your changes.
Key Pair Generation
On HA we need to make our keys directory:cd /config
mkdir .keys_ssh
then CD to /config/.keys_ssh
Note: I can’t remember if I had to change permissions on this directory, but it’s set to 0755.
Run ssh-keygen, it will prompt you to save to /root/.ssh/id_rsa,
but change that to
/config/.keys_ssh instead.
For whatever reason, this is the directory that HA will use. I advise to not set a passphrase for this use case.
Once generated, you will get a id_rsa private key, and a id_rsa.pub file. We will reference the private key later, but we need our public key for PNET/EVE-NG and whatever other endpoint we want to connect with.
Note: The public key will have a file extension of .pub and the private key will have no extension. The private key will not leave HA.
PNET/EVE-NG Changes
Edit the “authorized_keys” file and add the contents of the public key file. There will be entries already there, add to it.
nano /root/.ssh/authorized_keys
We can test this in HA from CLI. SSH in and run:
ssh -o StrictHostKeyChecking=no -i /config/.keys_ssh/id_rsa root@[ServerIP ]
(Note the reference to the private key in this string)
If everything is setup correctly it should log into your PNET/EVE-NG Server. Log out after a successful test.
If you have issues, these two commands can help in troubleshooting.
tail -F /var/log/auth.log
tail -F /var/log/syslog
That’s it for this section. In Part three, we’ll work on getting our YAML config going as well as explaining the command structure we’ll send to our nodes in PNET/EVE-NG.