创建yaml模板文件,通过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.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"


{'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'}}}}

创建yaml模板文件,通过python代码根据模板修改变量生成新yaml文件_第1张图片

PYYAML模块
yaml.dump()按原顺序存储
Template模块使用

你可能感兴趣的:(Python,docker,yaml,python,开发语言)