yq
yq copied to clipboard
Merge two files with anchors and getting reference errors
Describe the bug
I`m trying to merge two documents and i got a error in reference anchors
Version of yq: v4.34.1 Operating system: mac Installed via: homebrew
Input Yaml default.yml
services:
kafka:
username: &kafka_username username
password: &kafka_password password
servers: &kafka_servers
- server
- server
- server
applications:
app01:
version: 1.0.0
variables:
kafka_servers: *kafka_servers
kafka_usernane: *kafka_username
kafka_password: *kafka_password
other.yml
applications:
app02:
version: 2.0.0
app03:
version: 1.0.0
variables:
kafka_servers: *kafka_servers
kafka_usernane: *kafka_username
kafka_password: *kafka_password
Command
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' defaut.yml other.yml
Actual behavior
Error: bad file 'other.yml': yaml: unknown anchor 'kafka_servers' referenced
Expected behavior
services:
kafka:
username: &kafka_username username
password: &kafka_password password
servers: &kafka_servers
- server
- server
- server
applications:
app01:
version: 1.0.0
variables:
kafka_servers: *kafka_servers
kafka_usernane: *kafka_username
kafka_password: *kafka_password
app02:
version: 2.0.0
variables:
kafka_servers: *kafka_servers
kafka_usernane: *kafka_username
kafka_password: *kafka_password
app03:
version: 1.0.0
variables:
kafka_servers: *kafka_servers
kafka_usernane: *kafka_username
kafka_password: *kafka_password
The issue is that in order to merge the two files, it needs to be able to parse them both. And atm, other.yaml is not a valid file - it references anchors that don't exist.
What you can do is concatenate the two documents together into a single file, separated by the document separator ---
services:
kafka:
username: &kafka_username username
password: &kafka_password password
servers: &kafka_servers
- server
- server
- server
applications:
app01:
version: 1.0.0
variables:
kafka_servers: *kafka_servers
kafka_usernane: *kafka_username
kafka_password: *kafka_password
---
applications:
app02:
version: 2.0.0
app03:
version: 1.0.0
variables:
kafka_servers: *kafka_servers
kafka_usernane: *kafka_username
kafka_password: *kafka_password
That is a valid yaml file. Now you can merge the documents together:
yq ea 'select(di==0) * select(di==1)' file.yaml