如何使用yq交换YAML文件中对象数组内的capacityScaler值?
capacityScaler Values Between Two Backends in YAML Using yq To swap the capacityScaler values of the two backends in your YAML file, you can use yq's multi-assignment feature to directly exchange the values in one command. Here's how to do it:
Basic Command (Preview Output)
Run this to see the swapped result without modifying your original file:
yq e '(.backends[0].capacityScaler, .backends[1].capacityScaler) = (.backends[1].capacityScaler, .backends[0].capacityScaler)' https_backend.yaml
This expression works by assigning the second backend's capacityScaler value to the first, and the first's value to the second—all in a single step, no temporary variables needed.
In-Place File Edit
If you want to update the file directly instead of just previewing, add the -i (in-place) flag:
yq -i e '(.backends[0].capacityScaler, .backends[1].capacityScaler) = (.backends[1].capacityScaler, .backends[0].capacityScaler)' https_backend.yaml
Verify the Swap
Confirm the change worked using your original command to list the values:
yq e '.backends[].capacityScaler' https_backend.yaml
You’ll now see the output:
1.0 0.0
Note
This uses mikefarah/yq v4+ syntax (the version that uses yq e for evaluate mode), which matches the command you shared in your question. Older versions of yq use different syntax, but this will work with the setup you’re using.
内容的提问来源于stack exchange,提问作者SimpleNiko

