Let run the script and enjoy your yaml file ^^. The for loop will get the service infor and write the servive to file correponding with the servicename (SVC-servicename.yaml). import yaml configfile yaml.safeload(open('config.yaml', 'rb')) projectnameconfigfile.get('projectname') Creating more complex Python config files This method can be of course expanded to use more complex YAML data structures and multiple Python files, further improving your code quality and speed of development. import yaml from jinja2 import Environment, FileSystemLoader if _name_ = "_main_": values = yaml.load(open('./value.yaml')) # Load templates file from templtes folder env = Environment( loader = FileSystemLoader('./templates'), trim_blocks=True, lstrip_blocks=True) template = env.get_template('compoment_tmpl.j2') for service in values: file=open("resultfile/SVC-"+service+".yaml", "w") file.write(template.render(service)) file.close() Second, we need to load the value from yaml file and pass to the template. apiVersion: test/v1alpha1 kind: Component metadata: name: You can describe whatever the struct of this file. So, let’s start to do some script to generate this :įirst, we need a template compoment_tmpl.j2 file below.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |