使用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 下载到本地然 ...
随机推荐
- jQuery的配置。
在python中有提前定义模板的功能,所以提前将jQuery的导入语句导入就可以直接使用jQuery语法: 一.下载jQuery包. 下载官网: https://jquery.com/ 可下载迷你版的 ...
- 剑指Offer-4.重建二叉树(C++/Java)
题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2 ...
- 工具资源系列之给 windows 虚拟机装个 centos
前面我们已经介绍了如何在 Windows 宿主机安装 VMware 虚拟机,这节我们将利用安装好的 VMware 软件安装 centos 系统. 前情回顾 由于大多数人使用的 Windows 电脑而工 ...
- 7.Java内存模型详解
https://blog.csdn.net/qq_37141773/article/details/103138476 一.虚拟机 同样的java代码在不同平台生成的机器码肯定是不一样的,因为不同的操 ...
- ajax技术初识与应用
一.ajax技术初识 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术.Ajax不是一种新的编 ...
- Codeforces Round #553 (Div. 2) E 贡献
https://codeforces.com/contest/1151/problem/E 题意 一条长n的链,每个点上有值\(a[i]\),定义\(f(l,r)\)为该区间的\(值\)所代表的点留下 ...
- Visual Studio 2015 Tools for Unity使用基础
Unity4.x编辑器侧 具体版本号:Visual Studio 2015 Tools for Unity 3.7.0.1 该插件在:Microsoft Visual Studio Tools for ...
- navicat远程连接mysql的方法
navicat远程连接mysql的方法1 先在打开phpmyadmin 添加用户 用户名和密码自己设置 设置如下 2 关闭防火墙service iptables status可以查看到iptables ...
- 手风琴效果 animate
animate的手风琴效果 <style type="text/css"> * { margin: 0; padding: 0; } ul{ list-style: n ...
- WPF 3D相机基本坐标简介
基本概念 WPF中3D空间基本坐标系是右手坐标系. WPF中3D空间的原点是(0,0,0) Position: 这个参数用来表示相机在空间内的坐标.参数是(X,Y,Z).当修改相机的这个参数时,这个坐 ...