diff --git a/contrib/inventory/gce.ini b/contrib/inventory/gce.ini index 741eb06a98c..af27a9c4ab3 100644 --- a/contrib/inventory/gce.ini +++ b/contrib/inventory/gce.ini @@ -29,6 +29,7 @@ # (See ansible/test/gce_tests.py comments for full install instructions) # # Author: Eric Johnson +# Contributors: John Roach [gce] # GCE Service Account configuration information can be stored in the @@ -41,6 +42,8 @@ libcloud_secrets = # If you are not going to use a 'secrets.py' file, you can set the necessary # authorization parameters here. +# You can add multiple gce projects to by using a comma separated list. Make +# sure that the service account used has permissions on said projects. gce_service_account_email_address = gce_service_account_pem_file_path = gce_project_id = diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index d0cc6b15536..e72f343e36f 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -70,8 +70,9 @@ Examples: $ contrib/inventory/gce.py --host my_instance Author: Eric Johnson -Contributors: Matt Hite , Tom Melendez -Version: 0.0.3 +Contributors: Matt Hite , Tom Melendez , + John Roach +Version: 0.0.4 ''' try: @@ -167,7 +168,7 @@ class GceInventory(object): # Read settings and parse CLI arguments self.parse_cli_args() self.config = self.get_config() - self.driver = self.get_gce_driver() + self.drivers = self.get_gce_drivers() self.ip_type = self.get_inventory_options() if self.ip_type: self.ip_type = self.ip_type.lower() @@ -277,9 +278,9 @@ class GceInventory(object): ip_type = os.environ.get('INVENTORY_IP_TYPE', ip_type) return ip_type - def get_gce_driver(self): - """Determine the GCE authorization settings and return a - libcloud driver. + def get_gce_drivers(self): + """Determine the GCE authorization settings and return a list of + libcloud drivers. """ # Attempt to get GCE params from a configuration file, if one # exists. @@ -325,12 +326,16 @@ class GceInventory(object): kwargs['project'] = os.environ.get('GCE_PROJECT', kwargs['project']) kwargs['datacenter'] = os.environ.get('GCE_ZONE', kwargs['datacenter']) - # Retrieve and return the GCE driver. - gce = get_driver(Provider.GCE)(*args, **kwargs) - gce.connection.user_agent_append( - '%s/%s' % (USER_AGENT_PRODUCT, USER_AGENT_VERSION), - ) - return gce + gce_drivers = [] + projects = kwargs['project'].split(',') + for project in projects: + kwargs['project'] = project + gce = get_driver(Provider.GCE)(*args, **kwargs) + gce.connection.user_agent_append( + '%s/%s' % (USER_AGENT_PRODUCT, USER_AGENT_VERSION), + ) + gce_drivers.append(gce) + return gce_drivers def parse_env_zones(self): '''returns a list of comma separated zones parsed from the GCE_ZONE environment variable. @@ -420,9 +425,10 @@ class GceInventory(object): all_nodes = [] params, more_results = {'maxResults': 500}, True while more_results: - self.driver.connection.gce_params = params - all_nodes.extend(self.driver.list_nodes()) - more_results = 'pageToken' in params + for driver in self.drivers: + driver.connection.gce_params = params + all_nodes.extend(driver.list_nodes()) + more_results = 'pageToken' in params return all_nodes def group_instances(self, zones=None):