I have a YAML file as
# This is the group manages the permissions
group:
- state: present
group:
name: "Developers"
description: "The development team"
userNames:
- userl1 # <user1Name> - <User1email>
- userd2 # <user2Name> - <User2email>
- userr1 # <user3Name> - <User3email>
- state: present
group:
name: "Admins"
description: "The Admin team"
userNames:
- userl1 # <user1Name> - <User1email>
- userd2 # <user2Name> - <User2email>
I want to add a new user to developer whose id is usermd with comment in line # <UserName> - <Useremail>
similarly to the admin group too.
expected output as:
# This is the group manages the permissions
group:
- state: present
group:
name: "Developers"
description: "The development team"
userNames:
- userl1 # <user1Name> - <User1email>
- userd2 # <user2Name> - <User2email>
- userr1 # <user3Name> - <User3email>
- usermd # <usermdName, Surname> - <Usermdemail>
- state: present
group:
name: "Admins"
description: "The Admin team"
userNames:
- userl1 # <user1Name> - <User1email>
- userd2 # <user2Name> - <User2email>
The code I have used is
import sys
import ruamel.yaml
with open('Access.yaml', 'r') as f:
# Load the YAML data
yaml = ruamel.yaml.YAML(typ='rt')
data = yaml.load(f)
for x in data['jfrog']['groups']:
if not x["group"]["name"].endswith('Developers'):
continue
usr = x['group']['userNames']
usr.append('usermd')
yaml.dump(data, sys.stdout)
I am using a jfrog yaml so the jfrog is parent key for the repositories I haven't found a way to wrap the comment to the new user. but when I use this the output I got as
group:
name: Developers
description: The development group
autoJoin: false
userNames:
- userl1 # <user1Name> - <User1email>
- userd2 # <user2Name> - <User2email>
- userr1 # <user3Name> - <User3email>
- usermd
- state: present
# This is the Admins group need to be added
group:
name: Admins
description: The admin group
autoJoin: false
userNames:
- userl1 # <user1Name> - <User1email>
- userd2 # <user2Name> - <User2email>```
The sequence and its elements are loaded in a
CommentedSeqwhich behaves like a Python list, but also contains the comments. You can inspect its.caattribute to see what the comments on the existing lines look like and create an extra comment item for the added entry:which gives: