mongodb - 在 Ubuntu 中使用 shell 脚本更新 YAML 文件

标签 mongodb shell yaml ubuntu-16.04

我必须更新 YAML 文件配置。以下是当前和预期的输出。如何使用 shell 脚本以更好、更简单的方式做到这一点?

更新 YAML 文件之前:

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

更新 YAML 文件后:

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
  engine: "wiredTiger"
#  mmapv1:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 4

最佳答案

如果您的输入位于 config_in.yaml 中:

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

您可以使用 update.py 调用 python update.pywiredTiger 4:

import sys
from pathlib import Path

from ruamel.yaml import YAML

file_name = Path('config_in.yaml')

engine = sys.argv[1]
size = int(sys.argv[2])


yaml = YAML()
data = yaml.load(file_name)
data['storage']['engine'] = engine
data['storage'][engine] = dict(engineConfig=dict(cacheSizeGB=size))
yaml.dump(data, sys.stdout)
yaml.dump(data, Path('config.yaml'))

获取此输出(在 stdout 以及 config.yaml 中):

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
  engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 4

这假设Python3(或安装了pathlib2的Python2)和ruamel.yaml(我是作者)

关于mongodb - 在 Ubuntu 中使用 shell 脚本更新 YAML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46190396/

相关文章:

r - R : (Package which is only available in source form, 错误,可能需要编译 C/C++/Fortran)

node.js - 如何使用 mongoose 从数组字段中获取特定对象

java - Web 应用程序、集群 tomcat 和 mongoDB - 如何实现持久队列

linux - 组合两个 linux 命令以显示为单个字符串

linux - Shell脚本根据参数将单行翻译成多行

javascript - js-yaml 可以进行行号映射吗?

yaml - 开放API继承示例数据

java - Spring Boot RESTful 应用程序错误

java - 从字符串到 MongoDB ObjectID 的转换

shell - 从 shell 写入 fifo/pipe,有超时