I have different yaml files which may have different nested structure
file1.yaml:
test3:
service1:
name1: |
"somedata"
name2: |
"somedata"
file2.yaml:
test1:
app1:
app2:|
"somedata"
app7:
key2: |
"testapp"
So as you can see structure of yaml files may be different.
The question is, may I somehow flexibly manage of appending some data to particular blocks of these files?
For example in file1 I want to write key vaue on the level of name1 and name 2 keys or service1:
test3:
service1:
name1: |
"somedata"
name2: |
"somedata"
my-appended-key:|
"my appended value"
my_second_appended_key: |
"my second appended valye"
and so on.
So the idea is to be able to specify under which nested block in yaml I want to append a data.
I have different yaml files which may have different nested structure
file1.yaml:
test3:
service1:
name1: |
"somedata"
name2: |
"somedata"
file2.yaml:
test1:
app1:
app2:|
"somedata"
app7:
key2: |
"testapp"
So as you can see structure of yaml files may be different.
The question is, may I somehow flexibly manage of appending some data to particular blocks of these files?
For example in file1 I want to write key vaue on the level of name1 and name 2 keys or service1:
test3:
service1:
name1: |
"somedata"
name2: |
"somedata"
my-appended-key:|
"my appended value"
my_second_appended_key: |
"my second appended valye"
and so on.
For now I do it for specific case regarding structure of yaml file.
Here is a part of my code:
import gnupg
import re
import argparse
def NewPillarFile():
with open(args.sensitive) as sensitive_data:
with open(args.encrypted, "w") as encrypted_result:
encrypted_result.write('#!yaml|gpg
secrets:
'+args.service+':
')
for line in sensitive_data:
encrypted_value = gpg.encrypt(re.sub(r'^( +?|[A-Za-z0-9]|[A-Za]|[0-9])+( +)?'+args.separator+'( +)?','',line,1), recipients=args.resident, always_trust=True)
if not encrypted_value.ok:
print(encrypted_value.status, '
', encrypted_value.stderr)
break
line = re.sub(r'^( +)?','',line)
encrypted_result.write(' '+re.sub(r'( +)?'+args.separator+'.*',': |',line))
encrypted_result.write(re.sub(re.compile(r'^', re.MULTILINE), ' ', encrypted_value.data.decode())+'
')
def ExistingPillarFile():
with open(args.sensitive) as sensitive_data:
with open(args.encrypted, "a") as encrypted_result:
encrypted_result.write(' '+args.service+':
')
for line in sensitive_data:
encrypted_value = gpg.encrypt(
re.sub(r'^( +?|[A-Za-z0-9]|[A-Za]|[0-9])+( +)?' + args.separator + '( +)?', '', line, 1),
recipients=args.resident, always_trust=True)
if not encrypted_value.ok:
print(encrypted_value.status, '
', encrypted_value.stderr)
break
line = re.sub(r'^( +)?', '', line)
encrypted_result.write(' ' + re.sub(r'( +)?' + args.separator + '.*', ': |', line))
encrypted_result.write(re.sub(re.compile(r'^', re.MULTILINE), ' ', encrypted_value.data.decode())+'
')
So the idea is to be able to specify under which nested block in yaml I want to append a data to make script more flexible.
See Question&Answers more detail:
os