The below value of private_localaddress needs to passed to "range_start" in yaml file. how to do this ?
Here is the script:
#!/bin/bash
MAC=`curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/`
myarr=($MAC)
for val in ${myarr[@]}; do
interfaceindex=`curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$val/device-number`
if [ $interfaceindex == 2 ];
then
private_localaddress=`curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$val/local-ipv4s`
echo $private_localaddress
fi
done
Here is the YAML file :
---
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
name: worker-private-eth2
spec:
config: '{
"cniVersion": "0.3.1",
"ipam": {
"type": "whereabouts",
"range": "10.30.11.0/25",
"range_start": $private_localaddress,
"range_end": "10.30.11.127",
"routes": [
{ "dst": "10.93.123.0/24", "gw": "10.30.11.1" }
],
Ffffff
CodePudding user response:
Perhaps the simplest would be to make private_localaddress
an environment variable and use envsubst to apply the changes to your YAML.
However to end up with correct YAML code, you need to ensure that you have a quoted string to the right of the colon. The best would be if you can already ensure that your YAML is written as
"range_start": "$private_localaddress",
If this is not feasible, you have to add the quotes when creating the variable, i.e.
export private_localaddress=\"$(curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$val/local-ipv4s)\"
CodePudding user response:
Use a proper yaml parser. Here are some examples:
Using mikefarah/yq:
value="$private_localaddress" yq \
'.spec.config |= (fromjson | .ipam.range_start = strenv(value) | tojson)'
Using kislyuk/yq:
yq -y --arg value "$private_localaddress" \
'.spec.config |= (fromjson | .ipam.range_start = $value | tojson)'
Using itchyny/gojq:
gojq --arg value "$private_localaddress" --yaml-input --yaml-output \
'.spec.config |= (fromjson | .ipam.range_start = $value | tojson)'