Reputation: 827
I have a configuration file in YAML (Cassandra) and I would like to update a few values inside it. Do you have any suggestions for a script that would allow me to do this? Perhaps some existing examples?
INPUT:
cluster_name: 'CassandraCluster0'
initial_token:
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: "127.0.0.1"
OUTPUT:
cluster_name: 'CassandraCluster0'
initial_token: 582023494802482234
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: "10.12.3.4, 1.3.4.3"
Upvotes: 1
Views: 2379
Reputation: 76578
You can of course update text in a YAML file with sed
/awk
/perl
, but as with updating CSV, INI, XML, HTML using those tools, they horribly fail on more complex examples where values "all of a sudden" span multiple lines, or are otherwise semantically the same as what worked, but are no longer consisting of the same matched strings.
It is better to just use a programming language and a real parser (which is also the recommendation for the other formats). Here is how you can do so with Python and ruamel.yaml, of which I am the author. So if your input is in input.yml
, this:
import ruamel.yaml as yaml
data = yaml.load(open('input.yml'), Loader=yaml.RoundTripLoader)
data['initial_token'] = 582023494802482234
data['seed_provider'][0]['parameters'][0]['seeds'] = "10.12.3.4, 1.3.4.3"
print yaml.dump(data, Dumper=yaml.RoundTripDumper)
will get you:
cluster_name: CassandraCluster0
initial_token: 582023494802482234
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: 10.12.3.4, 1.3.4.3
Please note that there are not quotes around the IP addresses, as they are not necessary. It is possible, but not very easy to fine control the style to only put (double) quotes around values. And even more difficult to do so around specific values, so I hope that is not an issue.
If you have to update a set of input files for these two values often you can make script that takes the filename, token and seed provider from the commandline:
update_yml input.yml 582023494802482234 "10.12.3.4, 1.3.4.3"
and as script update_yml
:
#! /usr/bin/env python
import sys
import ruamel.yaml as yaml
data = yaml.load(open(sys.argv[1]), Loader=yaml.RoundTripLoader)
data['initial_token'] = sys.argv[2]
data['seed_provider'][0]['parameters'][0]['seeds'] = sys.argv[3]
print yaml.dump(data, Dumper=yaml.RoundTripDumper)
Upvotes: 1