Use yq to add to a YAML file with anchors, aliases and arrays - Stack Overflow

I have a YAML file like this:keys:- &node0001 ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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.

Share Improve this question asked Mar 9 at 17:39 Martijn de MunnikMartijn de Munnik 98612 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Using 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744864182a4597882.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信