Reputation: 7
I'm working on automation script to use variable from one yaml and replace in another yaml.
Requirement: i should load the yaml1 and create variables, and use those as variables in yaml2. I'm able to to create variables but not able to get logic for replace those obtained variables in yaml2. please help.
Sample yaml1
variable.yaml
environmentName: "dev1_westus2"
jumpbox: "dev1-westus2-jumpbox"
jumpboxDeploymentIndex: "0"
k8sClusterB:
kubeConfig: "dev1"
kubeContext: "admin"
namespace: "cp5"
nfsMountName: "nfsdev1westus2_cp5"
usego: "true"
sample values.yaml:
namespace: null
environmentName: null
protocol: https://
proxyErrorLog: /dev/stderr
adminAccessLog: /dev/stdout
adminErrorLog: /dev/stderr
serviceName: apiver
kubeConfig: null
kubeContext: null
replicas: 1
image:
repository: ubuntu
by taking above variable from variables.yaml and should replace the null values in values.yaml.
example:
it should take environmentname, namespace etc from variables.yaml and replace only the values with null value in vaules.yaml
required output:
namespace: cp5
environmentName: dev1_westus2
protocol: https://
proxyErrorLog: /dev/stderr
adminAccessLog: /dev/stdout
adminErrorLog: /dev/stderr
serviceName: apiver
kubeConfig: dev1
kubeContext: admin
replicas: 1
image:
repository: ubuntu
sample code:
import yaml
with open('variable.yaml') as info:
info_dict = yaml.safe_load(info)
print (info_dict)
for item, doc in info_dict.items():
print(item, ":", doc)
Thank you very much
Upvotes: 0
Views: 697
Reputation: 516
This will get you part of the way there. I see a problem with the variable.yaml, though - nested values (like kubeConfig) aren't terribly easy to access. How should the program know to look at k8sClusterB.kubeConfig to access the kubeConfig value?
#!/usr/bin/env python3
import yaml
with open('variable.yaml') as info:
info_dict = yaml.safe_load(info)
with open('values.yaml') as values_file:
values_dict = yaml.safe_load(values_file)
for key, value in values_dict.items():
if value == None:
values_dict[key] = info_dict.get(key)
print(yaml.dump(values_dict))
Upvotes: 2