diff --git a/plugins/inventory/rax.py b/plugins/inventory/rax.py index 162884b271..59c86917b8 100755 --- a/plugins/inventory/rax.py +++ b/plugins/inventory/rax.py @@ -66,8 +66,8 @@ authors: - Jesse Keating - Paul Durivage notes: - - One environment variable needs to be set: RAX_CREDS_FILE. - - RAX_CREDS_FILE points to a credentials file appropriate for pyrax. + - RAX_CREDS_FILE is an optional environment variable that points to a pyrax-compatible credentials file. + - If RAX_CREDS_FILE is not supplied, rax.py will look for a credentials file at ~/.rackspace_cloud_credentials. - See https://github.com/rackspace/pyrax/blob/master/docs/getting_started.md#authenticating - RAX_REGION is an optional environment variable to narrow inventory search scope - RAX_REGION, if used, needs a value like ORD, DFW, SYD (a Rackspace datacenter) and optionally accepts a comma-separated list @@ -181,14 +181,22 @@ def parse_args(): def setup(): + default_creds_file = os.path.expanduser('~/.rackspace_cloud_credentials') + + # Attempt to grab credentials from environment first try: creds_file = os.environ['RAX_CREDS_FILE'] - region = os.getenv('RAX_REGION') except KeyError, e: - sys.stderr.write('Unable to load environment ' - 'variable %s\n' % e.message) - sys.exit(1) + # But if that fails, use the default location of ~/.rackspace_cloud_credentials + if os.path.isfile(default_creds_file): + creds_file = default_creds_file + else: + sys.stderr.write('No value in environment variable %s and/or no ' + 'credentials file at %s\n' + % (e.message, default_creds_file)) + sys.exit(1) + region = os.getenv('RAX_REGION') pyrax.set_setting('identity_type', 'rackspace') try: