yaml
file:
$ cat ec2_attr.yml
treeroot:
branch1:
name: Node 1
snap: |
def foo():
from boto3.session import Session
import pprint
session = Session(region_name='us-east-1')
client = session.client('rds')
resp = client.describe_db_cluster_snapshots(
SnapshotType='manual',
)
filtered = [x for x in resp['DBClusterSnapshots'] if x[
'DBClusterSnapshotIdentifier'].startswith('xxxxx')]
latest = max(filtered, key=lambda x: x['SnapshotCreateTime'])
print(latest['DBClusterSnapshotIdentifier'])
foo()
branch2:
name: Node 2
Code:
import yaml
import pprint
with open('./ec2_attr.yml') as fh:
try:
yaml_dict = yaml.load(fh)
except Exception as e:
print(e)
else:
exec("a = yaml_dict['treeroot']['branch1']['snap']")
print('The Value is: %s' % (a))
Actual Output:
The Value is: def foo():
from boto3.session import Session
import pprint
session = Session(region_name='us-east-1')
client = session.client('rds')
resp = client.describe_db_cluster_snapshots(
SnapshotType='manual',
)
filtered = [x for x in resp['DBClusterSnapshots'] if x[
'DBClusterSnapshotIdentifier'].startswith('xxxxx')]
latest = max(filtered, key=lambda x: x['SnapshotCreateTime'])
print(latest['DBClusterSnapshotIdentifier'])
foo()
Expected output:
xxxxx-xxxx-14501111111-xxxxxcluster-2gwe6jrnev8a-2017-04-09
If I use exec
as exec(yaml_dict['treeroot']['branch1']['snap'])
, Then it does print the value that I want, but I cannot capture that value into a variable. I understand that exec
return value is None
. However, I am trying to do something exactly similar to https://stackoverflow.com/a/23917810/1251660, and it is not working in my case.