diff options
author | John Roach <JohnRoach@users.noreply.github.com> | 2018-05-11 17:38:13 +0200 |
---|---|---|
committer | ansibot <ansibot@users.noreply.github.com> | 2018-05-11 17:38:13 +0200 |
commit | 22456a57de0c45efa6cd0a274d677f5e4c5af847 (patch) | |
tree | b115d643e2ab992b51457e9016ac8dc14f65b9b8 /contrib/inventory | |
parent | "route" has to be the first option in ufw command (#31756) (diff) | |
download | ansible-22456a57de0c45efa6cd0a274d677f5e4c5af847.tar.xz ansible-22456a57de0c45efa6cd0a274d677f5e4c5af847.zip |
Adding multiple project support for GCE (#39473)
* Adding multiple project support for GCP inventory.
* Adding some documentation on the .ini file
Diffstat (limited to 'contrib/inventory')
-rw-r--r-- | contrib/inventory/gce.ini | 3 | ||||
-rwxr-xr-x | contrib/inventory/gce.py | 36 |
2 files changed, 24 insertions, 15 deletions
diff --git a/contrib/inventory/gce.ini b/contrib/inventory/gce.ini index 741eb06a98..af27a9c4ab 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 <erjohnso@google.com> +# Contributors: John Roach <johnroach1985@gmail.com> [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 d0cc6b1553..e72f343e36 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 <erjohnso@google.com> -Contributors: Matt Hite <mhite@hotmail.com>, Tom Melendez <supertom@google.com> -Version: 0.0.3 +Contributors: Matt Hite <mhite@hotmail.com>, Tom Melendez <supertom@google.com>, + John Roach <johnroach1985@gmail.com> +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): |