Skip to content

Commit

Permalink
Add environmental variables to allow for the script to be run without…
Browse files Browse the repository at this point in the history
… consul_io.ini. This allows it to run dynamically without any prerequisite config file (ansible#30900)
  • Loading branch information
jamiesonio authored and ansibot committed Sep 19, 2018
1 parent 534de2d commit 6711c95
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions contrib/inventory/consul_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
'datacenter':
which restricts the included nodes to those from the given datacenter
This can also be set with the environmental variable CONSUL_DATACENTER
'url':
the URL of the Consul cluster. host, port and scheme are derived from the
URL. If not specified, connection configuration defaults to http requests
to localhost on port 8500.
This can also be set with the environmental variable CONSUL_URL
'domain':
Expand Down Expand Up @@ -453,6 +455,7 @@ class ConsulConfig(dict):
def __init__(self):
self.read_settings()
self.read_cli_args()
self.read_env_vars()

def has_config(self, name):
if hasattr(self, name):
Expand Down Expand Up @@ -497,6 +500,14 @@ def read_cli_args(self):
if getattr(args, arg):
setattr(self, arg, getattr(args, arg))

def read_env_vars(self):
env_var_options = ['datacenter', 'url']
for option in env_var_options:
value = None
env_var = 'CONSUL_' + option.upper()
if os.environ.get(env_var):
setattr(self, option, os.environ.get(env_var))

def get_availability_suffix(self, suffix, default):
if self.has_config(suffix):
return self.has_config(suffix)
Expand Down

0 comments on commit 6711c95

Please sign in to comment.