使用ruamel.yaml库,解析yaml文件
在实现的需求如下:
同事提供了一个文本文件,内含200多个host与ip的对应关系,希望能在k8s生成pod时,将这些对应关系注入到/etc/hosts中。
网上看文档,这可以通过扩充pod中的hostAliases来实现。
实现的思路如下:
一,hosts文件内容示例
192.168.0.24 bi-server-3391 192.168.0.25 bi-server-3392 192.168.0.26 bi-server-3393 192.168.0.27 bi-server-3394 192.168.0.28 bi-server-3395 192.168.0.29 bi-server-3396 192.168.0.30 bi-server-3397 192.168.0.31 bi-server-3398 192.168.0.32 bi-server-3399 192.168.0.33 bi-server-3400 192.168.0.34 bi-server-3401 192.168.0.35 bi-server-3402 192.168.0.36 bi-server-3403 192.168.0.37 bi-server-3404 192.168.0.38 bi-server-3405 192.168.0.39 bi-server-3406 192.168.0.40 bi-server-3407
二,org_dep.yaml文件内容
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: xxx-ai-jupyter-v2
spec:
replicas: 1
selector:
matchLabels:
name: xxx-ai-jupyter-v2
template:
metadata:
labels:
name: xxx-ai-jupyter-v2
spec:
imagePullSecrets:
- name: xxx
nodeSelector:
accelerator: nvidia-tesla-k80
containers:
- name: xxx-ai-jupyter-v2
image: harbor.xxx.com.cn/3rd_part/tensorflow:xxx
imagePullPolicy: IfNotPresent
command: ["bash", "-c", "jupyter notebook --notebook-dir=/tf --ip 0.0.0.0 --no-browser --allow-root --NotebookApp.allow_remote_access=True --NotebookApp.disable_check_xsrf=True --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.allow_origin='*' --NotebookApp.allow_origin='*'"]
resources:
limits:
nvidia.com/gpu: 4
volumeMounts:
- mountPath: /tf
name: jupyter-data
volumes:
- name: jupyter-data
hostPath:
# directory location on host
path: /docker/jupyter_data
hostAliases:
- ip: "127.0.0.1"
hostnames:
- "bar.local"
- ip: "10.1.2.3"
hostnames:
- "bar.remote"
三,解析yaml并新增hostsAliases段的python脚本
# coding:utf-8
from ruamel import yaml as ruamel_yaml
import yaml
import os
cur_path = os.path.dirname(os.path.realpath(__file__))
org_dep_yaml = os.path.join(cur_path, "org_dep.yaml")
hosts_file = os.path.join(cur_path, "hosts")
f1 = open(org_dep_yaml)
d1 = yaml.load(f1)
yaml_host = d1['spec']['template']['spec']['hostAliases']
with open("hosts", 'r') as f:
for i in f:
if len(i.strip()) > 0:
temp_list = i.split()
temp_dict = dict()
temp_dict['ip'] = temp_list[0]
temp_dict['hostnames'] = [temp_list[1]]
yaml_host.append(temp_dict)
d1['spec']['template']['spec']['hostAliases'] = yaml_host
# 如果用原生的yaml功能,yaml文件一些列表项会有引号,所以要用ruamel的yaml库。
# with open("dst_dep.yaml", "w", encoding="utf-8") as f:
# yaml.dump(d1, f)
# 写入到yaml文件
with open("dst_dep.yaml", "w", encoding="utf-8") as f:
ruamel_yaml.dump(d1, f, Dumper=ruamel_yaml.RoundTripDumper)
四,最后扩展后的Yaml.
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: xxx-ai-jupyter-v2
spec:
replicas: 1
selector:
matchLabels:
name: xxx-ai-jupyter-v2
template:
metadata:
labels:
name: xxx-ai-jupyter-v2
spec:
imagePullSecrets:
- name: xxx
nodeSelector:
accelerator: nvidia-tesla-k80
containers:
- name: xxx-ai-jupyter-v2
image: harbor.xxx.com.cn/3rd_part/tensorflow:xxx
imagePullPolicy: IfNotPresent
command: ["bash", "-c", "jupyter notebook --notebook-dir=/tf --ip 0.0.0.0 --no-browser --allow-root --NotebookApp.allow_remote_access=True --NotebookApp.disable_check_xsrf=True --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.allow_origin='*' --NotebookApp.allow_origin='*'"]
resources:
limits:
nvidia.com/gpu: 4
volumeMounts:
- mountPath: /tf
name: jupyter-data
volumes:
- name: jupyter-data
hostPath:
# directory location on host
path: /docker/jupyter_data
hostAliases:
- ip: 127.0.0.1
hostnames:
- bar.local
- ip: 10.1.2.3
hostnames:
- bar.remote
- ip: 192.16.0.24
hostnames:
- bi-server-33391
- ip: 192.16.0.25
hostnames:
- bi-server-33392
- ip: 192.16.0.26
hostnames:
- bi-server-33393
......
五。END.最后,将这些yaml合进其它yaml文件即可,这时,脚本就需要进一步加功能了。
使用ruamel.yaml库,解析yaml文件的更多相关文章
- ACEXML解析XML文件——简单示例程序
掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...
- c++ 解析yaml文件
一直用c++操作ini做配置文件,想换成yaml,在全球最大的同性交友网站github上搜索,看有没有开源的库,功夫不负有心人,找到了yaml-cpp,用他解析了一个yaml的例子非常好使,分享一下如 ...
- python基础——python解析yaml类型文件
一.yaml介绍 yaml全称Yet Another Markup Language(另一种标记语言).采用yaml作为配置文件,文件看起来直观.简洁.方便理解.yaml文件可以解析字典.列表和一些基 ...
- Java使用snakeyaml解析yaml
YAML Yaml是一种"是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言."类似于XML但比XML更简洁,语法详见 http://www.ruan ...
- ACEXML解析XML文件——我是如何学习并在短时间内掌握一个库的使用方法的
最近做的C++项目中需要使用xml文件保存一些信息,程序启动时会读取这些信息.最终经过主程的评测,决定使用ACEXML库来读取解析XML文件. 好吧,至于为什么选择ACEXML库,我就不说了.既然选择 ...
- Ajax实现xml文件数据插入数据库(一)--- 构建解析xml文件的js库
Ajax实现将xml文件数据插入数据库的过程所涉及到的内容比较多,所以对于该过程的讲解本人打算根据交互的过程将其分为三个部分,第一部分为构建解析xml文件的javascript库,第二部分为ajax与 ...
- Java解析YAML和Android解析YAML
一.Java解析YAML 1. API的选择 一般分两种:Jyaml和snakeYAML.(Jyaml下载地址:http://download.csdn.net/detail/dgssfgfs/847 ...
- 解析prototxt文件的python库 prototxt-parser(使用parsy自定义文件格式解析)
解析prototxt文件的python库 prototxt-parser https://github.com/yogin16/prototxt_parser https://test.pypi.or ...
- swagger.yaml转换为swagger.json文件
方法一 swagger-editor页面 官方的 swagger-editor Live Demo (在线直接使用,就是访问的有点慢)或者将swagger-editor Download 下载到本地然 ...
随机推荐
- 19.8.2 luogu 夏令营 游
人生第一次游记....是在学校机房,在luogu网校,在炎炎夏日,在薯条汉堡,在自己的博客里... 十二天快吗? 刚上课那会真的比较兴奋,把每天的计划都排的满满的,希望这十二天慢点,再慢点,我得好好过 ...
- scrapy 爬取图片
scrapy 爬取图片 1.scrapy 有下载图片的自带接口,不用我们在去实现 setting.py设置 # 保存log信息的文件名 LOG_LEVEL = "INFO" # L ...
- vue.js 使用v-model v-once
v-model 双向绑定 v-once 单项绑定 代码: <!doctype html> <html lang="en"> <head> < ...
- unique_ptr的实现原理
在C++11中有两个智能指针类型来管理动态对象,share_ptr允许多个指针指向同一个对象,unique_ptr则“独占”所指对象. 我们知道指针或引用在离开作用域时是不会进行析构的,但是类在离开作 ...
- CodeForces - 545CWoodcutters
传送门 题目大意:n棵树(10^5),坐标xi,高度hi,把这棵树砍到,可以向右倒[xi,xi+hi]被占, 向左倒[xi-hi,xi]被占,必须要倒的坐标没有被占才能倒,不砍倒就xi被占,问最多砍几 ...
- C++ 实现 查找进程, 杀死进程, 启动进程, 进程重启
头文件: #include <Windows.h>#include <tlhelp32.h>#include <tchar.h>#include <Shell ...
- kafka_2.12-2.2.1 集群搭建
一.zookeeper集群搭建 kafka集群依赖于zookeeper的集群,搭建zookeeper集群的步骤参考我之前写过的,Solr集群搭建详细教程(一)中的第二步 二.下载解压 去官网下载htt ...
- Java-100天知识进阶-JVM内存-知识铺(三)
知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停的来唤醒你记忆深处的知识点. Java内存模型(JMM) JVM内存模式是JVM的内存分区 Java内存模式是一种虚 ...
- F#周报2019年第19期
新闻 介绍.NET 5 发布.NET Core 3.0预览版5以及F#的REPL OpenFsharp CFP开启 F#的Giraffe服务端stub生成器被添加到openapi-generator中 ...
- java基于NIO的分散读取文件,然后统一聚合后写入文件
分散读取:对于一个文件,可以分散的读取数据,可以快速的读取,好比多个线程在分段同时读取: 聚合写入:为了提高效率,一般读取到的数据都是分散的,要快速写入,就需要把分散的数据聚集在一起,然后一块写入到文 ...