Update YAML file using shell script in Ubuntu
If your input is in config_in.yaml
:
# Where and how to store data.storage: dbPath: /var/lib/mongodb journal: enabled: true
You can call python update.py wiredTiger 4
with update.py
:
import sysfrom pathlib import Pathfrom ruamel.yaml import YAMLfile_name = Path('config_in.yaml')engine = sys.argv[1]size = int(sys.argv[2])yaml = YAML()data = yaml.load(file_name)data['storage']['engine'] = enginedata['storage'][engine] = dict(engineConfig=dict(cacheSizeGB=size))yaml.dump(data, sys.stdout)yaml.dump(data, Path('config.yaml'))
to get this output (on stdout as well as in config.yaml
):
# Where and how to store data.storage: dbPath: /var/lib/mongodb journal: enabled: true engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 4
This assumes Python3 (or Python2 with pathlib2 installed), and ruamel.yaml (of which I am the author)