PyYAML
- The next generation YAML parser and emitter for Python.
PyYAML has some really nice features to manipulate YAML files. Was working on getting some configuration using YAML file.
Had a requirement where one of the variables had to be appended to another on the fly with the YAML.
Also the base variable would dynamically change (BaseCDH
) from a chef run.
Creating Variable &base_cluster_name
.
clusters:
# Basic cluster information
- cluster: &base_cluster_name !!python/object/apply:string.upper [BaseCDH] # Keep this CAPTITAL as we will using this tin each role and service.
Substitution in config.
KAFKA:
config:
cluster_name: *base_cluster_name
zookeeper_service: !!python/object/apply:string.join [[ZOOKEEPER, *base_cluster_name], '-']
zookeeper.chroot: /kafka
In the below string we are passing the BaseCDH
from the chef run. And we want this to be an UPPERcase.
So we are using the !!python/object/apply:string.upper
,
string operation, which are similar to string process methods here https://docs.python.org/2/library/stdtypes.html#str.upper
we can any of then as required.
!!python/object/apply:string.upper [BaseCDH]
Next &base_cluster_name
is the place holder for the variable.
We need to then concat strings so we use the join
operation.
!!python/object/apply:string.join [[ZOOKEEPER, *base_cluster_name], '-']
This would result in a string as ZOOKEEPER-BASECDH
. BASECDH
keeps changes based on the cluster_name
.
Example: Yaml Configuration.
clusters:
# Basic cluster information
- cluster: &base_cluster_name !!python/object/apply:string.upper [BaseCDH] # Keep this CAPTITAL as we will using this tin each role and service.
# Service. All the service definitions go here
services:
KAFKA:
config:
zookeeper_service: !!python/object/apply:string.join [[ZOOKEEPER, *base_cluster_name], '-']
zookeeper.chroot: /kafka
Output:
{
'clusters': [
{
'services': {
'KAFKA': {
'config': {
'zookeeper_service': 'ZOOKEEPER-BASECDH',
'zookeeper.chroot': '/kafka'
}
}
},
'cluster': 'BASECDH'
}
]
}