How to Automate Wake-on-LAN with Ansible Semaphore on Raspberry Pi: A Step-by-Step Guide
In this blog, we’ll walk through setting up Ansible Semaphore on a Raspberry Pi, configuring it to send Wake-on-LAN (WOL) commands, and automating the task to run every minute using a cron job. Whether you're waking up your server or a device remotely, this guide will help you achieve that with ease using Ansible Semaphore’s web interface.
Prerequisites
- A Raspberry Pi running a Linux-based OS (such as Raspberry Pi OS).
- Docker and Docker Compose installed on your Raspberry Pi.
- Basic knowledge of Docker and Ansible.
- The
wakeonlan
utility installed either via Python or from source.
Step 1: Set Up Docker and Docker Compose on Raspberry Pi
Before we begin setting up Ansible Semaphore, you need to ensure Docker and Docker Compose are installed on your Raspberry Pi. Here's how:
-
Install Docker:
Open a terminal on your Raspberry Pi and run the following commands:curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh
-
Install Docker Compose:
Next, install Docker Compose:sudo apt-get install -y python3-pip sudo pip3 install docker-compose
-
Enable Docker to Start on Boot:
Ensure Docker starts automatically with the system:sudo systemctl enable docker sudo systemctl start docker
Step 2: Set Up Ansible Semaphore in Docker
Now, we’ll set up Ansible Semaphore, a web-based UI for managing and running Ansible playbooks, within a Docker container.
-
Create a
docker-compose.yml
File:
Create adocker-compose.yml
file to define the configuration for Ansible Semaphore:version: '3.7' services: semaphore: image: ansible/semaphore:latest container_name: semaphore restart: always network_mode: host volumes: - /path/to/semaphore/data:/var/lib/semaphore environment: - SEMAPHORE_DB_HOST=localhost - SEMAPHORE_DB_PORT=3306 - SEMAPHORE_DB_USER=root - SEMAPHORE_DB_PASSWORD=yourpassword ports: - "3000:3000"
-
Start Ansible Semaphore:
Run the following commands to start the container:docker-compose up -d
-
Access Semaphore UI:
Open a web browser and go tohttp://<your-pi-ip>:3000
to access the Semaphore UI. Create a user and set up the database.
Step 3: Install Wake-on-LAN Dependencies
Next, we’ll install the necessary dependencies for sending Wake-on-LAN packets.
-
Access the Docker Container Running Semaphore:
Enter the running container by executing:docker exec -it semaphore /bin/sh
-
Install Required Packages for Wake-on-LAN:
Since wakeonlan isn't available in Alpine's default repository, we'll install it from Python's
pip
.apk update apk add --no-cache build-base python3 python3-dev py3-pip pip3 install wakeonlan
Step 4: Create an Ansible Playbook to Send Wake-on-LAN Command
Create an Ansible playbook that sends a Wake-on-LAN packet to a target device using its MAC address.
Example playbook (wakeonlan.yml
):
---
- name: Send Wake-on-LAN command
hosts: localhost
gather_facts: no
tasks:
- name: Send Wake-on-LAN packet
ansible.builtin.command:
cmd: "python3 -c 'import wakeonlan; wakeonlan.send_magic_packet(\"{{ lookup(\"env\", \"SERVER_MAC\") }}\")'"
In this playbook:
lookup("env", "SERVER_MAC")
retrieves the MAC address of the device from an environment variable. You can set this variable in the Semaphore UI when configuring the playbook.
Step 5: Set Up Ansible Semaphore to Run the Playbook
-
Create a Project in Semaphore:
- Go to Projects in the Semaphore UI and click Create Project.
- Name it and associate it with your inventory file.
-
Create a Template for the Playbook:
- Under Templates, create a new template and select the
wakeonlan.yml
playbook. - Link it to the project you just created.
- Under Templates, create a new template and select the
-
Configure Environment Variables:
- Go to Variables in the Semaphore UI and add the environment variable
SERVER_MAC
with the MAC address of the target device.
- Go to Variables in the Semaphore UI and add the environment variable
Step 6: Schedule the Task to Run Every Minute Using Cron
To run the Wake-on-LAN task every minute, you can schedule it using Semaphore's Cron-like scheduling.
-
Create a Scheduled Task in Semaphore:
- Go to Schedules in the Semaphore UI and click Create Schedule.
- Choose the template you created for the Wake-on-LAN playbook.
- Set the schedule to run every minute by using the cron syntax:
* * * * *
.
Example Cron Expression:
* * * * *
-
Save the Schedule:
Click Save to enable the cron job. This will execute the Wake-on-LAN command every minute.
Step 7: Monitor and Troubleshoot
After setting everything up, you can monitor the task from the Semaphore UI. If there are any issues, you can:
- Check the Logs of the scheduled task to see if there were any errors.
- Ensure that the environment variable
SERVER_MAC
is correctly set. - Verify that the container running Semaphore has access to the network where the target device is located.
Conclusion
By following this guide, you’ve successfully set up Ansible Semaphore on your Raspberry Pi to send Wake-on-LAN commands, scheduled to run every minute using a cron job. This solution is great for automating the process of waking up servers or devices remotely. Semaphore’s web interface simplifies the process of managing and scheduling tasks, while Ansible handles the execution with precision.
If you encounter any issues or have further questions, feel free to reach out. Happy automating!!
Member discussion