admin管理员组文章数量:1410697
I have a YAML file like this:
keys:
- &node0001 ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- &node0002 ageYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
creation_rules:
- path_regex: secrets.yaml$
key_groups:
- age:
- *node0001
- *node0002
Now I want to add a new entry to the keys array and to the creation_rules array. I can't figure out what yq
syntax to use to achieve this. So it becomes:
keys:
- &node0001 ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- &node0002 ageYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
- &node0003 ageZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
creation_rules:
- path_regex: secrets.yaml$
key_groups:
- age:
- *node0001
- *node0002
- *node0003
I've tried:
yq '.keys += "ageZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"' .sops.yaml
This adds a new entry to the keys array, but I don't know how to set the anchor. And:
yq '.creation_rules[0].key_groups[0].age += "node0003"' .sops.yaml
Also, this adds an entry to the list, but that entry should be an alias.
Any ideas how to achieve this? If all is possible with a single yq
call, that would even be better.
I have a YAML file like this:
keys:
- &node0001 ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- &node0002 ageYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
creation_rules:
- path_regex: secrets.yaml$
key_groups:
- age:
- *node0001
- *node0002
Now I want to add a new entry to the keys array and to the creation_rules array. I can't figure out what yq
syntax to use to achieve this. So it becomes:
keys:
- &node0001 ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- &node0002 ageYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
- &node0003 ageZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
creation_rules:
- path_regex: secrets.yaml$
key_groups:
- age:
- *node0001
- *node0002
- *node0003
I've tried:
yq '.keys += "ageZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"' .sops.yaml
This adds a new entry to the keys array, but I don't know how to set the anchor. And:
yq '.creation_rules[0].key_groups[0].age += "node0003"' .sops.yaml
Also, this adds an entry to the list, but that entry should be an alias.
Any ideas how to achieve this? If all is possible with a single yq
call, that would even be better.
1 Answer
Reset to default 1Using mikefarah/yq, have a look at Anchor and Alias Operators in the manual. As for your example, perform both of your +=
operations in one go using the pipe operator |
, then also set anchor
and alias
for the respective last items at .[-1]
.
.keys += "ageZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
| .creation_rules[0].key_groups[0].age += "node0003"
| .keys[-1] anchor = .creation_rules[0].key_groups[0].age[-1]
| .creation_rules[0].key_groups[0].age[-1] alias |= .
Alternatively, to make it somewhat simpler, deal with .keys
and its anchor
together first, then rely on that when moving to the alias
, combining it in a similar way.
.keys += ("ageZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" | . anchor = "node0003")
| .creation_rules[0].key_groups[0].age += ((.keys[-1] | anchor) | . alias |= .)
Both produce:
keys:
- &node0001 ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- &node0002 ageYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
- &node0003 ageZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
creation_rules:
- path_regex: secrets.yaml$
key_groups:
- age:
- *node0001
- *node0002
- *node0003
本文标签: Use yq to add to a YAML file with anchorsaliases and arraysStack Overflow
版权声明:本文标题:Use yq to add to a YAML file with anchors, aliases and arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744864182a2629250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论