Using YAML instead of JSON
Created: 2025-11-25 Tue 20:22
yq is a lightweight and portable command-line YAML processor:
https://mikefarah.gitbook.io/yqyqyq tool can be used to convert a JSON file (or JSON coming
from STDIN via shell pipe) into YAML
$ yq r --prettyPrint add-reservation.json
service:
- dhcp6
command: reservation-add
arguments:
reservation:
subnet-id: 1
duid: 01:02:03:04:05:06:07:08:09:0A
ip-addresses:
- 2001:db8:1::1
hostname: foo.example.com
option-data:
- name: vendor-opts
data: "4491"
- name: tftp-servers
space: vendor-4491
data: 3000:1::234
yq also supports the reverse, converting YAML back to JSON
# yq r --prettyPrint -j add-reservation.yaml
{
"arguments": {
"reservation": {
"duid": "01:02:03:04:05:06:07:08:09:0A",
"hostname": "foo.example.com",
"ip-addresses": [
"2001:db8:1::1"
],
"option-data": [
{
"data": "4491",
"name": "vendor-opts"
},
{
"data": "3000:1::234",
"name": "tftp-servers",
"space": "vendor-4491"
}
],
"subnet-id": 1
}
},
"command": "reservation-add",
"service": [
"dhcp6"
]
}
kea-cmd can aid to convert YAML
command-files into JSON and send them to the DHCP server via the
Kea control channel
#!/bin/sh
curl -s -X POST -H "Content-Type: application/json" \
-d "$(yq r -j ${1})" http://127.0.0.1:8000/ | yq r -P -
Successful command
# kea-cmd add-reservation.yaml - result: 0 text: Host added.
Error case
# kea-cmd add-reservation.yaml - result: 1 text: specified reservation '2001:db8:1:cafe::1' is not matching the IPv6 subnet prefix '2001:db8:1::/64'
service:
- dhcp6
command: reservation-get-all
arguments:
subnet-id: 1
|
{
"service": [
"dhcp6"
],
"command": "reservation-get-all",
"arguments": {
"subnet-id": 1
}
}
|
#!/bin/sh # If YAML config is newer than JSON, exit and don't overwrite [ /etc/kea/kea-dhcp4.yaml -nt /etc/kea/kea-dhcp4.conf ]; %% exit # make sure the backup dir exists mkdir -p /etc/kea/backup # create a backup copy of the current JSON config cp /etc/kea/kea-dhcp4.yaml /etc/kea/backup/kea-dhcp4.yaml.$(date -I) # create a new JSON config from the YAML config yq -r --prettyPrint -o yaml /etc/kea/kea-dhcp4.conf > /etc/kea/kea-dhcp4.yaml
#!/bin/sh # If JSON config is newer than YAML, exit and don't overwrite [ /etc/kea/kea-dhcp4.conf -nt /etc/kea/kea-dhcp4.yaml ]; %% exit # make sure the backup dir exists mkdir -p /etc/kea/backup # create a backup copy of the current JSON config cp /etc/kea/kea-dhcp4.conf /etc/kea/backup/kea-dhcp4.conf.$(date -I) # create a new JSON config from the YAML config yq -r --prettyPrint -j /etc/kea/kea-dhcp4.yaml > /etc/kea/kea-dhcp4.conf
[Unit] Description=Kea DHCPv4 Service Documentation=man:kea-dhcp4(8) Wants=network-online.target After=network-online.target After=time-sync.target [Service] RuntimeDirectory=kea-dhcp4 ExecStartPre=/etc/kea/kea-yaml2json.sh ExecStart=/usr/sbin/kea-dhcp4 -c /etc/kea/kea-dhcp4.conf ExecStopPost=/etc/kea/kea-json2yaml.sh Restart=on-failure RestartSec=60s [Install] WantedBy=multi-user.target