Skip to content

Instantly share code, notes, and snippets.

@mandre
Last active February 1, 2021 15:00
Show Gist options
  • Save mandre/0fb92091d2c491b7169b6decbc0b1633 to your computer and use it in GitHub Desktop.
Save mandre/0fb92091d2c491b7169b6decbc0b1633 to your computer and use it in GitHub Desktop.
Convert from clouds.yaml to environment variables
#!/usr/bin/env python
import os
import shlex
import yaml
cloud = os.environ.get('OS_CLOUD')
if not cloud:
print("OS_CLOUD isn't set. Export your cloud environment with OS_CLOUD.")
exit(1)
config_file = os.environ.get('OS_CLIENT_CONFIG_FILE')
if not config_file:
print("OS_CLIENT_CONFIG_FILE isn't set. Export the path to clouds.yaml with OS_CLIENT_CONFIG_FILE.")
exit(1)
# https://docs.openstack.org/openstacksdk/latest/user/config/configuration.html
# The keys are all of the keys you’d expect from OS_* - except lower case and
# without the OS prefix. So, region name is set with region_name.
def parse_key(clouds_key, clouds_value):
if clouds_key == 'auth':
for k in clouds_value:
parse_key(k, clouds_value[k])
elif not clouds_key == 'regions':
print('export OS_%s=%s' % (clouds_key.upper(), shlex.quote(str(clouds_value))))
with open(config_file) as f:
data = yaml.safe_load(f)
if not data.get('clouds', []).get(cloud):
print("Cloud %s doesn't exist in %s" % (cloud, config_file))
exit(1)
for k in data['clouds'][cloud]:
parse_key(k, data['clouds'][cloud][k])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment