Extract JSON config variables from the Airflow UI

One way to configure Apache Airflow is with “Variables” that can be modified programmatically or via the Airflow UI.

You can import the variables from a JSON file, again via the API or by uploading the file in the UI. Sensibly, Airflow does not provide a way to export these variables back out to a JSON file.

However, if you find yourself in the situation where Airflow holds the correct values and you’re not confident you’ve got them stored consistently elsewhere, you might end up having to manually copy the variables back out of the Airflow UI.

This situation is not ideal, and is also labourious. The little JS snippet below at least makes it quicker to extract the variables from the Airflow UI to a JSON blob:

const config = {};
document.querySelectorAll('tbody > tr').forEach((row) => {
    const keyCell = row.querySelector('.col-key');
    const valueCell = row.querySelector('.col-val');
    config[keyCell.innerText.trim()] = valueCell.innerText.trim();
console.log(JSON.stringify(config, null, 4));

You can try chucking that in the browser console to see if it works.

Some caveats:

  • The Airflow UI might change and break this.
  • The password-type values get exported as aserisks, so you’ll still have to put those back into the JSON manually.
  • Boolean attributes will end up in the JSON as strings, which could potentially reverse their value if not fixed, e.g. "False".
  • You’re manually executing some JS code in the UI of what might be a production system, which is probably a bad idea.

Once you’ve got your variables back out, make sure to store them properly this time so you don’t have to do this again!

View post: Extract JSON config variables from the Airflow UI