Get the App
SLTechnology News&Howtos  ›  Servers  › 

Python automatically generates k8s cluster yaml files

Shulou Source: shulou.com Published: 2022-06-02 22:24:21 10月04日 Update

The yaml module is used to process yaml files. Yaml is a lighter file format than xml and json. K8s cluster declares that the resource object is to use yaml files, and it is more convenient to use the yaml module to process yaml files.

Common methods of module

Dump: generates a python object as an yaml document

Load: returns an object

Dump_all:

Load_all:

Simple example demonstration

Load

Yaml_str1 = "" name: jackage: 25job: IT "" print (yaml_str1, type (yaml_str1)) contnet = yaml.load (yaml_str1) print (contnet, type (contnet))

Output

Name: jackage: 25job: IT {'name':' jack', 'age': 25,' job': 'IT'}

Dump

Import yamlyaml_str1 = "name: jackage: 25job: IT"print (yaml_str1, type (yaml_str1)) contnet = yaml.load (yaml_str1) print (contnet, type (contnet)) new_content = yaml.dump (contnet, open ('t1.yamlite,' wicked, encoding='utf-8'))

View the generated file

# generate the yaml file of nginx

Import yamlwith open ('nginx.yaml',' ritual, encoding='utf-8') as F1: content = yaml.load (F1) print (F1) with open ('nginxv2.yaml', 'walled, encoding='utf-8') as f2: yaml.dump (content, f2) # file stored after contnet object f2 generation

View the generated file

The generated file can be run

Multiple resource objects can be declared in the yaml file, using load_all

Import yamlwith open ('nginx_busy.yaml',' ritual, encoding='utf-8') as F1: content = yaml.load_all (F1) new_content = yaml.dump_all (content) print (new_content, type (new_content)) with open ('ng_busy.yaml', 'walled, encoding='utf-8') as f2: f2.write (new_content)

View the generated file

Tags: File generation object module resource processing cluster multiple that is commonly used document method format example demonstration output run automatic generation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker NVidia Shulou Tech Info vpn