如何通过python代码根据模板修改变量生成新yaml文件
有些时候,需要根据一个yaml模板创建多个yaml文件实例,我们先写一个yaml文件模板,然后通过python代码修改模板中的变量,存储为一个新的yaml文件。需要配合python的库Template及ymal使用。
- 创建yaml模板
如下截取了OAI核心网启动yaml文件docker-compose-basic-vpp-nrf-slice4
中的部分代码,将其中的部分参数设置为变量
创建的变量的格式为 ${variable}
或者$variable
version: ${version} services: ${mysql_sever_name}: container_name: ${mysql_container_name} image: mysql:8.0 volumes: - ./database/oai_db2.sql:/docker-entrypoint-initdb.d/oai_db.sql - ./healthscripts/mysql-healthcheck2.sh:/tmp/mysql-healthcheck.sh environment: - TZ=Europe/Paris - MYSQL_DATABASE=oai_db - MYSQL_USER=test - MYSQL_PASSWORD=test - MYSQL_ROOT_PASSWORD=linux healthcheck: test: /bin/bash -c "/tmp/mysql-healthcheck.sh" interval: 10s timeout: 5s retries: 30 networks: public_net: ipv4_address: ${mysql_ip} networks: public_net: driver: bridge name: ${network1_name} ipam: config: - subnet: ${network1_subnet} driver_opts: com.docker.network.bridge.name: ${network1_driver}
编写修改模板中变量的代码
from string import Template import yaml with open('D:\Visual Stdio file\onekeydeploye_network\changeyaml\yaml_template.yaml', encoding='utf-8') as fp: #打开yaml模板文件,指定到fp中 read_yaml= fp.read() #读取fp文件内容,存储到read_yaml中,类型为str tempTemplate1=Template(read_yaml) mysql_container_name="mysql1" #可以定义字符串变量,然后引用进行yaml文件的修改 c = tempTemplate1.safe_substitute({"mysql_container_name":mysql_container_name,"mysql_ip":"192.168.10.1","mysql_sever_name":"mysql1","natwork1_name":"demo-oai-public-net-slice4","network1_subnet":"192.168.10.0/24","network1_driver":'"oai-slice4"'}) print(c) yaml_date = yaml.safe_load(c) #返回python字典到yaml_date中 print(yaml_date) with open('docker-compose-basic-vpp-nrf4.yaml', 'w') as fp: yaml.dump(yaml_date, fp,sort_keys=False) #sort_keys=False 可以避免yaml文件顺序出先异常
运行结果
version: ${version}
services:
mysql1:
container_name: mysql1
image: mysql:8.0
volumes:
- ./database/oai_db2.sql:/docker-entrypoint-initdb.d/oai_db.sql
- ./healthscripts/mysql-healthcheck2.sh:/tmp/mysql-healthcheck.sh
environment:
- TZ=Europe/Paris
- MYSQL_DATABASE=oai_db
- MYSQL_USER=test
- MYSQL_PASSWORD=test
- MYSQL_ROOT_PASSWORD=linux
healthcheck:
test: /bin/bash -c "/tmp/mysql-healthcheck.sh"
interval: 10s
timeout: 5s
retries: 30
networks:
public_net:
ipv4_address: 192.168.10.1networks:
public_net:
driver: bridge
name: ${network1_name}
ipam:
config:
- subnet: 192.168.10.0/24
driver_opts:
com.docker.network.bridge.name: "oai-slice4"{'version': '${version}', 'services': {'mysql1': {'container_name': 'mysql1', 'image': 'mysql:8.0', 'volumes': ['./database/oai_db2.sql:/docker-entrypoint-initdb.d/oai_db.sql', './healthscripts/mysql-healthcheck2.sh:/tmp/mysql-healthcheck.sh'], 'environment': ['TZ=Europe/Paris', 'MYSQL_DATABASE=oai_db', 'MYSQL_USER=test', 'MYSQL_PASSWORD=test', 'MYSQL_ROOT_PASSWORD=linux'], 'healthcheck': {'test': '/bin/bash -c "/tmp/mysql-healthcheck.sh"', 'interval': '10s', 'timeout': '5s', 'retries': 30}, 'networks': {'public_net': {'ipv4_address': '192.168.10.1'}}}}, 'networks': {'public_net': {'driver': 'bridge', 'name': '${network1_name}', 'ipam': {'config': [{'subnet': '192.168.10.0/24'}]}, 'driver_opts': {'com.docker.network.bridge.name': 'oai-slice4'}}}}
参考阅读:
到此这篇关于如何通过python代码根据模板修改变量生成新yaml文件的文章就介绍到这了,更多相关python创建yaml模板文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Python urllib模块urlopen()与urlretrieve()详解
Python urllib模块urlopen()与urlretrieve()的使用方法详解。2013-11-11
最新评论