Introduction – What is Electricity Maps?
Understanding the carbon footprint of electricity consumption is essential for driving real climate action.
Electricity Maps provides a powerful platform that visualizes the real-time carbon intensity of electricity around the world. By leveraging geolocation and live data, it helps individuals and organizations make more informed, sustainable energy choices.
In this post, I’ll introduce an example of scheduling daily maintenance jobs by Ansible with Electricity Maps.
In IT operations, routine maintenance jobs play a crucial role in keeping systems stable and secure. Common scheduled tasks include log rotation and cleanup, disk space monitoring, regular backups, system updates, and security scans, and so on. These tasks are often automated using tools like cron, Ansible, or enterprise job schedulers to ensure consistent and efficient system upkeep.
However, some of those jobs’ such as log rotation, temporary file cleanup, or daily backups are often flexible for execution timing as long as they run regulary. For such jobs, we can choose the execution timing to reduce carbon emissions based on the carbon intensity in a location where the jobs will be executed.
Electricy Maps provides APIs so that we can obtain the data to inspect the carbon intencity of the region where the maintenance job will be executed. So, let take a look of an example.
Running Scheduled Maintenance Jobs with Ansible based on carbon intensity data from Electricity Maps
Here is an example of Ansible Playbook.
---
- name: Carbon-Aware Backup Job Execution
hosts: localhost
gather_facts: no
vars:
region_zone: "JP-TK" #Japan, Tokyo
carbon_intensity_threshold: 200
api_token: "{{ lookup('env', 'ELECTRICITY_MAPS_TOKEN') }}"
backup_dest: /path/to/backup_dest
backup_filename: /path/to/backup_file
backup_source: /path/to/backup_source
tasks:
- name: Get current carbon intensity from Electricity Maps
ansible.builtin.uri:
url: "{{ 'https://api.electricitymap.org/v3/carbon-intensity/latest?zone=' + region_zone }}"
method: GET
headers:
auth-token: "{{ api_token }}"
register: carbon_data
- name: Set carbon intensity fact
set_fact:
current_intensity: "{{ (carbon_data.json.carbonIntensity | default(0)) | int }}"
- name: Print current carbon intensity
debug:
msg: "Current carbon intensity in {{ region_zone }} is {{ current_intensity }} gCO₂eq/kWh."
- name: Ensure backup destination directory exists
file:
path: "{{ backup_dest }}"
state: directory
mode: '0755'
- name: Create backup archive of the source directory
archive:
path: "{{ backup_source }}"
dest: "{{ backup_dest }}/{{ backup_filename }}"
format: gz
mode: '0644'
when: (current_intensity | int) <= carbon_intensity_threshold
- name: Skip job and show a message if grid is not clean
when: (current_intensity | int) > carbon_intensity_threshold
debug:
msg: "Skipping job. Carbon intensity {{ current_intensity }} is above the threshold {{ carbon_intensity_threshold }}."
This Ansible Playbook demonstrates how to execute a carbon-aware backup job by integrating real-time data from Electricity Maps. The goal is to run the backup only when the electricity grid’s carbon intensity in a specific region—Tokyo in this case—is below a defined threshold, making the operation more environmentally friendly.
Here’s how it works:
- Carbon Data Retrieval: The playbook queries the Electricity Maps API for the current carbon intensity in the
JP-TK
(Japan, Tokyo) zone using a personal API token. - Dynamic Decision Making: It sets the returned value as a fact and compares it against a threshold (e.g., 200 gCO₂eq/kWh).
- Conditional Execution:
- If the carbon intensity is below or equal to the threshold, the backup proceeds by creating a compressed archive of the specified source directory.
- If the intensity is too high, the job is skipped, and a message is printed to inform the user.
This approach helps system administrators incorporate sustainability into routine IT tasks, using real-time environmental data to reduce emissions where possible. While this example only compresses a file system, running backup jobs with large data volumes during periods of low carbon intensity can help reduce emissions from IT operations.
Conclusion
By integrating real-time carbon intensity data into routine IT operations, we can take small but meaningful steps toward more sustainable computing. Using tools like Ansible and Electricity Maps enables us to make smarter, carbon-aware decisions with minimal effort.