switched to jq for parsing json responses
This commit is contained in:
14
Jenkinsfile
vendored
14
Jenkinsfile
vendored
@@ -108,21 +108,17 @@ pipeline {
|
|||||||
waitUntil {
|
waitUntil {
|
||||||
sleep(time: 10, unit: 'SECONDS')
|
sleep(time: 10, unit: 'SECONDS')
|
||||||
|
|
||||||
// Query OPNSense DHCP leases API
|
// Query OPNSense DHCP leases API and parse with jq
|
||||||
def response = sh(
|
vmIp = sh(
|
||||||
script: """
|
script: """
|
||||||
curl -s -k -u "\${OPNSENSE_KEY}:\${OPNSENSE_SECRET}" \
|
curl -s -k -u "\${OPNSENSE_KEY}:\${OPNSENSE_SECRET}" \
|
||||||
"https://${OPNSENSE_HOST}/api/dhcpv4/leases/searchLease"
|
"https://${OPNSENSE_HOST}/api/dhcpv4/leases/searchLease" | \
|
||||||
|
jq -r '.rows[] | select(.hostname == "${templateHost}" or .["client-hostname"] == "${templateHost}") | .address' | head -1
|
||||||
""",
|
""",
|
||||||
returnStdout: true
|
returnStdout: true
|
||||||
).trim()
|
).trim()
|
||||||
|
|
||||||
// Parse JSON and find the lease with matching hostname
|
if (vmIp) {
|
||||||
def leases = readJSON text: response
|
|
||||||
def matchingLease = leases.rows?.find { it.hostname == templateHost || it['client-hostname'] == templateHost }
|
|
||||||
|
|
||||||
if (matchingLease) {
|
|
||||||
vmIp = matchingLease.address
|
|
||||||
echo "Found IP address: ${vmIp} for hostname: ${templateHost}"
|
echo "Found IP address: ${vmIp} for hostname: ${templateHost}"
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user