Created
March 29, 2020 19:35
-
-
Save seocam/e0e6ff8e4b2b26884cda1ca046ec5921 to your computer and use it in GitHub Desktop.
Example for parsing distro info from json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"amazon2": { | |
"qcow": "amzn2-kvm-2.0.20190313-x86_64.xfs.gpt.qcow2", | |
"os_type": "linux", | |
"os_variant": "auto", | |
"image_url": "https://cdn.amazonlinux.com/os-images/2.0.20190313/kvm", | |
"disk_format": "qcow2", | |
"login_user": "ec2-user" | |
}, | |
"centos8": { | |
"qcow": "CentOS-8-GenericCloud-8.1.1911-20200113.3.x86_64.qcow2", | |
"os_variant": "centos8", | |
"image_url": "https://cloud.centos.org/centos/8/x86_64/images", | |
"disk_format": "qcow2", | |
"login_user": "centos" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
distro_from_json() { | |
local distro_name=$1 | |
local distros_file=$2 | |
# if $distro_name not defined in json | |
if ! cat $distros_file | jq -e ".${distro_name}" > /dev/null; then | |
QCOW= | |
OS_TYPE= | |
OS_VARIANT= | |
IMAGE_URL= | |
DISK_FORMAT= | |
LOGIN_USER= | |
return | |
fi | |
QCOW=$(cat $distros_file | jq ".${distro_name}.qcow") | |
OS_TYPE=$(cat $distros_file | jq ".${distro_name}.os_type") | |
OS_VARIANT=$(cat $distros_file | jq ".${distro_name}.os_variant") | |
IMAGE_URL=$(cat $distros_file | jq ".${distro_name}.image_url") | |
DISK_FORMAT=$(cat $distros_file | jq ".${distro_name}.disk_format") | |
LOGIN_USER=$(cat $distros_file | jq ".${distro_name}.login_user") | |
} | |
print_vars() { | |
echo "qcow: ${QCOW}" | |
echo "os_type: ${OS_TYPE}" | |
echo "os_variant: ${OS_VARIANT}" | |
echo "image_url: ${IMAGE_URL}" | |
echo "disk_format: ${DISK_FORMAT}" | |
echo "login_user: ${LOGIN_USER}" | |
echo -e "---------------------------\n" | |
} | |
echo "Vars for centos8:" | |
distro_from_json centos8 distros.json | |
print_vars | |
echo "Vars for amazon2:" | |
distro_from_json amazon2 distros.json | |
print_vars | |
echo "Vars for non-existing:" | |
distro_from_json centos7 distros.json | |
print_vars |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment